燃料 / email
FuelPHP 1.x 邮件包
1.8.2
2019-06-27 14:58 UTC
Requires
- composer/installers: ~1.0
This package is auto-updated.
Last update: 2024-08-28 12:29:21 UTC
README
Fuel的完整电子邮件类。使用PHP的mail函数、sendmail或SMTP发送邮件。
摘要
- 使用mail、sendmail或SMTP发送纯文本/HTML,可选的替代纯文本/HTML正文。
- 添加附件,可以是普通或内联,也可以是字符串或文件。
- 为HTML正文自动添加内联文件附件。
- 可配置的附件路径。
用法
$mail = Email::forge();
$mail->from('me@domain.com', 'Your Name Here');
// Set to
$mail->to('mail@domain.com');
// Set with name
$mail->to('mail@domain.com', 'His/Her Name');
// Set as array
$mail->to(array(
// Without name
'mail@domain.com',
// With name
'mail@domain.com' => 'His/Her Name',
));
// Work the same for ->cc and ->bcc and ->reply_to
// Set a body message
$email->body('My email body');
// Set a html body message
$email->html_body(\View::forge('email/template', $email_data));
/**
By default this will also generate an alt body from the html,
and attach any inline files (not paths like http://...)
**/
// Set an alt body
$email->alt_body('This is my alt body, for non-html viewers.');
// Set a subject
$email->subject('This is the subject');
// Change the priority
$email->priority(\Email::P_HIGH);
// And send it
$result = $email->send();
异常
+ \EmailValidationFailedException, thrown when one or more email addresses doesn't pass validation
+ \EmailSendingFailedException, thrown when the driver failed to send the exception
示例
// Use the default config and change the driver
$email = \Email::forge('default', array('driver' => 'smtp'));
$email->subject('My Subject');
$email->html_body(\View::forge('email/template', $email_data));
$email->from('me@example.com', 'It's Me!');
$email->to('other@example.com', 'It's the Other!');
try
{
$email->send();
}
catch(\EmailValidationFailedException $e)
{
// The validation failed
}
catch(\EmailSendingFailedException $e)
{
// The driver could not send the email
}
优先级
以下可以是以下之一
+ \Email::P_LOWEST - 1 (lowest)
+ \Email::P_LOW - 2 (low)
+ \Email::P_NORMAL - 3 (normal) - this is the default
+ \Email::P_HIGH - 4 (high)
+ \Email::P_HIGHEST - 5 (highest)
附件
添加附件有多种方式
$email = Email::forge();
// Add an attachment
$email->attach(DOCROOT.'dir/my_img.png');
// Add an inline attachment
// Add a cid here to point to the html
$email->attach(DOCROOT.'dir/my_img.png', true, 'cid:my_conten_id');
您还可以添加字符串附件
$contents = file_get_contents($my_file);
$email->string_attach($contents, $filename);
默认情况下,HTML图像自动包含,但它只包含本地文件。查看以下HTML了解其工作原理。
// This is included
<img src="path/to/my/file.png" />
// This is not included
<img src="http://remote_host/file.jpeg" />
驱动程序
驱动程序允许使用该库发送邮件的任何东西。
Mailgun
Mailgun是Rackspace(http://www.mailgun.com/)的一项在线服务,允许您按需发送电子邮件。您需要在FuelPHP中安装mailgun库(https://github.com/mailgun/mailgun-php),使用composer。
安装完包后,您需要设置应用的配置
<?php return array( /** * Override default Email.php settings */ 'defaults' => array( 'driver' => 'mailgun', 'mailgun' => array( 'key' => 'YOUR KEY', 'domain' => 'YOUR DOMAIN', 'endpoint' => null | 'OPTIONAL ALT. API ENDPOINT URL' // e.g. 'https://api.eu.mailgun.net/v3' ), ), );