doutui6241 2018-02-13 08:13
浏览 41

无法让PHPMailer使用我的Gmail

I am trying to use PHPMailer with my contact forms on my site. I got it working when I was local with Mailtrap. I moved my site online with Godaddy and now it doesn't work. I can't even send the contact forms to the demo Mailtrap site let alone my Gmail (where I want to go). Any suggestions would help a lot.

I run the following code as a PHP function

 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;
    }
 }

that code then links to my classes that has my email templates and all the variable inputs

 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"

This worked great for Mailtrap when it was local, now that I moved it live I cannot get it to work with Mailtrap or Gmail.

Locally when I try to run it with Gmail I get:

 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>
<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

When I try to run either Gmail or Mailtrap from Godaddy, nothing happens it just sits there trying to run. No error or anything.

Any help would be appreciated. Thank you!!

This post seems similar to post "PHPMailer GoDaddy Server SMTP Connection Refused", it isn't from what I can tell as that one is dealing with Godaddy hosted email and I am using Gmail. I did try to match some of their code but it doesnt work with Gmail.

  • 写回答

1条回答 默认 最新

  • dosc9472 2018-02-13 17:51
    关注

    So I contacted Godaddy, and they supplied me with a test script that ran PHPMailer on my shared server. The script ran fine and that just confused me more.

    After a bit of more searching, I found that Godaddy was blocking SMTP, I confirmed this from one of the options in the script. I changed my code from SMTP to PHP mail() and it worked.

     $mail->isSendmail();
    

    insted of $mail->isSMTP();

    Now it takes longer than SMTP to send the email but it's not too long and it works, which right now is all that matters.

    I put the file they gave me online (30-day temp) if anyone could use it. https://ufile.io/2kqkm

     That file has an option to send via SMTP or mail(). When I ran it through 
     SMTP on my Godaddy site I received an error, part of which stated: 
     We do not authorize the use of this system to transport unsolicited, 
     220 and/or bulk e-mail.
    

    Thank you all for your help!!

    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?