gelembjuk / mail
使用Smarty或Twig从模板创建邮件正文和主题,并使用支持的邮件发送器(PHPMailer、mail()函数等)发送邮件
1.0.1
2015-09-14 14:18 UTC
Requires
- gelembjuk/logger: 1.*
- gelembjuk/templating: 1.*
This package is not auto-updated.
Last update: 2024-10-02 10:26:07 UTC
README
PHP包,用于不修改PHP代码的情况下使用不同邮件发送器发送邮件,并使用模板器(Smarty、Twig)格式化邮件消息。
安装
使用composer: gelembjuk/mail require: {"gelembjuk/mail": "*"}
配置
$formatteroptions = array( 'locale' => '', // optional, add if you need international support 'deflocale' => 'en', // default locale. Optional as well, template from it are used if no in a `locale` 'templateprocessorclass' => null, // if not provided then smarty is used. this is one of classes from Gelembjuk/Templating 'templatecompiledir' => $thisdir.'/email_tmp/', // directory used to store tempopary files of cache engine . It i reuired for Smarty but not needed for Twig 'templatespath' => $thisdir.'/email_templates/' // a base path where email templates and files with subjects are stored ); $maileroptions = array( 'logger' => $logger, // (optional) Logger, instance of Gelembjuk\Logger\FileLogger, 'format' => $formatteroptions, // formatter options // other options related to specified email sending class // next options are only for PHPMailer 'mailsystem' => 'smtp', // for phpmailer it can be smtp or mail 'smtp_host' => 'smtp_host', // aka smtp.gmail.com 'smtp_port' => 25, // aka 587 'smtp_secure' => false, // or true in case of ssl/tls 'smtp_auth' => true, // usually true 'smtp_user' => 'smtp user', // aka your gmail account 'smtp_password' => 'smtp password', // your smtp password (gmail etc) );
使用方法
$mailer = new \Gelembjuk\Mail\PHPMailer(); // OR // $mailer = new \Gelembjuk\Mail\PHPNative(); // uses mail() // OR // $mailer = new \Gelembjuk\Mail\NullMail(); // is only for testing, doesn't send only log $mailer->initMailer($maileroptions); $email_data = array( 'user' => 'John Smith', 'activationlink' => 'http://our_site.com/activateaccount/code' ); $mailer->formatAndSendEmail( 'activate', // template name $email_data, 'john_smith@gmail.com', // send to email 'from_email@gmail.com' // send from email );
模板
在此示例中,templatespath
必须包含3个文件
activate.htm - 此电子邮件的模板
<h3>Hello {$user}</h3> <p>Click the link <a href="{$activationlink}">{$activationlink}</a></p>
out_default.htm - 通用out
模板,用于所有电子邮件(即所有电子邮件的通用头部/底部)
<table> <tr><td>Header. Company name, logo etc</td></tr> <tr><td>{$EMAILCONTENT}</td></tr> <tr><td>Footer. Contacts, signature etc</td></tr> </table>
subjects.xml - 包含模板的电子邮件主题文本
<?xml version="1.0" encoding="utf-8"?> <templates> <activate> <subject>Activate your new account</subject> <description>This email is sent to a user when new account is registered</description> </activate> </templates>
不同的模板引擎
现在可以使用Smarty和Twig。更多信息请参阅https://github.com/Gelembjuk/templating。将来可以添加更多支持的引擎。
国际化
请参阅此项目示例文件夹中的test2.php。它显示了如何组织本地文件夹的结构。
// send email with german template $mailer->setFormatterOption('locale','de'); $mailer->formatAndSendEmail( 'activate', // template name $email_data, 'john_smith@gmail.com', // send to email 'from_email@gmail.com' // send from email ); // send same email with french template $mailer->setFormatterOption('locale','fr'); $mailer->formatAndSendEmail( 'activate', // template name $email_data, 'john_smith@gmail.com', // send to email 'from_email@gmail.com' // send from email );
作者
Roman Gelembjuk (@gelembjuk)