Mail function |
Mail function |
tudsy |
Feb 13 2021, 07:03 AM
Post
#1
|
Advanced Member Group: Members Posts: 246 Joined: 30-September 14 Member No.: 21,611 |
Hi
I am trying to test out sending HTML in an email with no luck. Here is the code in test.php. test.php ( 1.35k ) Number of downloads: 596 |
CharlesEF |
Feb 13 2021, 10:09 PM
Post
#2
|
Programming Fanatic Group: Members Posts: 1,981 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
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 |
Feb 14 2021, 05:56 AM
Post
#3
|
Advanced Member Group: Members Posts: 246 Joined: 30-September 14 Member No.: 21,611 |
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: 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. Community.php ( 4.74k ) Number of downloads: 341 Comm1.php ( 6.06k ) Number of downloads: 338 |
CharlesEF |
Feb 14 2021, 05:29 PM
Post
#4
|
Programming Fanatic Group: Members Posts: 1,981 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
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 |
Feb 18 2021, 01:27 AM
Post
#5
|
Advanced Member Group: Members Posts: 246 Joined: 30-September 14 Member No.: 21,611 |
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. 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 >. emailtext.php ( 2k ) Number of downloads: 312 |
CharlesEF |
Feb 18 2021, 04:19 PM
Post
#6
|
Programming Fanatic Group: Members Posts: 1,981 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
I've been offline for 72 hours, power failure. I will look at the code later and post back.
|
tudsy |
Feb 19 2021, 03:24 AM
Post
#7
|
Advanced Member Group: Members Posts: 246 Joined: 30-September 14 Member No.: 21,611 |
|
CharlesEF |
Feb 25 2021, 03:50 PM
Post
#8
|
Programming Fanatic Group: Members Posts: 1,981 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
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 |
Feb 27 2021, 05:44 AM
Post
#9
|
Advanced Member Group: Members Posts: 246 Joined: 30-September 14 Member No.: 21,611 |
Hi Thanks for that. Its not urgent. Thanks. 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 |
Mar 1 2021, 05:27 AM
Post
#10
|
Advanced Member Group: Members Posts: 246 Joined: 30-September 14 Member No.: 21,611 |
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. Hi Thanks for that. Its not urgent. Thanks. 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. Comm1.php ( 4.88k ) Number of downloads: 378 |
tudsy |
Mar 2 2021, 08:28 AM
Post
#11
|
Advanced Member Group: Members Posts: 246 Joined: 30-September 14 Member No.: 21,611 |
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. 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. Hi Thanks for that. Its not urgent. Thanks. 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. Comm1.php ( 4.88k ) Number of downloads: 378 Comm1.php ( 4.88k ) Number of downloads: 377 |
tudsy |
May 20 2021, 12:04 AM
Post
#12
|
Advanced Member Group: Members Posts: 246 Joined: 30-September 14 Member No.: 21,611 |
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. 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. 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. Hi Thanks for that. Its not urgent. Thanks. 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. Comm1.php ( 4.88k ) Number of downloads: 378 Comm1.php ( 4.88k ) Number of downloads: 377 |
Lo-Fi Version | Time is now: 24th April 2024 - 05:44 AM |