定制 ank/mail 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ank/mail

Composer 安装命令:

composer require ank/mail

包简介

php mail

README 文档

README

因为调用的时候有些参数是必填项,如果不填写就会报错错,所以下面把邮件类封闭成一个函数来调用

function send_mail($conf = array())
{
    //ini_set("memory_limit","100M");
    $host     = isset($conf['host']) ? $conf['host'] : '';
    $port     = isset($conf['port']) ? $conf['port'] : '';
    $username = isset($conf['username']) ? $conf['username'] : '';
    $password = isset($conf['password']) ? $conf['password'] : '';

    $toname  = isset($conf['toname']) ? $conf['toname'] : '';
    $toemail = isset($conf['toemail']) ? $conf['toemail'] : '';

    $fromname  = isset($conf['fromname']) ? $conf['fromname'] : ''; //来源名称
    $fromemail = isset($conf['fromemail']) ? $conf['fromemail'] : ''; //来源邮箱

    $subject    = isset($conf['subject']) ? $conf['subject'] : '';
    $body       = isset($conf['body']) ? $conf['body'] : '';
    $extra_msg  = isset($conf['extra_msg']) ? $conf['extra_msg'] : '';
    $attachment = isset($conf['attachment']) ? $conf['attachment'] : '';
    $port       = $port ?: 25;
    $fromname   = $fromname ?: $username;

    if (empty($host)) {
        return '[host]主机地址不能为空';
    }
    if (empty($username)) {
        return '[username]smtp服务器用户名不能为空';
    }
    if (empty($password)) {
        return '[password]smtp服务器密码不能为空';
    }
    if (empty($fromname)) {
        return '[fromname]寄件人名字不能为空';
    }
    if (empty($fromemail)) {
        return '[fromemail]寄件人邮箱不能为空';
    }

    if (empty($toname)) {
        return '[toname]收件人名字不能为空';
    }
    if (empty($toemail)) {
        return '[toemail]收件人邮箱不能为空';
    }
    if (empty($subject)) {
        return '[subject]邮件主题不能为空';
    }
    if (empty($body)) {
        return '[body]邮件内容不能为空';
    }

    // import('Ainiku.PHPMailer');
    $mail = new \ank\PHPMailer();

    $mail->SMTPDebug = 0; // 关闭SMTP调试功能
    $mail->IsSMTP(); // send via SMTP
    $mail->Host     = $host; // SMTP servers
    $mail->SMTPAuth = true; // turn on SMTP
    $mail->Username = $username; // SMTP username
    $mail->Password = $password; // SMTP password
    $mail->From     = $fromemail; // 发件人邮箱
    $mail->FromName = $fromname; // 发件人
    $mail->CharSet  = "utf-8"; // 这里指定字符集!
    $mail->Encoding = "base64";
    $mail->AddAddress($toemail, $toname); // 收件人邮箱和姓名
    $mail->IsHTML(true); // send as HTML
    //$mail->SMTPSecure = 'ssl';   // 使用安全协议用qq邮箱时要用
    $mail->Subject  = $subject; // 邮件主题
    $mail->Body     = $body;
    $mail->WordWrap = 50; // set word wrap 换行字数
    $mail->AltBody  = "text/html";
    $mail->AddReplyTo($fromemail, $fromname); //快捷回复
    if ($attachment) {
        foreach ($attachment as $key => $value) {
            if (!file_exists($value['path'])) {
                return '附件文件路径不存在';
            }
            if (filesize($value['path']) > 1024 * 1024 * 10) {
                return '附件不能大于10M';
            }
            $mail->AddAttachment($value['path'], $value['name']);
        }
    }
    //$mail->AddAttachment("sendmail.rar");     // attachment 附件
    //$mail->AddAttachment("psd.jpg", "psd.jpg");  //添加一个附件并且重命名

    if ($mail->Send()) {
        return true;
    } else {
        return config('app_debug') ? $mail->ErrorInfo : '邮件发送错误!';
    }

}

##调用方法

$result = send_mail([
    'host'       => 'smtp.163.com',
    'port'       => 25,
    'username'   => 'deari****oever',
    'password'   => '*****',
    'fromemail'  => 'dearilo****oever@163.com',
    'fromname'   => '我是网易邮箱',

    'toemail'    => '735579768@qq.com',
    'toname'     => '你好克立兄',
    'subject'    => '内容的一个摘要',
    'body'       => '这个是邮件内容',
    //附件为一个数组格式如下
    'attachment' => [
        [
            'path' => 'e:/OpenHostsEdit.bat',
            'name' => 'host.txt',
        ],
        [
            'path' => 'E:/GitServer/curl-7.60.0.zip',
            'name' => 'curl.zip',
        ],
    ],
]);
if ($result !== true) {
    die($result);
}
die('send mail success');

ank/mail 适用场景与选型建议

ank/mail 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 12 月 02 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 ank/mail 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 ank/mail 我们能提供哪些服务?
定制开发 / 二次开发

基于 ank/mail 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 6
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-12-02