09
2025
04
17:22:52

PHPMailer发送邮件

安装

composer require phpmailer/phpmailer

发送邮件

//Import PHPMailer classes into the global namespace//These must be at the top of your script, not inside a functionuse PHPMailer\PHPMailer\PHPMailer;use PHPMailer\PHPMailer\SMTP;use PHPMailer\PHPMailer\Exception;//Load Composer's autoloaderrequire 'vendor/autoload.php';//Create an instance; passing `true` enables exceptions$mail = new PHPMailer(true);try {    //Server settings
    $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      //Enable verbose debug output
    $mail->isSMTP();                                            //Send using SMTP
    $mail->Host       = 'smtp.example.com';                     //Set the SMTP server to send through
    $mail->SMTPAuth   = true;                                   //Enable SMTP authentication
    $mail->Username   = 'user@example.com';                     //SMTP username
    $mail->Password   = 'secret';                               //SMTP password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;            //Enable implicit TLS encryption
    $mail->Port       = 465;                                    //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`

    //Recipients
    $mail->setFrom('from@example.com', 'Mailer');
    $mail->addAddress('joe@example.net', 'Joe User');     //Add a recipient
    $mail->addAddress('ellen@example.com');               //Name is optional
    $mail->addReplyTo('info@example.com', 'Information');
    $mail->addCC('cc@example.com');
    $mail->addBCC('bcc@example.com');    //Attachments
    $mail->addAttachment('/var/tmp/file.tar.gz');         //Add attachments
    $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    //Optional name

    //Content
    $mail->isHTML(true);                                  //Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    $mail->send();    echo 'Message has been sent';
} catch (Exception $e) {    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}

标题正文乱码

原因

因为默认采用的编码是iso-8859-1

//PHPMailer.php line 77
 public $CharSet = self::CHARSET_ISO88591;

如果我们的文件编码是UTF-8,按默认配置发送收到的可能是乱码。(很神奇,QQ邮箱可以自己识别)。

解决办法

    $mail = new PHPMailer(true);
    ...    //设置编码
    $mail->CharSet = PHPMailer::CHARSET_UTF8;
    ...
    $ret = $mail->send();

指定ip 发送

场景

有时候我们的服务器会绑多个ip,但要指定特定ip发送。

代码

    $mail = new PHPMailer(true);
    ...
    $mail->SMTPOptions = array(        'socket' => array(            'bindto' => "127.0.0.2:0",
        ),
    );
    ...
    $ret = $mail->send();

效果

打开收到的邮件,查看原文。可以找到发送邮件的地址。如果是新设定的ip,说明配置成功。

Received: from xxxx (127.0.0.2 [127.0.0.2])…




推荐本站淘宝优惠价购买喜欢的宝贝:

image.png

本文链接:https://hqyman.cn/post/10291.html 非本站原创文章欢迎转载,原创文章需保留本站地址!

分享到:
打赏





休息一下~~


« 上一篇 下一篇 »

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

请先 登录 再评论,若不是会员请先 注册

您的IP地址是: