简体   繁体   English

无法在php中发送电子邮件

[英]Unable to send email in php

I am trying following code to send mail but it is showing 我正在尝试按照以下代码发送邮件,但显示

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

and gmail is informing me that Sign-in attempt prevented 而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";
    ?>

How to resolve above problem? 如何解决以上问题?

You need set permission for send mail with gmail . 您需要设置许可才能使用gmail发送邮件。

- Login Google Account -登录Google帐户
- Go Privacy Page -进入隐私页面
- Allow third party apps -允许第三方应用

After try this code: 尝试以下代码后:

$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!";
}

Try the following steps: 请尝试以下步骤:

  • enable debug mode to catch possible errors 启用调试模式以捕获可能的错误

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

     $mail->SMTPAuth = true; 
  • also check for SSL support in php configuration file ( php.ini ) 还要在php配置文件( php.ini )中检查SSL支持

     extension=php_openssl.dll 

change the following in your code 更改代码中的以下内容

isSMTP() to IsSMTP() , addAddress() to AddAddress() & isHTML() to IsHTML() . isSMTP()IsSMTP()addAddress()AddAddress()isHTML()IsHTML()

and yes check the ports also. 是的,还要检查端口。 sometoimes port also off which do not let connection to be established. sometoimes端口也关闭,不允许建立连接。

Hope it will work! 希望它能工作!

Since you are getting email from Google, it describes the email is trying to send but it is blocked by Google. 由于您是从Google收到电子邮件,因此它描述了该电子邮件正在尝试发送,但已被Google阻止。 Do the following steps. 请执行以下步骤。

I hope this helps. 我希望这有帮助。

  1. Check if IMAP is enabled 检查是否启用了IMAP
  2. Check here and enable less secure apps 在此处检查并启用安全性较低的应用
  3. Display Unlock Captcha 显示解锁验证码

I think you have to enable POP and IMAP in you gamil. 我认为您必须在gamil中启用POP和IMAP。 Try this 尝试这个

  • Sign in to Gmail 登录到Gmail
  • Click the gear in the top right. 点击右上角的齿轮。
  • Select Settings. 选择设置。
  • Click Forwarding and POP/IMAP. 单击转发和POP / IMAP。
  • Select Enable IMAP. 选择启用IMAP。
  • Click Save Changes. 单击保存更改。

 <? $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