PHP发送邮件工具:PHPMailer

PHP发送邮件工具:PHPMailer。

PHPMailer

这是一个我使用了很久的php类库,如果你的网站需要一个发送邮件的功能,使用此类库可以快速做到。

它配置简单,可以一对一,也可以一对多,既可以发送纯文本内容的邮件也可以发送HTML格式内容的电子邮件。

总之,它是一个强大的邮件发送类库,使用起来也是相当的简单。

官方地址: https://github.com/PHPMailer/PHPMailer

  • Start:13377
  • Fork:7384

简单使用

安装

composer require phpmailer/phpmailer

例子

<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

// Load Composer's autoloader
require 'vendor/autoload.php';

// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);

try {
    //Server settings
    $mail->SMTPDebug = 2;                                       // Enable verbose debug output
    $mail->isSMTP();                                            // Set mailer to use SMTP
    $mail->Host       = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
    $mail->Username   = 'user@example.com';                     // SMTP username
    $mail->Password   = 'secret';                               // SMTP password
    $mail->SMTPSecure = 'tls';                                  // Enable TLS encryption, `ssl` also accepted
    $mail->Port       = 587;                                    // TCP port to connect to

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

QQ交流群: 824144151

技术援助

需要技术援助?点击这里,帮你解决你的所有问题!PS:可能你离大神之间,只差一个我们!!!!

给TA打赏
共{{data.count}}人
人已打赏
程序使用

快速搭建framework7项目:app与H5

2020-5-8 12:03:38

Good Github

PHP的HTTP请求工具:guzzle

2019-7-10 13:33:35

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索