Full error message: Warning: mail(): SMTP server response: 451 See http://pobox.com/~djb/docs/smtplf.html. in path_to_php_file on line #
This is an error message that I got lots of times and it was always the same problem. So I decided to write here the solution for the next time I encounter it and I thought it might also help other people.
So I tried to send an email in plain text using the PHP function mail() :
// consider this being an existing email address $to_address = "firstname.lastname@example.org"; $subject = "Email subject"; $message = "line1 line2 line3"; $headers = "MIME-Version: 1.0\r\n"; $headers .= "Content-type: text/plain; charset=iso-8859-1\r\n"; $headers .= "To: <".$to_email.">\r\n"; $headers .= "From: Me <".$my_email_address.">\r\n"; mail($to_email, $subject, $message, $headers);
It worked fine with Apache server running on Linux operating system, but I got the error message mentioned before with the server running on Windows.
It looks like while Unix-based systems recognize \n character (the equivalent in PHP of LF – line feed) as a newline even when sending emails, Windows systems are stricter in comunicating using some textual Internet Protocols (such as SMTP) that mandate the line terminator to be the ASCII CR+LF (carriage return + line feed) sequence, which is abstracted in PHP to \r\n character sequence.
So the problem was the $message variable. The correct way to assign the multiple lines value to it is:
$message = "line1\r\nline2\r\nline3"; // separate the lines with \r\n // or, elegantly: $message = "line1\r\n"; $message .= "line2\r\n"; $message .= "line3";
And now it works on Windows, too.