在 PHP 中通过本地主机发送电子邮件


Sending an email via localhost in PHP

我正在尝试通过PHP发送电子邮件。它给出以下警告。

Warning: mail() [function.mail]: Failed to connect to mailserver at 
"smtp.ntlworld.com" port 25, verify your "SMTP" and "smtp_port" setting in 
php.ini or use ini_set() in C:'wamp'www'wagafashion'customerside'BulkInquiry.php 
on line 1007

在 php.ini 中,SMTP 已更改如下。

[mail function]
; For Win32 only.
SMTP = smtp.ntlworld.com
smtp_port = 25
; For Win32 only.
sendmail_from = tiny1999@gmail.com

配置 php.ini 后,WAMP 重新启动并给出了上述警告。在 PHP 中通过本地主机发送电子邮件还需要进行哪些其他设置?

改用 PHPMailer: https://github.com/PHPMailer/PHPMailer

如何使用它:

require('./PHPMailer/class.phpmailer.php');
$mail=new PHPMailer();
$mail->CharSet = 'UTF-8';
$body = 'This is the message';
$mail->IsSMTP();
$mail->Host       = 'smtp.gmail.com';
$mail->SMTPSecure = 'tls';
$mail->Port       = 587;
$mail->SMTPDebug  = 1;
$mail->SMTPAuth   = true;
$mail->Username   = 'me.sender@gmail.com';
$mail->Password   = '123!@#';
$mail->SetFrom('me.sender@gmail.com', $name);
$mail->AddReplyTo('no-reply@mycomp.com','no-reply');
$mail->Subject    = 'subject';
$mail->MsgHTML($body);
$mail->AddAddress('abc1@gmail.com', 'title1');
$mail->AddAddress('abc2@gmail.com', 'title2'); /* ... */
$mail->AddAttachment($fileName);
$mail->send();