Reputation: 1351
I've been trying to get the mail() function of PHP to work by posting JSON to a response.php which handles the actual sending. After numerous attempts I still fail to get the message to the receiver. The problem I'm getting now is that the message needs to be RFC 5322 compliant:
[email protected]
host mx2.hotmail.com [65.54.188.72]
SMTP error from remote mail server after end of data:
550 5.7.0 (BAY004-MC1F14) Message could not be delivered. Please ensure the message is RFC 5322 compliant.
Message output of the script is:
asdada\r\n\nName: Www\r\nEmail: [email protected]
(Keep in mind that I just hit a few buttons after a thousand times of trying, so the message is not making sense.) I've used the example on PHP Manual:MAIL() to create the headers, so they look like this:
$headers = 'From: [email protected]' . "\r\n" .
'Reply-To: [email protected]' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
When I look in the message I got from the mail delivery system, the headers seem to order. The message, however, seems to get messed up in the process, causing the mail delivery to fail.
Does anyone know why this could occur? Please let me know if I need to upload more code/examples.
EDIT: Forgot to mention that I already tried adding a fifth parameter to the mail() function:
mail($to,$subject,$message,$headers,"-f [email protected]");
as suggested here EDIT 2: Code that I use for the mail() function:
//Send Mail
$to = "[email protected]"; // Your email here
$path = $_SERVER['HTTP_HOST'];
$subject = 'Message from ' . $path; // Subject message here
$email = $return["email"];
//Set headers
$headers = 'From: ' . $email .''. '\r\n'.
'Reply-To: '.$email.'' . '\r\n' .
'X-Mailer: PHP/' . phpversion();
$message = $return['msg'] . '\r\n\n' .'Name: '.$return['name']. '\r\n'
.'Email: '.$return['email'];
mail($to, $subject, $message, $headers,"-f [email protected]");
the $return array is used to encode to JSON, it just contains string-values
Upvotes: 1
Views: 161
Reputation: 74217
Notice the single quotes for all your '\r\n'
's? and '\r\n\n'
Those are not being interpreted/parsed correctly and must be wrapped in double quotes "\r\n"
and "\r\n\n"
.
Consult the manual for mail()
:
Nowhere does the manual suggest using single quotes for \r\n
but only for mail('[email protected]', 'My Subject', $message);
and a few other examples, but not for the \r\n
's.
\r\n
's have double quotes around them, not single quotes.As suggested by Martin in comments, PHPMailer is a good program to use, as is Swiftmailer.
References:
Upvotes: 2