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:

[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

2
  • You need to post full or more code. Not enough code. Show us what "you" are using. Commented Mar 16, 2016 at 11:39
  • 2
    mail is not very good at all, I would recommend rather than solving this issue instead using PHPMailer: github.com/PHPMailer/PHPMailer Commented Mar 16, 2016 at 11:45

1 Answer 1

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('[email protected]', '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:

Sign up to request clarification or add additional context in comments.

3 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.