由于我已将我的网站上传到服务器,因此我无法使用PHPMailer发送smtp邮件。它之前在我的本地服务器(XAMPP)上工作。我正在使用GMAIL所以我也更改了所有必需的gmail设置(即我创建了另一个OAuth2令牌)。然而,它只是不起作用......
我正在使用的PHP代码(以及脱机工作的代码)如下:
status
当我尝试在线使用它时,首先需要很长时间才能收到错误(尝试使用<?php
/**
* This example shows settings to use when sending via Google's Gmail servers.
*/
//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
date_default_timezone_set('Etc/UTC');
//Load dependencies from composer
//If this causes an error, run 'composer install'
require 'vendor/phpmailer/phpmailer/PHPMailerAutoload.php';
//Load dependencies from composer
//If this causes an error, run 'composer install'
require 'vendor/autoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailerOAuth;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 0;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Set AuthType
$mail->AuthType = 'XOAUTH2';
//User Email to use for SMTP authentication - Use the same Email used in Google Developer Console
$mail->oauthUserEmail = "mygmail@gmail.com";
//Obtained From Google Developer Console
$mail->oauthClientId = "IWONTTELLU.apps.googleusercontent.com";
//Obtained From Google Developer Console
$mail->oauthClientSecret = "IWONTTELLU";
//Obtained By running get_oauth_token.php after setting up APP in Google Developer Console.
//Set Redirect URI in Developer Console as [https/http]://<yourdomain>/<folder>/get_oauth_token.php
// eg: http://localhost/phpmail/get_oauth_token.php
$mail->oauthRefreshToken = "1/IWONTTELLU";
//Set who the message is to be sent from
//For gmail, this generally needs to be the same as the user you logged in as
$mail->setFrom('mygmail@gmail.com', 'BLA');
//Set who the message is to be sent to
$mail->addAddress('my@privatemail.com', 'Hi');
//Set the subject line
$mail->Subject = 'PHPMailer GMail SMTP test';
//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(file_get_contents('contents.html'), dirname(__FILE__));
//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()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
;长话短说:也没用),其次,我得到的代码是gethostbyname('smtp.gmail.com')
&amp; Error: Failed to connect to server: Connection timed out (110)
。我使用的服务由1and1(1und1 / 1&amp; 1)提供。我一直试图解决这个问题超过4个小时,现在无法找出问题所在。不太安全的应用程序可以访问,我还创建了一个令牌并“创建”了https://github.com/PHPMailer/PHPMailer/wiki/Using-Gmail-with-XOAUTH2中的刷新令牌。故障排除指南(https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting)对我来说也无法帮助我,尽管我认为它在验证方面存在问题......我也尝试过使用ssl(当然我也更改了端口等)工作要么。
答案 0 :(得分:0)
gmail还支持端口587吗?你可能想要SSL端口465.这篇文章也可能有用https://www.wpsitecare.com/gmail-smtp-settings/或者l&1可能被gmail阻止了?请参阅此帖子http://docs.mailpoet.com/article/61-sending-with-gmail-doesnt-work