views:

96

answers:

1

I have a small php mailer script within a php file that works fine and sends mail fine:

$subject = "subject"; 
$mail_body = "mail body";
$name = "noreply";
$email = "[email protected]";
$recipient = "[email protected]";
$header = "From: ". $name . " <" . $email . ">\r\n";
mail($recipient, $subject, $mail_body, $header);

However, if I take this out and make it its own file, or put it into a different file, it doesn't work. There are no error messages and email is not sent.

There are no php ini set commands and no included php files.

Any ideas on why it works in the larger php script, but doesn't work on its own?

+1  A: 

Everything is OK with your code. I tested it by only replacing the email address in $recipient with my address and it worked fine (I tested it with my corporate email - Outlook on Exchange server and another test with an Hotmail address - both worked fine and reached my inbox).

  • But make sure that new lines in $mail_body are "\n" and not "\r\n". As you can read in the docs about the message parameter:

Each line should be separated with a LF (\n). Lines should not be larger than 70 characters.

  • Make sure $name and $email does not contain forbidden characters like <, >, and new lines (\r, \n, or \r\n). As Michael points out new lines in there can lead to email injection attacks. You don't want your contact form to be used as a mail platform by spammers!

You should consider using an email library like PHPMailer which will ease your development since mail() is quite tricky for advanced mailing. Most of these libraries already cover these attacks and standards...

AlexV
I know everything is ok with the code, but why doesn't it work on its own? It only works in the original script I wrote a while ago. If I just wrap the above text in <?php ?> and save as new file, the mail doesn't send.
CheeseConQueso
Are the addresses in $recipient are the same in your 2 versions? If not maybe it's the server-side spam filter on one side that is intercepting the mail... You can try to add the Reply-To, Return-Path, Message-ID and X-Mailer headers to see if it helps (see php.net examples).
AlexV
I tried to put the same working script into its own php file and it wouldn't send...
CheeseConQueso
And they are both executed the same Server?
AlexV
yeah... the only difference between the two files at this point is that one has some actual functionality in terms of being a usable, live, script. the other one is exactly the part i have above, but just in its own php wrapper and in its own file
CheeseConQueso
AlexV
yes - like I said, I have a working version exactly like this that emails me after certain tasks. the only thing I swapped out was all the vars int he message and subject and also the domain names
CheeseConQueso
I'm out of ideas... Your code is fine (tested it on 2 different email accounts) and both messages reached my inbox. It's probably something else that you missed somewhere. If you find out let us know it will probably usefull for someone there! If I find something else I'll update my answer/comments. Good luck!
AlexV
Yeah i surely will... I have no clue what the problem is at all and its really a time waster for today. why are all your comments flagged?
CheeseConQueso
Flagged? I don't see them flagged...
AlexV
maybe because you are the poster... they have that bold, italicized ! after your post dates
CheeseConQueso
It's not a bold italic "i", It's a pen... It's because I edited my comments :) I often see spelling errors after I post a comment.
AlexV
i just realized that and was coming back to say "nevermind" haha
CheeseConQueso