Categories
PHP

Warning: mail(): SMTP server response: 451 See http://pobox.com/~djb/docs/smtplf.html. in path_to_php_file on line #

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 = "abc@def.com";
$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.

89 replies on “Warning: mail(): SMTP server response: 451 See http://pobox.com/~djb/docs/smtplf.html. in path_to_php_file on line #”

I’ve looked at the above scenario in reference to mine – and the fix doesn’t necessarily apply w/my exact scenario. I’m getting the same error – but my code is as follows:

\n”;
$mailheaders .= “Reply-To: $_POST[email]\r\n”;

ini_set(‘sendmail_from’, ‘tashika.hillman@gmail.com’);
ini_set(‘SMTP’, ‘relay-hosting.secureserver.net’);

mail($to, $subject, $msg, $mailheaders);

?>

— this is on a GoDaddy – Windows Server, btw.
Thanks in advance for any help.

I think you have to replace the new lines characters with “\r\n” in the $msg variable.

I need to see more of your code so that i can determine if there is really a problem similar to the one described in this post. I’m interested in where you assign values to the variables passed to the mail function.

Good luck.

just in case someone else is having the same problem as Jai trying to do this on a godaddy windows server. (I just encountered this same problem) In addition to the things mentioned in the posts above godaddy also runs windows server IIS6 and IIS7. IIS6 has limited support for php5 if you upgrade to IIS7 and also do all the things mentioned above it should work fine (at least it did in my situation)

WOW it works great. I put \r in front of all \n and it worked. The book I’m leaning from does not mention this.
Thank You

I have been beating my head against the wall using Windows hosting on GoDaddy with PHP mail(). I have tried every fix I found on the web, including the ones mentioned above. Of course, I’ve not received much support from GoDaddy but learned that that setting SMTP to relay-hosting.secureserver.net in the script seemed to help process the emails. However, now I am getting the 451 Error when trying to insert a hyperlink into the body of the email taking the user back to my web site to log in. Any suggestions on this formatting? I am using text within the body, (not HTML) and I’ve tried backslashes in front of the “”, and without. Not sure what to do? Thanks in advance!
LA Ray

Help now i get
Parse error: syntax error, unexpected ‘:’ in D:\Hosting\4670544\html\sendeail.php on line 46

I also use GoDaddy’s Windows hosting using IIS7.

I prefixed any ‘\n’ newlines character with ‘\r’ so that it reads ‘\r\n’ and everything works great straight away.

Simple when you know how I guess.

@John is the book you are using the Head First PHP & MySQL book by any chance?

Just gave this a shot and it fixed the problem of getting the error immediately.

I’m doing the same GoDaddy thing that was mentioned earlier (IIS7, etc.) The \n was already in the code so I just added the \r and it fixed the error.

Thanks

I also have GoDaddy with IIS7, etc and this fixed my problem with gbcf-v3 contact form. I was getting extremely frustrated until I found this resource.

I simply searched through my code and replaced all \n with \r\n. Is this safe? Everything seems to be working…

$message = “line1\r\nline2\r\nline3”;

dint work for me in godaddy.com

$message = “line1\r\n”;
$message .= “line2\r\n”;
$message .= “line3”;

worked well!!

thanks a lot

Thank you! Tested the code with ‘\n’ on a well-kept server. No problem. But when moved to a godaddy server, got the errors.

$message = “line1\r\n”;
$message .= “line2\r\n”;
$message .= “line3″;

Fixes it perfectly!

@pskavita, actually the two ways of assigning a value to the $message variable are the same. So it cannot happen that one works and the other does not work.
It’s like $a = 5 and $a = 2 + 3, there are two correct ways of asigning the value 5 to the $a variable.

thank you so very much! my form works now.

the only change i made was \n to \r\n throughout the php form.

i’m with godaddy with the server running on a windows system.

Hey All! This really does work. I was getting frustrated until I found this page.Just replace \n to \r\n\

The way above solved the immediate error, but I still don’t receive emails from Godaddy.

Anyone by chance know why this is?
Thanks,

I’m using GoDaddy with Windows.

I spent the past 3 hours trying to figure out what I was doing wrong.

Thank you so much!!!

@CC, I’m using GoDaddy with Windows too…what’s your code look like?

HAHA… this is the greatest feed/post… had this issue in my head for about 3 weeks now for my clients’s website… ended up running his PHP from an iframe directed to my only server… thaks alot!!!!!!!!!!!!!!!!!

hello,

I have put the \r\n\ in. It works sort of but i don’t receive the data that the user has actually typed in. The form only comes up with my headers.

Any help for this??

Theres my code:

<?php

// SET SMTP IN PHP.INI
ini_set ("SMTP", "relay-hosting.secureserver.net");

// CHANGE THE VARIABLES BELOW

$EmailFrom = "”;
$EmailTo = “”;
$Subject = “Contact Form Submission”;

$Name = Trim(stripslashes($_POST[‘Name’]));
$Email = Trim(stripslashes($_POST[‘Email’]));
$URL = Trim(stripslashes($_POST[‘URL’]));
$Message = Trim(stripslashes($_POST[‘Message’]));

// prepare email body text
$Body = “\r\n”;
$Body .= “Name: “;
$Body .= $Name;
$Body .= “\r\n”;
$Body .= “Email: “;
$Body .= $Email;
$Body .= “\r\n”;
$Body .= “URL: “;
$Body .= $URL;
$Body .= “\r\n”;
$Body .= “Message: “;
$Body .= $Message;
$Body .= “\r\n”;

// send email
$success = mail($EmailTo, $Subject, $Body, “From: ” );

// redirect to success page
// CHANGE THE URL BELOW TO YOUR “THANK YOU” PAGE

die();

?>

NOTE: GoDaddy is a bit odd sometimes. I made the changes and reloaded my contact.php form code. I realized it still was not working, I opened the contact.php form code in the file manager and realized that the one I corrected had not been uploaded and the old one was still in place. SO, I edited it in the FILE MANAGER and it worked (I replaced all the \n with \r\n).

Hello all,

I have one problem and maybe anyone can help me with it.
I use godaddy and I am getting all the variables from a flash contact form.
The problem for me is that when using the flash with other hosting it worked just fine, but now with godaddy not working.
I will post the code here and maybe anyone can help me on this.

Thank you in advance!

Hello all,
i had been fighting with this error 451 for weeks and godaddy did not help much until they sent me to this area. i did what you said but now I get error 554.
the chages i did them inside the body but not in my last two lines.
this is my code for last two lines. any solution?

mail($_POST[“name”].” “,$ToSubject, $EmailBody, “From: “.$_POST[“name”].” “);

mail($_POST[“name”].” “,$ToSubject, $EmailBody, “From: “.$_POST[“name”].” “);

?>

thanks for your help

hello all,

i get a new error now that i did the changes and godaddy tells me that i have an invalid IP. i have an FTP where client conects.
any solution?
this is what i get now:
Warning: mail() [function.mail]: SMTP server response: 554 The message was rejected because it contains prohibited virus or spam content in D:\Hosting\5733015\html\mailform\MailPHPPL.php on line 27

Warning: mail() [function.mail]: SMTP server response: 554 The message was rejected because it contains prohibited virus or spam content in D:\Hosting\5733015\html\mailform\MailPHPPL.php on line 29

this is related to”these lines”
mail ($_POST[“nombre_empresa”].” “,$ToSubject, $EmailBody, “From: “.$_POST[“nombre_empresa”].” “);

mail($_POST[“nombre_empresa”].” “,$ToSubject, $EmailBody, “From: “.$_POST[“nombre_empresa”].” “);

?>

thanks for your help

@ivette, $_POST[“nombre_empresa”].” ” – wtf is this?
the first parameter of the mail() PHP function should be an e-mail address, not any string

Hello thalissar, i did change the name to mail address but it is still the same. these are my last two lines that one is to send the form and the other one the person sending it will get a copy. it has worked fine sometimes. my 451 error was allowing sometimes this form to pass through but now i believe i fix with the \r\n\ however it does not go through because it believes that those two lines are spams. godaddy tells me that i need to change to the relay email which i did but it is still the same, so i believe the problem is not that. it may be that something abouth these two lines is contradicting but i have no idea.

1. The two lines you are talking about are identical.
2. Can you send a simple e-mail with PHP, like:

if(mail($MyEmailAddress, “E-mail Subject”, “E-mail body”))
{
echo “coocoo”;
}

or you get that error all the time?

I did as you said but i guess this code is missing something with the echo.

it passes twice, in one i get the info in the second one i get only this
“E-mail Body”. but it is starting to work or to pass through.

what i want is that the client keeps a copy of what they sent.

thanks a lot for your tips

@thalissar. i finally got it to passthrough, i changed the way you sent it to me with the echo. I will still need the copy for the client but it is okay for now.
this is waht i did, otherwise it will not work for me.

if(mail($MyEmailAddress,$ToSubject,$EmailBody));

thanks for your info.

now i do not get any errors.

@ivette, do you understand anything about programming?
1. I don’t care what e-mails you are supposed to send and to whom, I only told you that with those two lines you are sending the same e-mail to the same e-mail address, as the two lines are the same. If you want to send the same e-mail to two different e-mail addresses you should do something like:

mail($EmailAddress, $ToSubject, $EmailBody, “From: “.$_POST[“nombre_empresa”].” “);
mail($DifferentEmailAddress, $ToSubject, $EmailBody, “From: “.$_POST[“nombre_empresa”].” “);

Replace $EmailAddress and $DifferentEmailAddress with your specific data.

2. In the previous post I was just giving an example about knowing if a mail can be sent using the mail() PHP function.
PHP mail() function returns TRUE if it succeeds and FALSE otherwise. So I was only using the if statement to show you how to check if the mail function succeeds: if you see the text echoed, it worked (the function returned TRUE). So you don’t need to replace anything in your code with what I showed you, as it was JUST AN EXAMPLE. You should not copy paste code people are showing you as an example into your project thinking that is the solution to your problem. Make a test script and try there example codes with replacing some variables with your own data.
And the mail() function works the same with the if statement or without it. It is absurd to think that it only works as an if clause condition, meaning that

mail($MyEmailAddress,$ToSubject,$EmailBody);

works exactly the same as

if(mail($MyEmailAddress,$ToSubject,$EmailBody));

I don’t fully understand what you are saying (stuff like “this code is missing something with the echo”, “it passes twice, in one i get the info in the second one i get only this…” – who and what passes? what are “one” and “the second”? what are you referring to as “the info”? etc.), but I guess you succeeded sending an e-mail. So the problem is your coding.

When you reply, please be more specific. Insert code into your comment and, when you do that, discuss upon that piece of code so that people can understand what you are talking about.

does anyone knows how a file can pass when a form is sent in a PHP? mi form passes all information except the file attached in the browse file spot.

thanks

@ivette,
for uploading a file on a server via PHP you should set the enctype attribute of the form to “multipart/form-data” like this:
<form enctype=”multipart/form-data” action=”” method=”post”>
and, of course, you should have an input of type “image”:
<input type=”file” id=”my_file” name=”my_file” />

after a file is selected and the form is submitted, you should be able to access the $_FILES[‘my_file’] variable that holds the info of the uploaded file.

and now skiddoo. sing to another table if you are out of the topic.

@thalissar, i do have my form as yu suggested, however, it does not work

in my form:

for the browse:
Select A File To Upload:

in my PHP:
“\r\nfile uploaded : “.$_POST[“my_file”].

but it does not work. do i have to add somthing else to make it work or it has to do with the server again?
thanks

@ivette, can you read?
as I said, in PHP you can access the info of the uploaded file through the $_FILES superglobal:
echo “file uploaded:<br />\n”;
print_r($_FILES[“my_file”]);

Someone please could help me? I don’t know why this code is no working. Thanks.

$value){
if (!is_array($value)){
$message .= “\n”.$key.” : “.$value;
}
else{
foreach ($_POST[$key] as $itemvalue){
$message .= “\n”.$key.” : “.$itemvalue;
}
}
}
mail($mailto, $subject, stripslashes($message), $header);
?>

Could someone please help me stop getting this 451 error. I know nothing about this stuff, just trying to build a website and came across this error. I see alot of /r/n but don’t know how to do it. can someone modify this to work for me. Thanks in advance!

Your Request Has Been Submitted.

Thank
you!
Your
data has been received and is
being processed.
You will be contacted shortly to

arrange your repair.

@matthew: You can use any simple text editor that has the “find/replace” ability. Do a find on \n, replace with \r\n (make sure that the \n doesn’t already have a \r in front as some of them might already have). As you find the next occurrence in your file (by clicking “find next” or just the “find” button), it should highlight the \n it finds, so it should be fairly easy for you to see if there is a \r in front of it. If it does have a /r, you should have a “skip” or “next” button to pass over that occurrence to leave it alone.

Thanks admin, I had the same problem with goddady, I just replaced the /n with /r/n and now Im a happy man

Hey guys, all my code has the \r\n in there and I am still getting the same error, can anyone help me out? here is the code I’m using, Thanks

<?php

/* Subject and Email Variables */

$emailSubject = 'Crazy PHP Scripting!';
$webMaster = 'abarrie@articulatepromotions.com';

/* Gathering Data Variables */

$emailField = $_POST['email'];
$nameField = $_POST['name'];
$messageField = $_POST['message'];

$body = <<<EOD

Email: $email
Name: $name
Message: $message
EOD;

$headers = “From: $email\r\n”;
$headers .= “Content-type: text/html\r\n”;
$success = mail($webMaster, $emailSubject, $body, $headers);

/* Results Rendered as HTML */

$theResults = <<<EOD

JakesWorks – travel made easy-Homepage

Thank you for your interest! Your email will be answered very soon!

EOD;
echo “$theResults”;

?>

Comments are closed.