PHPMailer can't send message to GMAIL

僤鯓⒐⒋嵵緔 提交于 2020-06-29 05:57:08

问题


I am trying to create the contact form and created send mail function. I have faced the problem of send message to my gmail. When I send the message to my gmail. The gmail show me below the message:

The gmail message show me due to someone just used your password to try to sign in to your account from a non-Google app. Google blocked them, but you should check what happened. Review your account activity to make sure no one else has access. Gmail message

       <?php

 use PHPMailer\PHPMailer\PHPMailer;
 use PHPMailer\PHPMailer\SMTP;


 function email_setting() {
 $email_setting['host'] = 'smtp.gmail.com';
 $email_setting['username'] = 'abcxxxxx@gmail.com';
 $email_setting['password'] = '123456';
 $email_setting['port'] = '587';
 $email_setting['admin_email'] = 'abc@xxxxxxxx.com.my';
 $email_setting['admin_name'] = 'Mr Chong';

 return $email_setting;
 }


 function send_email_function($user_email, $mail_subject, $mail_content, $user_name) {
 require_once ('plugins/PHPMailer/src/PHPMailer.php');
 require_once ('plugins/PHPMailer/src/SMTP.php');
 $email_setting = email_setting();
 //Create a new PHPMailer instance
 $mail = new PHPMailer;
 //Tell PHPMailer to use SMTP
 $mail->isSMTP();
//Enable SMTP debugging
// SMTP::DEBUG_OFF = off (for production use)
 // SMTP::DEBUG_CLIENT = client messages
// SMTP::DEBUG_SERVER = client and server messages
 $mail->SMTPDebug = SMTP::DEBUG_OFF ;
//Set the hostname of the mail server
$mail->Host = $email_setting['host'];
 // use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = $email_setting['port'];
//Set the encryption mechanism to use - STARTTLS or SMTPS
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = $email_setting['username'];
//Password to use for SMTP authentication
$mail->Password = $email_setting['password'];
//Set who the message is to be sent from
$mail->setFrom($user_email, $user_name);
//Set an alternative reply-to address
$mail->addReplyTo($user_email, $user_name);
//Set who the message is to be sent to
$mail->addAddress($email_setting['admin_email'], $email_setting['admin_name']);
//Set the subject line
$mail->Subject = $mail_subject;
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML($mail_content);
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
//$mail->addAttachment('images/phpmailer_mini.png');
//send the message, check for errors
if (!$mail->send()) {
    return 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    return true;
    //Section 2: IMAP
    //Uncomment these to save your message in the 'Sent Mail' folder.
    #if (save_mail($mail)) {
    #    echo "Message saved!";
    #}
}
 }
   $user_email=$_POST['email'];
   $mail_subject="ACASE Enquiry.";
     $mail_content=$_POST['message'];
    $user_name=$_POST['name'];
     $mail_content = wordwrap($mail_content,70);
       echo send_email_function($user_email, $mail_subject, $mail_content, $user_name)
       ?>

May I know how can unblock the gmail sign in just can let me send the message? Thanks.


回答1:


You can generate an application specific password by following these instructions: https://support.google.com/accounts/answer/185833

Then set $email_setting['password'] to your application specific password.



来源:https://stackoverflow.com/questions/59385174/phpmailer-cant-send-message-to-gmail

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!