简体   繁体   中英

Debian can't send mail from PHP

Mailer Error: Could not execute: /usr/sbin/sendmail

I'am using debian server, the file permission is 777(all alowed), so I can't execute it why is that?

//Create a new PHPMailer instance
$mail = new PHPMailer();
// Set PHPMailer to use the sendmail transport
$mail->isSendmail();
//Set who the message is to be sent from
$mail->setFrom('admin@test.com', 'test');
//Set an alternative reply-to address
//$mail->addReplyTo('replyto@example.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress($_POST['email'], $_POST['name']);
//Set the subject line
$mail->Subject = 'PHPMailer sendmail test';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML("from test");
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
//$mail->addAttachment('images/phpmailer_mini.gif');

//send the message, check for errors
if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
}

In Ubuntu sendmail is not installed by default. You will have to install it manually:

sudo apt-get install sendmail-bin

EDIT 1:

In case you're using PHPMailer you can set Sendmail path using:

$mail->Sendmail     = '/usr/sbin/sendmail';

It's easy to test if the problem in PHP code or in your mail server configuration, or even probably firewall. Try running from the command line and see if you receive your email:

/usr/sbin/sendmail -v my@address.com < email.test

Additionally you actually could receive the mail but it could be put in SPAM folder, so check for the message there as well.

EDIT 2:

And one more thing is that you should install sendmailconfig and then run it to configure it:

sudo sendmailconfig

Read more about configuring sendmail on Ubuntu: sendmail: how to configure sendmail on ubuntu?

The immediate problem appears to be that you did not have /usr/sbin/sendmail installed on your system. There are multiple MTAs which provide this, so there is no particular need to install the Sendmail suite; in fact, I would recommend against it, in favor of Postfix or some really simple MTA such as smtpd . Any package which Provides: sendmail should do.

The other problem which needs to be pointed out is the chmod 777 permission. You should absolutely not make anything on a production system world-writable. The correct permission for a PHP script is 755 or possibly 775 if you can trust the group. The httpd process certainly does not need to be able to write to the script -- indeed, should absolutely not be allowed to write anything to the script file.

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