开发者社区 问答 正文

ECS 如何解决windows系统下php.ini邮件配置正确不发送邮件的问题



问题现象


php.ini邮件配置正确但不发送邮件。报错如下 Warning: mail() [function.mail]: SMTP server response: 530 5.7.0 Must issue a STARTTLS command first


问题原因


php mail()函数不可用,需要安装sendmail 。


解决方案


1.从 http://glob.com.au/sendmail/下载sendmail.zip
2.解压到C:下,例如:【C:\php\sendmail】最好短路径,长路径名有可能产生问题。
3.修改【php.ini】如下 sendmail_path = "C:\wamp\sendmail\sendmail.exe -t"
4.根据自己的配置环境修改【sendmail.ini】。
第一次最好启用debug.log_file,error_logfile,以查看sendmail是否生效。
5.重启apache
用sendmail结合其它的smtp服务器,如smtp.163.com来实现发邮件。现在大部分邮箱都要求smtp验证,所以要在sendmail.ini中加入用户名和密码。
php.ini配置 (以用163邮箱为例) [mail function]; For Win32 only.SMTP = smtp.163.comsmtp_port = 25; For Win32 ……
用sendmail结合其它的smtp服务器,如smtp.163.com来实现发邮件。现在大部分邮箱都要求smtp验证,所以要在sendmail.ini中加入用户名和密码。
php.ini配置 (以用163邮箱为例) [mail function]; For Win32 only.SMTP = smtp.163.comsmtp_port = 25; For Win32 only.sendmail_from = 4u4v@163.com; For Unix only. You may supply arguments as well (default: “sendmail -t -i”).sendmail_path = “D:\xampp\sendmail\sendmail.exe -t”
主要配置三项,【smtp】、【smtp_port】和【sendmail_path】
【sendmail.ini】配置 [sendmail]; you must change mail.mydomain.com to your smtp serversmtp_server=smtp.163.comsmtp_port=25auth_username=yourusernameauth_password=yourpasswordforce_sender=yourusername@163.com
以上都是已163为例子的,请更具自己的实际需要修改自己的邮箱配置。


如果问题还未能解决,请联系 售后技术支持

展开
收起
boxti 2017-11-02 16:19:46 2885 分享 版权
阿里云 AI 助理回答

您的解决方案已经很详细了,这里我再补充一些使用阿里云产品进行邮件发送的建议,这或许能为您提供另一种解决思路。

阿里云提供了邮件服务——阿里云邮件推送(DirectMail),它支持API调用和SMTP服务,可以方便地集成到您的PHP应用中。相较于自建邮件发送系统或使用第三方SMTP服务器,使用阿里云DirectMail有以下优势:

  1. 高送达率:阿里云与各大邮件服务商保持良好合作关系,有助于提高邮件送达率。
  2. 免维护:无需关心邮件服务器的搭建、维护及IP信誉问题。
  3. 安全性:提供SSL加密传输,保护数据安全。
  4. 易集成:提供多种语言的SDK,包括PHP SDK,便于快速集成到现有系统中。
  5. 成本可控:按量付费,没有额外的硬件或运维成本。

使用步骤简述:

1. 创建阿里云账号并开通DirectMail服务

如果您还没有阿里云账号,首先需要注册一个,并在控制台开通DirectMail服务。

2. 获取AccessKey ID和AccessKey Secret

这些凭证是调用阿里云API时的身份验证信息,需妥善保管。

3. 创建发信域名

在DirectMail控制台创建一个用于发送邮件的域名,并完成域名解析验证。

4. 配置SMTP发送

  • 在DirectMail控制台获取SMTP服务器地址、端口以及SMTP授权码。
  • 修改您的php.ini配置文件,使用阿里云提供的SMTP服务代替之前的配置:
    [mail function]
    SMTP = smtpdm.aliyun.com
    smtp_port = 465
    sendmail_from = your_email@example.com
    ; 如果需要SSL加密
    smtp_ssl = ssl
    
  • 根据实际情况配置认证信息,可能需要在代码层面设置用户名(邮箱地址)和密码(SMTP授权码)。

5. PHP代码示例

使用PHPMailer库配合阿里云SMTP服务发送邮件,确保您已安装PHPMailer库。

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'path/to/PHPMailer/src/Exception.php';
require 'path/to/PHPMailer/src/PHPMailer.php';
require 'path/to/PHPMailer/src/SMTP.php';

$mail = new PHPMailer(true);

try {
    // 服务器设置
    $mail->SMTPDebug = 0;                      // Enable verbose debug output
    $mail->isSMTP();                                            // Set mailer to use SMTP
    $mail->Host       = 'smtpdm.aliyun.com';                    // Specify main and backup SMTP servers
    $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
    $mail->Username   = 'your_email@example.com';                     // SMTP username
    $mail->Password   = 'your_smtp_authorization_code';                               // SMTP password
    $mail->SMTPSecure = 'ssl';            // Enable TLS encryption, `ssl` also accepted
    $mail->Port       = 465;                                    // TCP port to connect to

    // Recipients
    $mail->setFrom('your_email@example.com', 'Mailer');
    $mail->addAddress('recipient@example.com', 'Recipient');     // Add a recipient

    // Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $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';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>

通过上述方式,您可以利用阿里云的DirectMail服务来替代传统的sendmail或自建SMTP服务器,以更稳定、高效的方式发送邮件。

有帮助
无帮助
AI 助理回答生成答案可能存在不准确,仅供参考
0 条回答
写回答
取消 提交回答