Help - Search - Members - Calendar
Full Version: Mail function
HTMLHelp Forums > Programming > Server-side Scripting
tudsy
Hi

I am trying to test out sending HTML in an email with no luck.


Here is the code in test.php.Click to view attachment
CharlesEF
I send mail that contain both plain text and html in the same e-mail.

This is an overview of what you need to do:
CODE
<?php
// Generate random hash to use as e-mail multipart_boundary
$rnd_hash = md5(date("r", time()));
$to = "xxxx@yyyy.com"; // or "xxxx@yyyy.com, yyyy@zzzz.com, ..."
$subject = "Subject";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/alternative; boundary=\"Multipart_Boundary_{$rnd_hash}\"\r\n";
$headers .= "From: \"Joe Blow, Inc.\" <joe@blow.com>\r\n";
$message = "--Multipart_Boundary_{$rnd_hash}\r\n";
$message .= "Content-Type: text/plain; charset=UTF-8\r\n";
$message .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$message .= "Text message.\r\n";
$message .= "As many\r\n";
$message .= "lines\r\n";
$message .= "as needed.\r\n";
$message .= "--Multipart_Boundary_{$rnd_hash}\r\n";
$message .= "Content-Type: text/html; charset=UTF-8\r\n";
$message .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$message .= "<html>\r\n";
$message .= "<head>\r\n";
$message .= "</head>\r\n";
$message .= "<body>\r\n";
$message .= "<p>HTML starts</p>\r\n";
$message .= "<b>and continues</b>\r\n";
$message .= "<i>as many</i>\r\n";
$message .= "<i>lines as</i>\r\n";
$message .= "<i>needed.</i>\r\n";
$message .= "</body>\r\n";
$message .= "</html>\r\n";
$message .= "--Multipart_Boundary_{$rnd_hash}--\r\n";
$message = wordwrap($message, 71);
mail($to, $subject, $message, $headers);
?>

This code should run as is, just change the $to e-mail address.
tudsy
Hi

Thanks for that.

I can send email from the test code only you gave me.

The problem is that I cannot send email accessed from a form.


The relevant files are below:










QUOTE(CharlesEF @ Feb 14 2021, 12:39 PM) *

I send mail that contain both plain text and html in the same e-mail.

This is an overview of what you need to do:
CODE
<?php
// Generate random hash to use as e-mail multipart_boundary
$rnd_hash = md5(date("r", time()));
$to = "xxxx@yyyy.com"; // or "xxxx@yyyy.com, yyyy@zzzz.com, ..."
$subject = "Subject";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/alternative; boundary=\"Multipart_Boundary_{$rnd_hash}\"\r\n";
$headers .= "From: \"Joe Blow, Inc.\" <joe@blow.com>\r\n";
$message = "--Multipart_Boundary_{$rnd_hash}\r\n";
$message .= "Content-Type: text/plain; charset=UTF-8\r\n";
$message .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$message .= "Text message.\r\n";
$message .= "As many\r\n";
$message .= "lines\r\n";
$message .= "as needed.\r\n";
$message .= "--Multipart_Boundary_{$rnd_hash}\r\n";
$message .= "Content-Type: text/html; charset=UTF-8\r\n";
$message .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$message .= "<html>\r\n";
$message .= "<head>\r\n";
$message .= "</head>\r\n";
$message .= "<body>\r\n";
$message .= "<p>HTML starts</p>\r\n";
$message .= "<b>and continues</b>\r\n";
$message .= "<i>as many</i>\r\n";
$message .= "<i>lines as</i>\r\n";
$message .= "<i>needed.</i>\r\n";
$message .= "</body>\r\n";
$message .= "</html>\r\n";
$message .= "--Multipart_Boundary_{$rnd_hash}--\r\n";
$message = wordwrap($message, 71);
mail($to, $subject, $message, $headers);
?>

This code should run as is, just change the $to e-mail address.

