Question

$to = "jijodasgupta@gmail.com";
$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 sendmail_from=jijodasgupta@gmail.com 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?

Was it helpful?

Solution

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.

OTHER TIPS

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: jijodasgupta@gmail.com";
mail($to, $subject, $body, $hdrs);

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.

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 = 'fromemail@domain.com'
$xheaders = "From: " . $from . " <" . $from . ">\n"; 
$i = mail("$useremail","$subject","$content",$xheaders,'-f fromemail@domain.com'); 

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.

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:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top