Jump to content

Recommended Posts

Posted (edited)

Hi,

I wrote a PHP script that sends me an email but I can't receive it. Why?

Thanks

PHP 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 by maicol07
Posted

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?
Posted

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?

Posted

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".

Posted

Give me some time to check the code... I've to execute the code to debug.

Posted

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:

Yr3kbom.png

Posted

So the only thing to change is to indent the html code in the string message and in the final echo?

Posted

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".

Posted

Your welcome!

Posted

 

 

<?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...
Posted

 

<?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 .

 

 

@jotace

Yes, 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.

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...