简体   繁体   中英

PHP mail() message handling failed

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:

wouter0.7@live.nl
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: wouter0.7@live.nl

(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: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\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 your@email.here");

as suggested here EDIT 2: Code that I use for the mail() function:

//Send Mail

    $to = "wouter0.7@live.nl"; // 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 wouter0.7@live.nl");

the $return array is used to encode to JSON, it just contains string-values

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('caffeinated@example.com', 'My Subject', $message); and a few other examples, but not for the \\r\\n 's.

  • Read the manual again. All \\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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM