2

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?

8
  • What error message are you getting, if any? Commented Feb 23, 2010 at 19:57
  • Can you provide details on how it doesn't work? Are there any error messages? If not, can you confirm that you've configured php.ini to output errors? Commented Feb 23, 2010 at 19:58
  • When you say it doesn't work, do you mean it does not send mail or you receive an error? Commented Feb 23, 2010 at 19:58
  • It seems like its taking way too long to load... that's the only hinting of an error I get. There are no error messages and an email is not sent. @sidereal - I can't confirm that, but can check on it with our network admin. Commented Feb 23, 2010 at 20:30
  • Are both scripts (working and nonworking) being executed from the same environment (commandline or webserver)? Are they being executed as the same user? Commented Feb 23, 2010 at 20:42

1 Answer 1

1

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

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

12 Comments

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.
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).
I tried to put the same working script into its own php file and it wouldn't send...
And they are both executed the same Server?
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
|

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.