sun / sunmailer
SunMailer 帮助您轻松发送电子邮件。
v1.4
2015-09-07 12:35 UTC
Requires
- php: >=5.4.0
- phpmailer/phpmailer: 5.2.9
Requires (Dev)
- phpspec/phpspec: ~2.0
README
SunMailer 帮助您轻松发送电子邮件。
安装过程
只需将 SunMailer 文件夹复制到您的项目目录中某处。然后包括 SunMailer 自动加载器。
require_once('/path/to/SunMailer/autoload.php');
SunMailer 也可通过 Composer/Packagist 获取。
composer require sun/sunmailer
配置
打开位于 SunMailer/config.php 的 config.php 文件,然后:
设置您的 SMTP 服务器(默认设置为 Gmail SMTP)
'host' => 'smtp.gmail.com',
'port' => 465,
'encryption' => 'ssl',
设置您的用户名和密码
'username' => 'example@gmail.com',
'password' => 'secret',
您还可以添加“from”和“reply”,这将包括电子邮件和姓名。
'from' => [ 'email' => 'admin@example.com', 'name' => 'Administrator' ],
'reply' => [ 'email' => 'contact@example.com', 'name' => 'Information' ],
设置您的电子邮件视图目录。如果您没有添加视图目录的任何路径,则它指向根目录。
'view-directory' => 'app/view'
如果您想在本地测试电子邮件,请将日志设置为 true。(默认设置为 false)
'log' => false
发送基本电子邮件
// namespace use SunMailer\Mailer; use SunMailer\View; use SunMailer\MailerException; $email = 'example@gmail.com'; $name = 'Test mail name'; $subject = 'Test mail subject'; $body = 'Test mail body'; try { if(Mailer::send($email, $name, $subject, $body)) { echo 'Email has been sent successfully.'; } } catch (MailerException $e) { echo 'Oops!!! Something goes to wrong. '. $e->getMessage(); }
带附件的电子邮件
$attached = 'images/sunmailer.jpg'; if(Mailer::send($email, $name, $subject, $body, $attached)) { echo 'Email has been sent successfully.'; }
使用视图渲染发送电子邮件
视图类的 render() 方法可以帮助您渲染 HTML outlook。
View::render('to.path.test');
对于指向文件路径,请使用(.)或(/),并添加不带(.php)扩展名的文件名。
$body = View::render('email.test'); if(Mailer::send($email, $name, $subject, $body)) { echo 'Email has been sent successfully.'; }
您还可以通过 render() 方法的第二个参数传递任何值到视图模板(默认设置为 null)。
$data = [ 'name' => 'Test Name' ]; View::render('email.test', $data);
您需要在视图模板中添加一个占位符(在变量名开头添加 @)来获取这些数据。以下是一个示例。
@name
清理日志目录
Mailer::logClean();
一些辅助函数
// to get configuration file Helper::config(); // to get root directory path Helper::root_path(); // to get log directory path Helper::log_path(); // to get temp directory path Helper::temp_path();
许可证
此软件包根据 MIT 许可证 许可。