简体   繁体   English

PHPMailer无法正常工作:无法发送消息

[英]PHPMailer not working: Message could not be sent

I am trying to create a contact form on my website using PHPMailer. 我正在尝试使用PHPMailer在我的网站上创建联系表单。 I am having some trouble setting it up. 我在设置时遇到了一些麻烦。 I am trying to use G-mail as my smtp host. 我正在尝试使用G-mail作为我的smtp主机。 I was wondering if anyone can help troubleshoot this? 我想知道是否有人可以帮助解决这个问题?

This is my mailer code: 这是我的邮件代码:

<?php
require("class.phpmailer.php");
require("class.smtp.php");

$mail = new PHPMailer();

$mail->IsSMTP();   
$mail->SMTPAuth = true;     // turn on SMTP authentication      
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail        
$mail->Host = 'smtp.gmail.com';
$mail->Port = 467;  

$mail->Username = "validmail@gmail.com";  // SMTP username
$mail->Password = "workingpassword"; // SMTP password

$mail->From = "validmail@gmail.com";
$mail->FromName = "Mailer";
$mail->AddAddress("josh@example.net", "Josh Adams");
$mail->AddAddress("ellen@example.com");                  // name is optional
$mail->AddReplyTo("info@example.com", "Information");

$mail->WordWrap = 50;                                 // set word wrap to 50 characters


// $mail->AddAttachment("/var/tmp/file.tar.gz");         // add attachments
   // $mail->AddAttachment("/tmp/image.jpg", "new.jpg");    // optional name
    $mail->IsHTML(true);                                  // set email format to HTML

$mail->Subject = "Here is the subject";
$mail->Body    = "This is the HTML message body <b>in bold!</b>";
$mail->AltBody = "This is the body in plain text for non-HTML mail clients";

if(!$mail->Send())
{
   echo "Message could not be sent. <p>";
   echo "Mailer Error: " . $mail->ErrorInfo;
   exit;
}

echo "Message has been sent";
?>

The error message: 错误消息:

Message could not be sent.
Mailer Error: The following From address failed: validmail@gmail.com

Have you looked at and tried the info from this Q? 你有没看过并试过这个Q的信息?

PHPMailer: SMTP Error: Could not connect to SMTP host PHPMailer:SMTP错误:无法连接到SMTP主机

In particular, does this provide any additional info? 特别是,这是否提供任何其他信息?

$mail->SMTPDebug = 1;

smtp.gmail.com requires that you use SSL and port 587 or 465. smtp.gmail.com要求您使用SSL和端口587或465。

See their configuration page: http://support.google.com/mail/bin/answer.py?hl=en&answer=13287 请参阅其配置页: http//support.google.com/mail/bin/answer.py?hl = zh-CN&answer = 13287

Are you running PHP on Windows? 你在Windows上运行PHP吗? Then this might help: 那么这可能有所帮助:

http://www.devcha.com/2010/01/php-fsockopen-unable-to-connect-ssl.html http://www.devcha.com/2010/01/php-fsockopen-unable-to-connect-ssl.html

Got the same error and the problem was that i was trying to sent email from "boy333@in**" account pretending as "girl333@in**". 得到了同样的错误,问题是我试图从“boy333 @ in **”帐户发送电子邮件假装为“girl333 @ in **”。 I just changed 我改变了

$mail->From = 'girl333@in**'

to username i was actually connecting. 用户名我实际连接。 So I changed to: 所以我改为:

$mail->From = 'boy333@in**'

The idea is that these two fields is with the same usernames. 这个想法是这两个字段具有相同的用户名。

$mail->Username   = "boy333";
$mail->From = 'boy333@in**';
<?php

require_once('class.phpmailer.php');
include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded


    $nameField = $_POST['name'];
    $emailField = $_POST['email'];
    $messageField = $_POST['message'];
    $phoneField = $_POST['contactno'];
    $cityField = $_POST['city'];

$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch

$mail->IsSMTP(); // telling the class to use SMTP

$body .= $nameField;

try {
     //$mail->Host       = "mail.gmail.com"; // SMTP server
      $mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
      $mail->SMTPAuth   = true;                  // enable SMTP authentication
      $mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
      $mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
      $mail->Port       = 465;   // set the SMTP port for the GMAIL server
      $mail->SMTPKeepAlive = true;
      $mail->Mailer = "smtp";
      $mail->Username   = "xxxxx@gmail.com";  // GMAIL username
      $mail->Password   = "********";            // GMAIL password
      $mail->AddAddress('sendto@gmail.com', 'abc');
      $mail->SetFrom('xxxxxx@gmail.com', 'def');
      $mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
      $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
      $mail->MsgHTML($body);
      $mail->Send();
      echo "Message Sent OK</p>\n";
      header("location: ../test.html");
} catch (phpmailerException $e) {
      echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
      echo $e->getMessage(); //Boring error messages from anything else!
}

?>

