通过PHP中的localhost发送电子邮件

时间:2012-07-02 19:59:56

标签: php email smtp

我正在尝试通过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中通过localhost发送电子邮件还有哪些其他设置?

1 个答案:

答案 0 :(得分:1)

改为使用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();