maicol07 Posted September 18, 2017 Posted September 18, 2017 (edited) Hi,I wrote a PHP script that sends me an email but I can't receive it. Why?ThanksPHP script: (he gives me error that Mail class doesn't exists) <?php /** * Created by PhpStorm. * User: Maicol * Date: 17/09/2017 * Time: 20:49 */ $post_name=$_POST["name"]; $post_email=$_POST["email"]; $post_message=$_POST["message"]; if ( $post_name ) { $to = "maicolbattistini@live.it"; $lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2); $subject = "Request from 'About Me' Page"; $message = " <html> <head> <title>Request from 'About Me' page</title> </head> <body> <p>You have received a request from your personal webpage:</p> <table> <tr> <th>Name</th> <th>Email</th> <th>Message</th> </tr> <tr> <td>".$post_name."</td> <td>".$post_email."</td> <td>".$post_message."</td> </tr> </table> </body> </html> "; // Always set content-type when sending HTML email $headers = "MIME-Version: 1.0" . "\r\n"; $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n"; // More headers $headers .= 'From: <noreply@maicol07.tk>' . "\r\n"; $headers .= 'Cc: noreply@maicol07.tk\r\n'; $mail = new Mail(); $mail->setFrom(SITEEMAIL); $mail->addAddress($to); $mail->subject($subject); $mail->body($body); if ($mail->send()) { echo "<script> alert('Email successfully sent!') </script>"; } else { echo "<script> alert('Email not sent! Please contact the administrator')</script>"; } echo "<script> location.href = 'index.php'; </script>"; ?> With PHP mail() function: <?php /** * Created by PhpStorm. * User: Maicol * Date: 17/09/2017 * Time: 20:49 */ $post_name=$_POST["name"]; $post_email=$_POST["email"]; $post_message=$_POST["message"]; if ( $post_name ) { $to = "maicolbattistini@live.it"; $lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2); $subject = "Request from 'About Me' Page"; $message = " <html> <head> <title>Request from 'About Me' page</title> </head> <body> <p>You have received a request from your personal webpage:</p> <table> <tr> <th>Name</th> <th>Email</th> <th>Message</th> </tr> <tr> <td>".$post_name."</td> <td>".$post_email."</td> <td>".$post_message."</td> </tr> </table> </body> </html> "; // Always set content-type when sending HTML email $headers = "MIME-Version: 1.0" . "\r\n"; $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n"; // More headers $headers .= 'From: <noreply@maicol07.tk>' . "\r\n"; $headers .= 'Cc: noreply@maicol07.tk\r\n'; if (mail($to, $subject, $message, $headers)) { echo "<script> alert('Email successfully sent!') </script>"; } else { echo "<script> alert('Email not sent! Please contact the administrator')</script>"; } echo "<script> location.href = 'index.php'; </script>"; ?> Edited September 18, 2017 by maicol07
Krydos Posted September 18, 2017 Posted September 18, 2017 I just tested mail() on Tommy using<?php $to = "me@example.com"; $subject = "Does php mail() work?"; $message = "Hello world!"; $headers = "From: no-reply@krydos@heliohost.org\r\n"; mail($to, $subject, $message, $headers); I received it on my gmail account and my hotmail account. What version of php are you using?
maicol07 Posted September 19, 2017 Author Posted September 19, 2017 I just tested mail() on Tommy using <?php $to = "me@example.com"; $subject = "Does php mail() work?"; $message = "Hello world!"; $headers = "From: no-reply@krydos@heliohost.org\r\n"; mail($to, $subject, $message, $headers); I received it on my gmail account and my hotmail account. What version of php are you using?PHP 5.6. Should I upgrade?
sagnik Posted September 19, 2017 Posted September 19, 2017 Actually, I think php mail() works well with php 5.6. Anyway, you have added if...else statement for validation, do you able to see any output like you specified in the "if" block or "else" block? Like, "Email sent successfully" or "Email not sent".
sagnik Posted September 19, 2017 Posted September 19, 2017 Give me some time to check the code... I've to execute the code to debug.
maicol07 Posted September 19, 2017 Author Posted September 19, 2017 I found that the email arrives but extremely late...
sagnik Posted September 19, 2017 Posted September 19, 2017 Here is the code (The problem was the new line string): With Mail() class: <?php /** * Created by PhpStorm. * User: Maicol * Date: 17/09/2017 * Time: 20:49 */ /*$post_name=$_POST["name"]; $post_email=$_POST["email"]; $post_message=$_POST["message"]; if($post_name){ $to = "maicolbattistini@live.it"; $lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2); $subject = "Request from 'About Me' Page"; $message = " <html> <head> <title>Request from 'About Me' page</title> </head> <body> <p>You have received a request from your personal webpage:</p> <table> <tr> <th>Name</th> <th>Email</th> <th>Message</th> </tr> <tr> <td>".$post_name."</td> <td>".$post_email."</td> <td>".$post_message."</td> </tr> </table> </body> </html> "; // Always set content-type when sending HTML email $headers = "MIME-Version: 1.0" . "\r\n"; $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n"; // More headers $headers .= 'From: <noreply@maicol07.tk>'.'\r\n'; $headers .= 'Cc: noreply@maicol07.tk'; $mail = new Mail(); $mail->setFrom(SITEEMAIL); $mail->addAddress($to); $mail->subject($subject); $mail->body($body); if ($mail->send()) { echo "<script> alert('Email successfully sent!') </script>"; } else { echo "<script> alert('Email not sent! Please contact the administrator')</script>"; } //echo "<script> location.href = 'index.php'; </script>"; } else { echo "FAIL-1"; }*/ ?> With PHP mail() function: <?php /** * Created by PhpStorm. * User: Maicol * Date: 17/09/2017 * Time: 20:49 */ $post_name=$_POST["name"]; $post_email=$_POST["email"]; $post_message=$_POST["message"]; if($post_name){ $to = "sagnikganguly@whatsnew.in"; $lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2); //$lang="EN"; $subject = "Request from 'About Me' Page"; $message = " <html> <head> <title>Request from 'About Me' page</title> </head> <body> <p>You have received a request from your personal webpage:</p> <table> <tr> <th>Name</th> <th>Email</th> <th>Message</th> </tr> <tr> <td>".$post_name."</td> <td>".$post_email."</td> <td>".$post_message."</td> </tr> </table> </body> </html> "; // Always set content-type when sending HTML email $headers = "MIME-Version: 1.0" . "\r\n"; $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n"; // More headers $headers .= 'From: <noreply@maicol07.tk>'."\r\n"; $headers .= 'Cc: noreply@maicol07.tk'; //<--HERE WAS THE PROBLEM if(mail($to, $subject, $message, $headers)) { echo "<script> alert('Email successfully sent!') </script>"; } else { echo "<script> alert('Email not sent! Please contact the administrator'); </script>"; } echo "<script> location.href = 'index.php'; </script>"; } else { echo "FAIL-2"; } ?> Here is the received email screenshot:
maicol07 Posted September 20, 2017 Author Posted September 20, 2017 So the only thing to change is to indent the html code in the string message and in the final echo?
sagnik Posted September 20, 2017 Posted September 20, 2017 No, actually the problem were in the "\r\n" of cc header. I've marked the line. Remember, when setting the mail headers, the last added header can't contain a line break string, in your case, it was "\r\n".
jotace Posted September 22, 2017 Posted September 22, 2017 <?php $headers .= 'Cc: noreply@maicol07.tk\r\n'; //<-- his code $headers .= 'Cc: noreply@maicol07.tk' . "\r\n"; //<-- correct way??? ?> Are you sure the last added header can't contain a line break string?? Please take a look at example #3 : https://www.w3schools.com/php/func_mail_mail.asp .
maicol07 Posted September 22, 2017 Author Posted September 22, 2017 <?php $headers .= 'Cc: noreply@maicol07.tk\r\n'; //<-- his code $headers .= 'Cc: noreply@maicol07.tk' . "\r\n"; //<-- correct way??? ?> Are you sure the last added header can't contain a line break string?? Please take a look at example #3 : https://www.w3schools.com/php/func_mail_mail.asp . I think it is a bug about the PHP mail function... But I'm not sure... What I'm sure is that it works without it and the email arrives istantly, without imperfections. With /r/n added to the cc header the emails arrives lot of hour later...
sagnik Posted September 23, 2017 Posted September 23, 2017 <?php $headers .= 'Cc: noreply@maicol07.tk\r\n'; //<-- his code $headers .= 'Cc: noreply@maicol07.tk' . "\r\n"; //<-- correct way??? ?> Are you sure the last added header can't contain a line break string?? Please take a look at example #3 : https://www.w3schools.com/php/func_mail_mail.asp . @jotaceYes, the last line also contain a line-break like you did but he had merged the line-break with the header inside the quotation mark so it didn't worked. As it was the last line of his mail headers block, I've removed the line-break. And may beis another reason, the emails he used in the 'from' & 'cc' header it has not configured correctly so the email couldn't send.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now