Click to view attachment Click to view attachment
CharlesEF
So, my sample code did work for you but you're having trouble incorporating it into your script? Is that correct?

I looked at Comm1.php, because that's the script that sends the mail. Your logic is all messed up. !st, you don't do any validation, I guess you depend on 'required' to handle that for you, you shouldn't. Next you define the function getRandomString() inside a try block. You should not do that. I don't understand why you do that at all. You could use:
CODE
substr(str_shuffle('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'),1,8)
Or, just generate a random md5 hash to use. Next, you have 2 mail calls, the 1st is right after the '$subject = ' line. You need to remove it.

You changed my code a little when you added it to your code. You should not do that. When you have a working sample of code you should add it to your code as is. First get your code working, with my sample, then you can go back and change things. You are defining complete paragraphs to the $message variable. I don't know it that would break it but you should follow my sample exactly. You should feed $message 1 line at a time. And if you want a blank line use $message .= "\r\n";

And you still don't use !DOCTYPE and you use deprecated elements like < center >.
tudsy
Hi

Thanks for your help so far.

I am having trouble sending a text email through this code: emailtext.php.

The code that you gave me only sends a text email to second@ecovib2d.com.au (my second email address).
It does not send the html part.

Thanks.









QUOTE(CharlesEF @ Feb 15 2021, 07:59 AM) *

So, my sample code did work for you but you're having trouble incorporating it into your script? Is that correct?

I looked at Comm1.php, because that's the script that sends the mail. Your logic is all messed up. !st, you don't do any validation, I guess you depend on 'required' to handle that for you, you shouldn't. Next you define the function getRandomString() inside a try block. You should not do that. I don't understand why you do that at all. You could use:
CODE
substr(str_shuffle('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'),1,8)
Or, just generate a random md5 hash to use. Next, you have 2 mail calls, the 1st is right after the '$subject = ' line. You need to remove it.

You changed my code a little when you added it to your code. You should not do that. When you have a working sample of code you should add it to your code as is. First get your code working, with my sample, then you can go back and change things. You are defining complete paragraphs to the $message variable. I don't know it that would break it but you should follow my sample exactly. You should feed $message 1 line at a time. And if you want a blank line use $message .= "\r\n";

And you still don't use !DOCTYPE and you use deprecated elements like < center >.

Click to view attachment
CharlesEF
I've been offline for 72 hours, power failure. I will look at the code later and post back.
tudsy
Sorry ok.



QUOTE(CharlesEF @ Feb 19 2021, 06:49 AM) *

I've been offline for 72 hours, power failure. I will look at the code later and post back.

CharlesEF
I haven't forgotten about you. I have to catch up on many things. I tested the code on my web server and now I get an error 500, my host provider forced PHP 7.4.13 on me and I didn't know it. Anyway, give me a few days or you can just use a library, like PHPMailer.
tudsy

Hi

Thanks for that.

Its not urgent.

Thanks.




QUOTE(CharlesEF @ Feb 26 2021, 06:20 AM) *

I haven't forgotten about you. I have to catch up on many things. I tested the code on my web server and now I get an error 500, my host provider forced PHP 7.4.13 on me and I didn't know it. Anyway, give me a few days or you can just use a library, like PHPMailer.

tudsy
Hi

I am now using PHPMailer to send out emails.


But I am getting an error.

[01-Mar-2021 10:18:16 UTC] PHP Warning: Creating default object from empty value in /home/ecovibdc/public_html/workfromhome/Emailcomm/Comm1.php on line 92
[01-Mar-2021 10:18:16 UTC] PHP Fatal error: Uncaught Error: Call to undefined method stdClass::setFrom() in /home/ecovibdc/public_html/workfromhome/Emailcomm/Comm1.php:100
Stack trace:
#0 {main}
thrown in /home/ecovibdc/public_html/workfromhome/Emailcomm/Comm1.php on line 100



The php file that I use PHPMailer is Comm1.php.







QUOTE(tudsy @ Feb 27 2021, 08:14 PM) *

