TP5实现发送邮件

记录一次邮件发送功能实现
使用 composer 安装 phpemailer 依赖

composer update 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';

$mail = new PHPMailer(true);                              // Passing true enables exceptions
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;
}
  • 实际项目中要结合具体环境与应用场景

结合 env 或者 config 配置邮件相关参数

<?php
//配置文件
return [
    'host' => 'xxx',
    'port' => xxx,
    'username' => 'xxx',
    'password' => 'xxx',
];
//发送邮件方法
//事先引入文件
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
//Load Composer's autoloader
require 'vendor/autoload.php';
public function sendEmail($attachment)
{
       $email_config = Config::get('email.');
       $mail = new PHPMailer();
       $mail->isSMTP();
       $mail->CharSet = "UTF-8";
       $mail->Host = $email_config['host'];
       $mail->SMTPAuth = true;
       $mail->Username = $email_config['username'];
       $mail->Password = $email_config['password'];
       $mail->SMTPSecure = 'ssl';
       $mail->Port = $email_config['port'];

       $mail->setFrom($email_config['username'],"Mailer"); //名字随意叫

       $staffs = Config::get('report.staff');//发送邮件的的对象,可随意配置
       foreach( $staffs as $staff){
           $mail->addAddress($staff['email'],$staff['name']);
       }
       $mail->addReplyTo('xxx@email.com',"Reply"); //回复
       $mail->addCC("aaaa@sss.com"); // 抄送
       $mail->addBCC("bbbb@sss.com"); //密送
       // 添加附件
       $mail->addAttachment($attachment);

       $mail->isHTML(true);
       $mail->Subject = '邮件标题';// 邮件标题
       $mail->Body = "This is the html body <b>very stronge非常强壮</b>";// 邮件正文
       //$mail->AltBody = "This is the plain text纯文本";// 这个是设置纯文本方式显示的正文内容,如果不支持Html方式,就会用到这个,基本无用

       if( ! $mail->send() ){// 发送邮件
           trace('Message could not be sent, Mailer Error: '.$mail->ErrorInfo,'report');
           var_dump('send fail');
           exit;
       }
}
  • 发送邮件附件的时候附件文件乱码

转码 iconv("UTF-8", "GBK//TRANSLIT", $text)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值