PHPMailer无法将消息发送到GMAIL

时间:2019-12-18 03:40:58

标签: php gmail

我正在尝试创建联系表并创建发送邮件功能。我遇到了向我的gmail发送消息的问题。当我将邮件发送到我的Gmail时。 gmail向我显示以下消息:

由于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邮件向我显示 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)
       ?>

我可以知道如何解除对gmail登录的阻止吗?可以让我发送邮件吗?

1 个答案:

答案 0 :(得分:0)

您可以按照以下说明生成应用专用密码:https://support.google.com/accounts/answer/185833

然后将$ email_setting ['password']设置为您的应用程序专用密码。