简体   繁体   中英

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 you?";

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM