无法让PHPMailer使用我的Gmail

时间:2018-02-13 08:13:45

标签: php email phpmailer

我正在尝试将PHPMailer与我的网站上的联系表单一起使用。当我在Mailtrap本地时,我得到了它的工作。我用Godaddy将我的网站移到了网上,现在它无法正常工作。我甚至无法将联系表单发送到示例Mailtrap站点,更不用说我的Gmail(我想去的地方)。任何建议都会有所帮助。

我将以下代码作为PHP函数运行

 use PHPMailer\PHPMailer\PHPMailer;
 use PHPMailer\PHPMailer\Exception;
 function send_email($email, $subject, $message, $alt_message, $headers){
    $mail = new PHPMailer(true);
    $mail->isSMTP();
    $mail->Host = Email::SMTP_HOST;
    $mail->Username = Email::SMTP_USER;
    $mail->Password = Email::SMTP_PASSWORD;
    $mail->Port = Email::SMTP_PORT;
    $mail->SMTPAuth = true;
    $mail->SMTPSecure = 'tls';
    $mail->setFrom('no-reply@email.ca', 'Admin');
    $mail->addAddress($email);
    $mail->Subject = $subject;
    $mail->Body    = $message;
    $mail->AltBody = $alt_message;
    if(!$mail->send()){
        return false;
    }else{
        return true;
    }
 }

该代码然后链接到我的具有我的电子邮件模板和所有变量输入的类

 class Email{
     const SMTP_HOST = 'smtp.mailtrap.io';
     const SMTP_PORT = 2525;
     const SMTP_USER = '******';
     const SMTP_PASSWORD = '*******';

     function validate_email_temp($email, $message, $first_name, 
     $last_name){
      return "long html email markup"

这对Mailtrap来说非常有用,因为它是本地的,现在我将它移动到现在我无法使用Mailtrap或Gmail。

当我尝试使用Gmail运行时,我会得到:

 Fatal error: Uncaught PHPMailer\PHPMailer\Exception: SMTP connect() failed. 
 https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting in 
 C:\xampp\htdocs\lib\vendor\phpmailer\phpmailer\src\PHPMailer.php:1726 Stack 
 trace: #0 
 C:\xampp\htdocs\lib\vendor\phpmailer\phpmailer\src\PHPMailer.php(1481): 
 PHPMailer\PHPMailer\PHPMailer->smtpSend('Date: Mon, 12 F...', 'This is a 
 multi...') #1 
 C:\xampp\htdocs\lib\vendor\phpmailer\phpmailer\src\PHPMailer.php(1320): 
 PHPMailer\PHPMailer\PHPMailer->postSend() #2 
 C:\xampp\htdocs\lib\functions.php(68): PHPMailer\PHPMailer\PHPMailer-
 >send() #3 C:\xampp\htdocs\lib\Contact.php(68): 
 send_email('email@gma...', 'test', '<html>\r\n<head><...', 'This is a 
 messa...', '') #4 
 C:\xampp\htdocs\public\inc\templates\contact_forms.php(4): Contact-
 >contacts('gen', 'asdf@asdf', 'test', 'test', 'test', 'test') #5 {main} 
 thrown in C:\xampp\htdocs\lib\vendor\phpmailer\phpmailer\src\PHPMailer.php 
 on line 1726

当我尝试从Godaddy运行Gmail或Mailtrap时,没有任何反应只是坐在那里试图运行。没有错误或任何事情。

任何帮助将不胜感激。谢谢!!

这篇文章看起来类似于帖子“PHPMailer GoDaddy Server SMTP Connection Wasfused”,它不是我能说的,因为那个人正在处理Godaddy托管的电子邮件而我正在使用Gmail。我确实尝试匹配他们的一些代码,但它不适用于Gmail。

1 个答案:

答案 0 :(得分:0)

所以我联系了Godaddy,他们为我提供了一个在共享服务器上运行PHPMailer的测试脚本。脚本运行正常,这让我更加困惑。

经过一番搜索后,我发现Godaddy阻止了SMTP,我从脚本中的一个选项中确认了这一点。我将我的代码从SMTP更改为PHP mail()并且它有效。

let array = [
{
  id: 123456, user: 'xyz'
},
{
  id:9787389023, user: 'zyx'
},
{
  id: 123456, user: 'che'
}
]

let newArray = array.filter((element) => element.id === 123456)
console.log(newArray)
绝对的      $ MAIL-&GT; isSMTP();

现在发送电子邮件需要比SMTP更长的时间,但它不会太长而且有效,现在这一切都很重要。

我把他们给我的文件放在网上(30天临时)如果有人可以使用它。 https://ufile.io/2kqkm

 private static async Task<Stream> GetByteArrayAsync(Uri uri, CancellationToken token) {
            using (HttpClient httpClient = new HttpClient()) {
                HttpResponseMessage response = await httpClient.GetAsync(uri, token);
                return await response.Content.ReadAsStreamAsync();
            }
        }

谢谢大家的帮助!!

相关问题