Проблемы с отправкой почты через PHPMailer - PullRequest
0 голосов
/ 30 марта 2012

Я получаю две ошибки при попытке отправить почту:

  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 голосов
/ 30 марта 2012

Вам нужен настоящий SMTP-сервер для отправки подобных писем.Поскольку у вас явно не установлен 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!
}
...