ThinkPHP 3.2.3 使用 Swift Mailer 邮件系统发送邮件
发送 swift 邮件 thinkphp 3.2 使用
2023-09-11 14:17:02 时间
SwiftMailer 下载地址:https://github.com/swiftmailer/swiftmailer
版本:swiftmailer-5.x
把压缩包解压到 /ThinkPHP/Library/Vendor 中。
配置文件 config.php
<?php return array( //'配置项'=>'配置值' // 邮件配置 'SMTP' => 'smtp.XXX.cn', 'MAIL_PORT' => 25, 'MAIL_USER' => 'XXX@XXX.com', //邮箱用户名 'MAIL_PWD' => 'XXX', //发送邮箱密码或者授权码 'MAIL_FROM' => 'XXX@XXX.com', 'MAIL_FROM_NAME' => 'dee', );
/Application/Home/Common/Swiftmail.class.php
<?php namespace Home\Common; class Swiftmail { // @param $host 邮件服务器地址 // @param $port 端口号 // @param $encryption_type 加密方式(例如:使用腾讯qq邮箱时此处填ssl,不加密不填写此项) // @param $user 用户名 // @param $pwd 密码或授权码 // @param $subject 邮件主题 // @param $body 邮件内容 // @param $from 邮件来自邮箱号 // @param $from_name 邮件来自名称 // @param $to 收件人邮箱 public static function sendMail($to, $subject, $body, $encryption_type = null) { $host = C('SMTP'); $port = C('MAIL_PORT'); $user = C('MAIL_USER'); $pwd = C('MAIL_PWD'); $from = C('MAIL_FROM'); $from_name = C('MAIL_FROM_NAME'); Vendor('swiftmailer.lib.swift_required'); $transport=\Swift_SmtpTransport::newInstance($host, $port, $encryption_type) ->setUsername($user) ->setPassword($pwd); $mailer =\Swift_Mailer::newInstance($transport); $message=\Swift_Message::newInstance() ->setSubject($subject) ->setFrom(array($from=>$from_name)) ->setTo($to) ->setContentType("text/html") ->setBody($body); $mailer->protocol='smtp'; $mailer->send($message); } }
控制器和方法(按需求确定位置)/Application/Home/Controller/IndexController.class.php
<?php namespace Home\Controller; use Think\Controller; use Home\Common\Swiftmail; class IndexController extends Controller { public function mail_send() { $to = '472323087@qq.com'; $subject = 'SwiftMail测试标题'; $body = '<h1>SwiftMail演示</h1>这是dee对SwiftMail的测试内容'; try { Swiftmail::sendMail($to, $subject, $body); echo 'success'; } catch(Swift_RfcComplianceException $e) { echo $e->getMessage(); } } }
运行后显示 success
收取邮件:
打开邮件:
相关文章
- C#winform UDP通信 发送和接收信息
- C# UDP接收和发送
- nagios邮件报警发送中文、nagios监控页面乱码解决
- Swift - 使用NSNotificationCenter发送通知,接收通知
- swift的static和class修饰符---What is the difference between static func and class func in Swift?
- Java Mail最基本的发送邮件例子
- UDP 网络程序-发送_接收数据
- 【GD32】从0开始学GD32单片机(9)—— SPI外设详解+主机从机发送和接收例程
- C# 发送HTTP请求(可加入Cookies)
- vue使用axios发送post请求时的坑及解决原理
- js 发送http请求
- java中原生的发送http请求(无任何的jar包导入)