无法发送任何电子邮件

时间:2015-02-18 15:53:20

标签: php sendmail

我一直收到错误

warning: mail() [<a href='function.mail'>function.mail</a>]: Failed to connect to mailserver at &quot;localhost&quot; port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() 

在我的PHP代码中

<?php
// the message
$msg = "Testing";

// use wordwrap() if lines are longer than 70 characters
$msg = wordwrap($msg,70);

// send email
mail("someone@example.com","My subject",$msg);
?>

的php.ini

[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = localhost
; http://php.net/smtp-port
smtp_port = 26
; For Win32 only.
; http://php.net/sendmail-from
sendmail_from = you@yourdomain

; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
;sendmail_path = ""

1 个答案:

答案 0 :(得分:0)

您需要运行SMTP服务器,该服务器接受您的电子邮件,这些电子邮件通过PHP发送。您需要在php.ini中指出它。

您的配置指向localhost。我想,localhost上没有运行电子邮件服务器?如果是这样,请仔细检查,如果它接受来自localhost(PHP)的电子邮件。

以下是设置服务器的方法:

对于IIS:

http://geekswithblogs.net/tkokke/archive/2009/05/31/sending-email-from-php-on-windows-using-iis.aspx

请注意,它是在IIS6控制台上配置的,如果您有最新版本,也是如此。由于某些原因,它无法使用>=IIS7控制台进行配置,但仍可以使用旧的控制台进行配置。

对于Linux:

How to send email from localhost using PHP on Linux

通过SMTP登录到邮件帐户

还有一个名为PHPMailer的好的电子邮件类,如果我们在PHP端直接使用SMTP,它就派上用场了:

$mail = new PHPMailer;
$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication

$mail->Username = 'user@example.com';                 // SMTP username
$mail->Password = 'secret';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

$mail->From = 'from@example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
$mail->addAddress('ellen@example.com');               // Name is optional

$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}