Jump to content

Php Mail Not Received


maicol07

Recommended Posts

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
Link to comment
Share on other sites

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?
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

 

 

<?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...
Link to comment
Share on other sites

 

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

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...