首页 > 解决方案 > 使用phpmailer发送邮件的功能不断加载没有警告

问题描述

我正在测试 phpmailer 发送电子邮件,我创建了一个类来发送电子邮件,但页面一直加载,直到超时

我创建了下面的类,我在另一个文件中引用这个类,该文件应该在加载时发送电子邮件


class ManageEmails
{

private $pdo;
private $mail;



public function __construct(\PDO $pdo, PHPMailer $mail)
     {
        $this->pdo = $pdo;
        $this->mail = $mail;
     }

public function sendEmail($toEmail, $subject, $message, $file_path='')
    {   
        $this->mail->setFrom('info@afrojp.com', 'Darth Vader');
        $this->mail->addAddress($toEmail, 'Emperor');
        $this->mail->Subject = $subject;
        $this->mail->Body = $message;


        $this->mail->isSMTP();
        $this->mail->Host = 'smtp.gmail.com';

        $this->mail->Port = '587';

        $this->mail->SMTPAuth=true;

        $this->mail->SMTPSecure='tls';

        // need to get smtp username
        $this->mail->username = 'myemail@gmail.com';

        $this->mail->password ='appPassword';

        //we can set true if we have ssl otherwise leave blank;





        $this->mail->wrap = 50;

        $this->mail->isHTML(true);

        // if there is an attachment

        if($file_path='') $this->mail->AddAttachment($file_path);

        if($this->mail->send())
            {
                return true;
            }

        else return false;

    }

然后我调用了发送邮件方法;



    require 'classes/PHPMailer/src/Exception.php';


    require 'classes/PHPMailer/src/PHPMailer.php';


    require 'classes/PHPMailer/src/SMTP.php';

    $mail = new PHPMailer(true);

    $manageEmails = new ManageEmails($pdo,$mail);

    $manageEmails->sendEmail('msiskasmith@gmail.com','Hello Trial', 'Whoop whoop');



当我期望它发送电子邮件时,页面继续加载

标签: phpphpmailer

解决方案


推荐阅读