簡體   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