开发者

Why can’t I send email via PHP?

I wrote this code for sending email using PHP and upload it to a server, but it doesn't work:

<?php
$to = "recipient@example.com";
$subject = "Hi!";
$body = "Hi,\n\nHow are yo开发者_StackOverflow中文版u?";

if (mail($to, $subject, $body)) {
  echo("<p>Message successfully sent!</p>");
} else {
  echo("<p>Message delivery failed...</p>");
}
?>

It says "Message delivery failed..." every time!

Can anyone help?


I tried running your code on local server and this is the error i got:

"sendmail_from" not set in php.ini or custom "From:" header missing 

You should probably set "From" header and it should work just fine

...
mail ($to,$subject,$body,'From: sender@example.com');


Antispam softwares are very picky, if you send emails that way you have few chanches to get them in your recipients' inbox. If you want more control on your messages, consider sending emails with a mailer library. http://swiftmailer.org/ is a really good choice. This is not a complete solution to spam problems, but it helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