【问题标题】:Send email using PHP Mailer使用 PHP Mailer 发送电子邮件
【发布时间】:2018-08-17 13:20:18
【问题描述】:

我正在尝试使用 PHP Mailer 发送电子邮件。该网站被上传到一个免费的主机。显示有关失败的 SMTP 连接的错误。我希望有这方面的专业知识的人。

这是我的代码。

    require '../extensions/phpmailer/PHPMailerAutoload.php';

        $emailAddress = 'SampleEmail';
        $password = 'Emailpassword';            
        $subject = 'Invitation to employee portal'; 
        $message = '                
            <!DOCTYPE html>
            <html xmlns="http://www.w3.org/1999/xhtml">
            <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
            <title>Invitation</title>
            <body style ="background:#eee;">
            <table cellpadding="0" cellspacing="0" border="0" id="backgroundTable" style ="padding: 40px; max-width:640px;background-color:#f3f3f3;display:block;margin:0 auto; font-family:Lato, sans-serif; color:#626262;font-size:13px;line-height:22px;" width ="600"> 
            <tbody>                                 
            <tr>                                        
            <td style ="background-color: #3552f2; padding: 34px 122px;">   
                <h1 width ="600" style ="max-width:600px;width:100%; text-align:center; color:#fff;">HRMS DHVTSU</h1>                                       
            </td>                                   
            </tr>                                   
            <tr>                                        
            <td style ="background-color: #ffffff; text-align: center; padding: 40px;">                                         
            <h1 style="font-size: 40px; color:#000; font-style: italic;">Sorry your account has been deactivated.</h1>                                          
            <br>                                            
            <p style ="color:#000; font-size: 16px;">For any questions please contact  HR Department.</p>                                           
            <br>                                            
            <br>                                        
            </td>                                   
            </tr>                                   
            <tr>                                        
            <td  style ="background-color: #3552f2; text-align: center; font-size: 16px; color: #fff;">                                         
            <p>© 2016. All rights reserved.</p>                                     
            </td>                                   
            </tr>                               
            </tbody>                            
            </table>                        
            </body>';               



        $mail = new PHPMailer;                      
        $mail->isSMTP();    
        // $mail->SMTPDebug = 2;
        $mail->SMTPOptions = array(
            'ssl' => array(
                'verify_peer' => false,
                'verify_peer_name' => false,
                'allow_self_signed' => true
            )
        );          
        $mail->Host = 'smtp.gmail.com';         
        $mail->Port = 587;          
        $mail->SMTPSecure = 'tls';          
        $mail->SMTPAuth = true;         
        $mail->Username = $emailAddress;            
        $mail->Password = $password;            
        $mail->setFrom('noreply@hrms.com', 'HRMS');         
        $mail->addReplyTo('johndoe@gmail.com', 'HRMS');         
        $mail->addAddress($email);          
        $mail->Subject = $subject;          
        $mail->msgHTML($message);                       


        if (!$mail->send()) {

            // $error = "Mailer Error: " . $mail->ErrorInfo;
            // echo '<script>alert("'.$error.'");</script>';
            return false;
        }else {
            $query = 'Update employee_has_credentials set status = '.self::STATUS_INACTIVE.' where id= '.$id.'';
            $result = $this->con->prepare($query);
            $result->execute(array($id));

            if($result->rowCount()){
                return true;
            }else{
                return false;
            }
        }

【问题讨论】:

  • Search before you post。免费托管很可能会阻止出站 SMTP。与 gmail 交谈时,您永远不需要禁用证书验证 - 您可能需要更新您的 CA 证书,如故障排除指南中所述。
  • 您正在尝试从 noreply@hrms.com 发送并尝试使用 Gmail SMTP,我认为您不能这样做,请尝试使用主机的 SMTP,我不确定是否这就是问题,但我希望它有所帮助
  • 您可以尝试使用 SparkPost 而不是 GMail - 如果 587 被您的免费托管服务提供商阻止,它们会在端口 2525 上提供连接。

标签: php phpmailer


【解决方案1】:

PHP Mailer issue 的可能重复项使用该代码,它将解决您的问题,因为您尚未添加以下代码:

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

或转到上面的链接,您可以从该答案中看到更多描述。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-03
    • 1970-01-01
    • 1970-01-01
    • 2012-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多