4
$to = "[email protected]";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
if (mail($to, $subject, $body)) {
  echo("pMessage successfully sent!/p");
} else {
  echo("pMessage delivery failed.../p");
}

Wrote a basic php sendmail code that but it gives me the following error:

Warning: mail() [function.mail]: "sendmail_from" not set in php.ini or custom "From:" header missing in C:\xampp\htdocs\mail.php on line 5 Message delivery failed...

I changed the ``php.ini file and put [email protected] but still the problem persists. Writing the mail script for the first time.

Am I doing something wrong? Is there a better way to do this?

1
  • 2
    I think it's worth mentioning that putting your real email address in plaintext on the internet is just asking to get it harvested by spambots. I hope you obfuscated it just a little and that I'm just not seeing it because I don't know your real email address. :D Commented Jul 9, 2009 at 17:21

6 Answers 6

2

additional_headers (optional)

String to be inserted at the end of the email header.

This is typically used to add extra headers (From, Cc, and Bcc). Multiple extra headers should be separated with a CRLF (\r\n).

Note: When sending mail, the mail must contain a From header. This can be set with the additional_headers parameter, or a default can be set in php.ini. Failing to do this will result in an error message similar to Warning: mail(): "sendmail_from" not set in php.ini or custom "From:" header missing. The From header sets also Return-Path under Windows.

I hope that helps.

1

First all, check you edited the correct php.ini - add phpinfo(); to your script output diagnostic information, including the location of php.ini. You should also be able to see the configured "sendmail_from" value here too.

Failing that, provide a From header, as indicated by usoban

$hdrs="From: [email protected]";
mail($to, $subject, $body, $hdrs);
1
  • i have edited the right php.ini file but still there are errors
    – anand
    Commented Jul 9, 2009 at 15:31
1

If you edited the correct php.ini and it does not reflect your change, you might want to restart your web service, as many environments will only load the php.ini when the server starts.

1

I ran into the same problem and it ended up being because my server was using the account@servername as the from email address even though I specified my own in the headers.

The answer I found somewhere else was to add a 5th parameter to the mail call to force the system to use the From Email Address that I specified :

$from = '[email protected]'
$xheaders = "From: " . $from . " <" . $from . ">\n"; 
$i = mail("$useremail","$subject","$content",$xheaders,'-f [email protected]'); 
0

Rather than setting the form address in php.ini, just send it in the headers like usoban said instead.

This will save you headaches when you host another site on the same setup and forget to set the headers next time.

0

PHP mail() function generates a lot of problems. When you finally get it working on your server, you will notice that your emails end up in spam folders of your users.

I would recommend to send mail using one of those PHP SMTP libraries instead:

Not the answer you're looking for? Browse other questions tagged or ask your own question.