繁体   English   中英

无法在php中发送电子邮件

[英]Unable to send email in php

我正在尝试按照以下代码发送邮件,但显示

Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting   

而gmail通知我该登录尝试已阻止

 <?php
    require 'PHPMailer/PHPMailerAutoload.php';

    $mail = new PHPMailer;

    $mail->isSMTP();
    $mail->Host = 'smtp.gmail.com';
    $mail->SMTPAuth = true;
    $mail->Username = 'sss@gmail.com';
    $mail->Password = '*******';
     $mail->Port =  587;
    $mail->SMTPSecure = 'tls';
     $mail->From = 'abc@gmail.com';
    $mail->FromName = 'asdf ';
    $mail->addAddress('abc@gmail.com', 'sadf ');

    $mail->WordWrap = 50;
    $mail->isHTML(true);

    $mail->Subject = "Using PHPMailer";
    $mail->Body    = "Hi Iam using PHPMailer library to sent SMTP mail from localhost";

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

    echo "Message has been sent";
    ?>

如何解决以上问题?

您需要设置许可才能使用gmail发送邮件。

-登录Google帐户
-进入隐私页面
-允许第三方应用

尝试以下代码后:

$mail             = new PHPMailer();

$body             = file_get_contents('contents.html');
$body             = eregi_replace("[\]",'',$body);

$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host       = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
                                           // 1 = errors and messages
                                           // 2 = messages only
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->SMTPSecure = "tls";                 // sets the prefix to the servier
$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
$mail->Port       = 587;                   // set the SMTP port for the GMAIL server
$mail->Username   = "yourusername@gmail.com";  // GMAIL username
$mail->Password   = "yourpassword";            // GMAIL password

$mail->SetFrom('name@yourdomain.com', 'First Last');

$mail->AddReplyTo("name@yourdomain.com","First Last");

$mail->Subject    = "PHPMailer Test Subject via smtp (Gmail), basic";

$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

$mail->MsgHTML($body);

$address = "whoto@otherdomain.com";
$mail->AddAddress($address, "John Doe");


if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {
  echo "Message sent!";
}

请尝试以下步骤:

  • 启用调试模式以捕获可能的错误

     $mail->SMTPDebug = 1; 
  • 启用S​​MTP身份验证

     $mail->SMTPAuth = true; 
  • 还要在php配置文件( php.ini )中检查SSL支持

     extension=php_openssl.dll 

更改代码中的以下内容

isSMTP()IsSMTP()addAddress()AddAddress()isHTML()IsHTML()

是的,还要检查端口。 sometoimes端口也关闭,不允许建立连接。

希望它能工作!

由于您是从Google收到电子邮件,因此它描述了该电子邮件正在尝试发送,但已被Google阻止。 请执行以下步骤。

我希望这有帮助。

  1. 检查是否启用了IMAP
  2. 在此处检查并启用安全性较低的应用
  3. 显示解锁验证码

我认为您必须在gamil中启用POP和IMAP。 尝试这个

  • 登录到Gmail
  • 点击右上角的齿轮。
  • 选择设置。
  • 单击转发和POP / IMAP。
  • 选择启用IMAP。
  • 单击保存更改。

 <? $account="email_address@gmail.com"; $password="accountpassword"; $to="mail@subinsb.com"; $from="email_address@gmail.com"; $from_name="Name"; $msg="<strong>This is a bold text.</strong>"; // HTML message $subject="Database Backup"; /*End Config*/ include("phpmailer/class.phpmailer.php"); $mail = new PHPMailer(); $mail->IsSMTP(); $mail->CharSet = 'UTF-8'; $mail->Host = "smtp.gmail.com"; $mail->SMTPAuth= true; $mail->Port = 465; // Or 587 $mail->Username= $account; $mail->Password= $password; $mail->SMTPSecure = 'ssl'; $mail->From = $from; $mail->FromName= $from_name; $mail->isHTML(true); $mail->Subject = $subject; $mail->Body = $msg; $mail->addAddress($to); if(!$mail->send()){ echo "Mailer Error: " . $mail->ErrorInfo; }else{ echo "E-Mail has been sent"; } ?> 

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM