当前位置: 动力学知识库 > 问答 > 编程问答 >

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

 <?php

require 'PHPMailer/PHPMailerAutoload.php';

$mail = new PHPMailer;

$mail->isSMTP();

$mail->Host = 'smtp.gmail.com';

$mail->SMTPAuth = true;

$mail->Username = '[email protected]';

$mail->Password = '*******';

$mail->Port = 587;

$mail->SMTPSecure = 'tls';

$mail->From = '[email protected]';

$mail->FromName = 'asdf ';

$mail->addAddress('[email protected]', '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?

网友答案:

Since you are getting email from Google, it describes the email is trying to send but it is blocked by Google. Do the following steps.

I hope this helps.

  1. Check if IMAP is enabled
  2. Check here and enable less secure apps
  3. Display Unlock Captcha
网友答案:

Try the following steps:

  • enable debug mode to catch possible errors

    $mail->SMTPDebug = 1;
    
  • enable SMTP authentication

    $mail->SMTPAuth = true;
    
  • also check for SSL support in php configuration file (php.ini)

    extension=php_openssl.dll
    
网友答案:

You need set permission for send mail with gmail.

- Login Google Account
- 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   = "[email protected]";  // GMAIL username
$mail->Password   = "yourpassword";            // GMAIL password

$mail->SetFrom('[email protected]', 'First Last');

$mail->AddReplyTo("[email protected]","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 = "[email protected]";
$mail->AddAddress($address, "John Doe");


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

change the following in your code

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

and yes check the ports also. sometoimes port also off which do not let connection to be established.

Hope it will work!

网友答案:

I think you have to enable POP and IMAP in you gamil. Try this

  • Sign in to Gmail
  • Click the gear in the top right.
  • Select Settings.
  • Click Forwarding and POP/IMAP.
  • Select Enable IMAP.
  • Click Save Changes.

<?
$account="[email protected]";
$password="accountpassword";
$to="[email protected]";
$from="[email protected]";
$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";
}
?>
分享给朋友:
您可能感兴趣的文章:
随机阅读: