1

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

Community
  • 1
  • 1
Wouter Pol
  • 1,031
  • 1
  • 8
  • 16
  • You need to post full or more code. Not enough code. Show us what "you" are using. – Funk Forty Niner Mar 16 '16 at 11:39
  • 2
    `mail` is not very good at all, I would recommend rather than solving this issue instead using PHPMailer: https://github.com/PHPMailer/PHPMailer – Martin Mar 16 '16 at 11:45

1 Answers1

2

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:

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • Thank you for pointing that out, unfortunately it does not solve my problem, so something else must be wrong. I'm not receiving a mail delivery failure-email either now, so I'll go with PHPmailer (as suggested by Martin) or Swiftmailer. – Wouter Pol Mar 16 '16 at 11:57
  • @WouterPol You're welcome. *Hm....*, that is most bizarre. I would try another test using PHP.net's actual bare example and not modifying it in any way. I'm pretty sure that will work. Yet, PHPMailer/Swiftmailer are good programs to use, for sure. *Cheers* – Funk Forty Niner Mar 16 '16 at 11:59
  • Tried that to make sure my php.ini of the server was in order. The bare example worked like a charm, so it's my error (of course). If I find out what the error was, I'll make sure to update my post. Cheers – Wouter Pol Mar 16 '16 at 12:01