Go to google settings and enable 'less secure' applications. 转到谷歌设置并启用“不太安全”的应用程序。 It worked for me. 它对我有用。

ping smtp.gmail.com

For some reason, google redirects SMTP requests to gmail-smtp-msa.l.google.com 出于某种原因,Google会将SMTP请求重定向到gmail-smtp-msa.l.google.com

PING gmail-smtp-msa.l.google.com (74.125.133.108): 56 data bytes
64 bytes from 74.125.133.108: icmp_seq=0 ttl=43 time=72.636 ms
64 bytes from 74.125.133.108: icmp_seq=1 ttl=43 time=68.841 ms
64 bytes from 74.125.133.108: icmp_seq=2 ttl=43 time=68.500 ms

So you should put the final destination in your config, as PHPMailer does not follow redirections. 因此,您应该将最终目标放在配置中,因为PHPMailer不遵循重定向。 Also you may try to send emails without SMTPSecure by keeping that field blank. 此外,您可以尝试通过将该字段留空来发送没有SMTPSecure的电子邮件。

$mail->Host = 'gmail-smtp-msa.l.google.com';  
$mail->SMTPAuth = true;                            
$mail->Username = 'email';   
$mail->Password = 'password';    
$mail->SMTPSecure = '';   
$mail->Port = 25;  

If your email in gsuite check two steps: https://support.google.com/a/answer/6260879?hl=en https://accounts.google.com/DisplayUnlockCaptcha 如果您在gsuite中发送电子邮件,请检查以下两个步骤: https//support.google.com/a/answer/6260879 hl = zh-CN https://accounts.google.com/DisplayUnlockCaptcha

Php Mail Settings; Php邮件设置; PHPMailer version 5.2.22 Check class.phpmailer.php, PHPMailer版本5.2.22检查class.phpmailer.php,

  var $Host = 'smtp.gmail.com';
  var $Port = 465;
  var $Helo ='domain.net';
  public $SMTPSecure = 'ssl';
  public $SMTPAutoTLS = true;
  public $SMTPAuth = true;
  public $SMTPOptions = array();
  public $Username = 'gmail-username';//or domain credentials on google mail
  public $Password = 'gmail-password';
  public $Timeout = 10;
  public $SMTPDebug = true;

mail send php 邮件发送PHP

<?php
require('PHPMailer/class.phpmailer.php');
require('PHPMailer/class.smtp.php');

$headers = "Content-Type: text/html; charset=UTF-8";    
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->CharSet="SET NAMES UTF8";
$mail->SMTPDebug  = 0;
$mail->CharSet="utf8";
$mail->Debugoutput = 'html';
$mail->host       = "smtp.gmail.com";
$mail->port       = 465;
$mail->smtpauth   = true;
$mail->smtpsecure = 'ssl';
$mail->username   = "gmail-username"; //or domain credentials on google mail
$mail->password   = "gmail-password";
$mail->SetFrom('from-email', 'from-name');
$mail->AddAddress('to-address', 'to-address');
$mail->Body = 'your-text'; //emailbody
if(!$mail->Send()) 
        {
            mysql_close();
        echo "<script>alert('Error: " . $mail->ErrorInfo."');</script>";
        } 
        else 
        {
        mysql_close();
        echo "<script>alert('success');</script>";
        }

?>

This is work for me. 这对我有用。 Hope it helps. 希望能帮助到你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法实例化邮件 function。消息未发送 PHPMailer 错误:无法实例化邮件 function - Could not instantiate mail function. Message was not sent PHPMailer Error: Could not instantiate mail function PHPMailer 错误:无法发送消息。 邮件程序错误:SMTP 错误:无法验证 - PHPMailer Error: Message could not be sent. Mailer Error: SMTP Error: Could not authenticate Bluehost:PHPMailer 将已发送消息保存在已发送项目中 - Bluehost: PHPMailer save sent message in Sent Items 我从phpmailer收到此错误消息无法发送。Mailer错误:SMTP connect()失败? - I am receiving this error from phpmailer Message could not be sent.Mailer Error: SMTP connect() failed? PhpMailer错误:无法发送邮件。Mailer错误:SMTP connect()失败 - PhpMailer error : Message could not be sent.Mailer Error: SMTP connect() failed PHPMailer 错误:无法发送邮件。Mailer 错误:地址无效:(addAnAddress to):您的电子邮件 - PHPMailer error : Message could not be sent.Mailer Error: Invalid address: (addAnAddress to): Your Email PHPMailer和Gmail smtp,未发送或失败消息 - PHPMailer and Gmail smtp, not getting sent or failed message 邮件已发送但未通过PHPMailer中的电子邮件地址接收? - Message sent but doesnt receive by email address in PHPMailer? PHPMailer 显示“消息已发送”错误 - PHPMailer showing 'Message has been sent' with errors PhpMailer,ClearAddresses()无法正常工作,消息被发送给所有人 - PhpMailer, ClearAddresses() won't work, message get sent to everyone
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM