php - 无法发送任何电子邮件

标签 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 = ""

最佳答案

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

您的配置指向本地主机。我想,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';
}

关于php - 无法发送任何电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28587862/

相关文章:

javascript - 通过ajax获取单选按钮值到php文件

php - 两个日期之间的分钟数

linux - sendmail 从不发送邮件。总是推迟

php - 命令不同步;在 Mysql 中调用存储过程时,您现在无法运行此命令

php - 在sql语句中包含变量的正确方法是什么

javascript - 使用 PHP 或 JS 解码 Youtube 密码签名的最佳方法

jQuery onClick 打印屏幕并发送图像电子邮件

excel - 如何将excel范围作为图片添加到outlook邮件正文

c# - 使用带有 Mandrill 的 Smtp 客户端发送电子邮件

linux - 使用 perl 在 Linux 上设置 SMTP 传出邮件