Hi

Thanks for that.

Its not urgent.

Thanks.




QUOTE(CharlesEF @ Feb 26 2021, 06:20 AM) *

I haven't forgotten about you. I have to catch up on many things. I tested the code on my web server and now I get an error 500, my host provider forced PHP 7.4.13 on me and I didn't know it. Anyway, give me a few days or you can just use a library, like PHPMailer.


Click to view attachment
tudsy
Hi

I have solved the below problem.

The program works now but I get this error in the delivery of email:


This message was created automatically by mail delivery software.

A message that you sent could not be delivered to one or more of its
recipients. This is a permanent error. The following address(es) failed:

second@ecovib2d.com.au
Sender from address in blocked list


I am using second@ecovib2d.com.au as my test email address.


Thanks.





QUOTE(tudsy @ Mar 1 2021, 07:57 PM) *

Hi

I am now using PHPMailer to send out emails.


But I am getting an error.

[01-Mar-2021 10:18:16 UTC] PHP Warning: Creating default object from empty value in /home/ecovibdc/public_html/workfromhome/Emailcomm/Comm1.php on line 92
[01-Mar-2021 10:18:16 UTC] PHP Fatal error: Uncaught Error: Call to undefined method stdClass::setFrom() in /home/ecovibdc/public_html/workfromhome/Emailcomm/Comm1.php:100
Stack trace:
#0 {main}
thrown in /home/ecovibdc/public_html/workfromhome/Emailcomm/Comm1.php on line 100



The php file that I use PHPMailer is Comm1.php.







QUOTE(tudsy @ Feb 27 2021, 08:14 PM) *

Hi

Thanks for that.

Its not urgent.

Thanks.




QUOTE(CharlesEF @ Feb 26 2021, 06:20 AM) *

I haven't forgotten about you. I have to catch up on many things. I tested the code on my web server and now I get an error 500, my host provider forced PHP 7.4.13 on me and I didn't know it. Anyway, give me a few days or you can just use a library, like PHPMailer.


Click to view attachment

Click to view attachment
tudsy
Hi

Just an update.

I will use an existing (off the shelf) platform to send html emails to my list.

Thanks for your efforts so far.










QUOTE(tudsy @ Mar 2 2021, 10:58 PM) *

Hi

I have solved the below problem.

The program works now but I get this error in the delivery of email:


This message was created automatically by mail delivery software.

A message that you sent could not be delivered to one or more of its
recipients. This is a permanent error. The following address(es) failed:

second@ecovib2d.com.au
Sender from address in blocked list


I am using second@ecovib2d.com.au as my test email address.


Thanks.





QUOTE(tudsy @ Mar 1 2021, 07:57 PM) *

Hi

I am now using PHPMailer to send out emails.


But I am getting an error.

[01-Mar-2021 10:18:16 UTC] PHP Warning: Creating default object from empty value in /home/ecovibdc/public_html/workfromhome/Emailcomm/Comm1.php on line 92
[01-Mar-2021 10:18:16 UTC] PHP Fatal error: Uncaught Error: Call to undefined method stdClass::setFrom() in /home/ecovibdc/public_html/workfromhome/Emailcomm/Comm1.php:100
Stack trace:
#0 {main}
thrown in /home/ecovibdc/public_html/workfromhome/Emailcomm/Comm1.php on line 100



The php file that I use PHPMailer is Comm1.php.







QUOTE(tudsy @ Feb 27 2021, 08:14 PM) *

Hi

Thanks for that.

Its not urgent.

Thanks.




QUOTE(CharlesEF @ Feb 26 2021, 06:20 AM) *

I haven't forgotten about you. I have to catch up on many things. I tested the code on my web server and now I get an error 500, my host provider forced PHP 7.4.13 on me and I didn't know it. Anyway, give me a few days or you can just use a library, like PHPMailer.


Click to view attachment

Click to view attachment

This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2021 Invision Power Services, Inc.