【问题标题】:SMTP ERROR: Failed to connect to server: With PHPMAILER in DreamhostSMTP 错误:无法连接到服务器:在 Dreamhost 中使用 PHPMAILER
【发布时间】:2016-03-18 01:14:42
【问题描述】:
I can not send mail using the PHPMailer lib. My site is hosted on dreamhost, 

使用 SMTP gmail,我可以更正确地发送到主机提供的配置,唯一的回报是: SMTP 错误:无法连接到服务器:php_network_getaddresses:getaddrinfo 失败:名称或服务未知(0) SMTP 连接()失败。 我的代码:

<?php

date_default_timezone_set('Etc/UTC');

require 'vendor/phpmailer/phpmailer/PHPMailerAutoload.php';

$mail = new PHPMailer;

$name       = trim(stripslashes($_POST['name']));   
$from       = trim(stripslashes($_POST['email']));
$subject    = trim(stripslashes($_POST['subject']));
$message    = trim(stripslashes($_POST['message']));

$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Host = 'mail.example.com.br';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = 'e-mail@example.com';
$mail->Password = 'password';
$mail->SMTPSecure = 'tls';
$mail->Debugoutput = 'html';

$mail->isHTML(true);
$mail->setFrom($from, $name);
$mail->addReplyTo($from, $name);
$mail->addAddress('email@example.com');
$mail->Subject = $subject;
$mail->Body = $message;

 if(!$mail->send()) {
   echo 'Message could not be sent.';
   echo 'Mailer Error: ' . $mail->ErrorInfo;  
  } else {
   echo 'Message has been sent';
}

`

【问题讨论】:

    标签: phpmailer sendmail dreamhost


    【解决方案1】:

    我设法解决了这个问题,先把发给自己客户端的邮件替换为sender@example.com,然后尝试了dreamhost的一些文档,做了必要的修改,代码:

      date_default_timezone_set('Etc/UTC');
    
      require 'vendor/phpmailer/phpmailer/PHPMailerAutoload.php';
    
      $subject    = trim(stripslashes($_POST['subject']));
      $name       = trim(stripslashes($_POST['name']));
      $from       = trim(stripslashes($_POST['email']));
      $body       = trim(stripslashes($_POST['message']));
    
      $mail = new PHPMailer;
    
      $mail->isSMTP();
      $mail->CharSet = 'UTF-8';
      $mail->SMTPDebug = 1;
      $mail->Host = 'mail.example.com.br';
      $mail->Port = 587;
      $mail->SMTPAuth = true;
      $mail->Username = 'sender@example.com.br';
      $mail->Password = 'password';
      $mail->Debugoutput = 'html';
    
      $mail->isHTML(true);
      $mail->setFrom('sender@dexample.com.br', 'DontAnswer');
      $mail->addReplyTo($from, $name);
      $mail->addAddress('contact@example.com.br');
      $mail->Subject = $subject;
      $mail->Body = $body;
    
      if(!$mail->send()) {
          echo 'Message could not be sent.';
          echo 'Mailer Error: ' . $mail->ErrorInfo;
      } else {
          echo 'Message has been sent';
      }
    

    在dreamhost 文档中说应该在setFrom() 方法中重复发送电子邮件;经过这些更改后,表单完美运行。

    【讨论】:

      猜你喜欢
      • 2016-07-20
      • 2018-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-10
      • 2011-09-04
      • 1970-01-01
      • 2016-03-14
      相关资源
      最近更新 更多