通过PHPMailer发送邮件的问题

时间:2012-03-30 14:50:34

标签: php sendmail phpmailer

尝试发送邮件时出现两个错误:

  Warning: fsockopen() [function.fsockopen]: unable to connect to mail.localhost.com:25 (A
  connection attempt failed because the connected party did not properly respond after 
  a period of time, or established connection failed because connected host has failed 
  to respond. ) in C:\xampp\htdocs\euisample\class.smtp.php on line 122

 SMTP -> ERROR: Failed to connect to server: A connection attempt failed because 
 the  connected party did not properly respond after a period of time, or 
 established connection failed because connected host has failed to respond. 
 (10060)  Mailer
 Error: SMTP Error: Could not connect to SMTP host.

目前我正在使用此测试脚本

require("class.phpmailer.php");
$mail    = new PHPMailer();
$message = "Hello \n
      Thank you for registering with us. Here are your login details...\n
      User ID: $user_name
      Email: $usr_email \n
      Passwd: $data[pwd] \n";
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host      = "mail.localhost.com"; // SMTP server
$mail->SMTPDebug = 2;
$mail->SMTPAuth  = true; // enable SMTP authentication
$mail->Host      = "mail.localhost.com"; // sets the SMTP server
$mail->Port      = 25; // set the SMTP port for the GMAIL server
$mail->Username  = "xxx@localhost.com"; // SMTP account username
$mail->Password  = "xxx"; // SMTP account password
$mail->AddReplyTo("xxx@localhost.com", "First Last");
$mail->Subject = "PHPMailer Test Subject via smtp, basic with authentication";
$mail->MsgHTML($message);
$address = $usr_email;
$mail->AddAddress($address, "John Doe");

我需要系统发送电子邮件。如果无法使用localhost,它是否适用于Gmail?还有其他建议吗?

1 个答案:

答案 0 :(得分:0)

您需要一个真正的SMTP服务器才能发送这样的电子邮件。由于您显然没有在localhost上安装SMTP服务器,因此您唯一的选择是使用第三方服务器。例如,你可以使用GMail,这是如何。

require_once('class.phpmailer.php');

try {
    $mail = new PHPMailer(true);
    $mail->IsSMTP(); // Using SMTP.
    $mail->CharSet = 'utf-8';
    $mail->SMTPDebug = 2; // Enables SMTP debug information - SHOULD NOT be active on production servers!
    $mail->SMTPSecure = 'tls';
    $mail->SMTPAuth = true; // Enables SMTP authentication.
    $mail->Host = "smtp.gmail.com"; // SMTP server host.
    $mail->Port = 587; // Setting the SMTP port for the GMAIL server.
    $mail->Username = "EMAIL/USERNAME"; // SMTP account username (GMail email address).
    $mail->Password = "PASSWORD"; // SMTP account password.
    $mail->AddReplyTo('EMAIL', 'NAME'); // Use this to avoid emails being classified as spam - SHOULD match the GMail email!
    $mail->AddAddress('EMAIL', 'NAME'); // Recipient email / name.
    $mail->SetFrom('EMAIL', 'NAME'); // Sender - SHOULD match the GMail email.
    $mail->Subject = 'PHPMailer Test Subject via smtp, basic with authentication';
    $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';
    $mail->MsgHTML($message);
    $mail->Send();
} catch (phpmailerException $e) {
    echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
    echo $e->getMessage(); //Boring error messages from anything else!
}