php send mail code not working

六月ゝ 毕业季﹏ 提交于 2019-12-10 15:48:56

问题


$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?


回答1:


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.




回答2:


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);



回答3:


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.




回答4:


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'); 



回答5:


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.




回答6:


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:

  • PHPMailer - de facto standard
  • http://swiftmailer.org/ - a more modern approach, yet smaller user base


来源:https://stackoverflow.com/questions/1104555/php-send-mail-code-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!