menu 贺大礼(乱丶心)的博客
PHP发送邮件 带附件
32 浏览 | 2024-12-27 | 阅读时间: 约 1 分钟 | 分类: php | 标签:

首先引入phpmailer

composer require phpmailer/phpmailer

代码:

<?php

namespace app\services;

use PHPMailer\PHPMailer\PHPMailer;
use think\facade\Log;

class EmailService
{
    public function sendEmail($to, $subject, $content, $files = [])
    {
        $mail = new PHPMailer(true);
        $mail->CharSet='UTF-8';

        $config = [
            'host' => sys_config('email_host'),
            'port' => sys_config('email_port'),
            'username' => sys_config('email_username'),
            'password' => sys_config('email_password'),
            'tls' => sys_config('email_tls'),
            'send_email' => sys_config('email_send_email'),
            'send_name' => sys_config('email_send_name'),
        ];

        Log::error([
            'msg' => '发送邮件',
            'to' => $to,
            'subject' => $subject,
            'content' => $content,
            'files' => $files,
            'config' => $config,
        ]);

        if (empty($config['host']) || empty($config['port']) || empty($config['username']) || empty($config['password']) || empty($config['send_email']) || empty($config['send_name'])) {
            Log::error('发件箱配置错误!');
            return true;
        }

        // 服务器设置
        $mail->isSMTP();                                      // 设置邮件发送服务器
        $mail->Host = $config['host'];                    // 指定SMTP服务器
        $mail->SMTPAuth = true;                               // 开启SMTP认证
        $mail->Username = $config['username'];            // SMTP用户名
        $mail->Password = $config['password'];                    // SMTP密码
        $mail->SMTPSecure = $config['tls'];                            // 安全协议
        $mail->Port = intval($config['port']);                                    // 端口号

        // 收件人
        $mail->setFrom($config['send_email'], $config['send_name']);

        $dirPath = app()->getRootPath() . 'public/uploads/download';
        if (!is_dir($dirPath)) {
            mkdir($dirPath, 0777, true);
        }
        // 附件
        if (!empty($files)) {
            foreach ($files as $file) {
                // 将文件下载到本地
                $fileContent = file_get_contents($file);
                $fileName = basename($file);
                $filePath = $dirPath . '/' . $fileName;
                if (!file_exists($filePath)) {
                    if (!file_put_contents($filePath, $fileContent)) {
                        Log::error('文件下载失败');
                        continue;
                    }
                }
                $mail->addAttachment($filePath);  // 添加附件
            }
        }

        // 内容
        $mail->isHTML(true);                                   // 设置邮件格式为HTML
        $mail->Subject = $subject;
        $mail->Body = $content;


        $mail->addAddress($to);                              // 添加收件人
        try{
            $res = $mail->send();
            if (!$res) {
                Log::error('邮件发送失败: ' . $mail->ErrorInfo);
            }
        }catch (\Exception $e){
            $mail->getSMTPInstance()->reset();
        }
        $mail->clearAddresses();
        return true;
    }
}
知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议

发表评论

email
web

全部评论 (暂无评论)

info 还没有任何评论,你来说两句呐!