PHP code example of cyzonetech / tp-mailer
1. Go to this page and download the library: Download cyzonetech/tp-mailer library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once ('vendor/autoload.php' );
cyzonetech / tp-mailer example snippets
use mailer \tp31 \Mailer ;
$mailer = Mailer::instance();
$mailer->send(function ($mailer, $message) {
$mailer->to('tianpian0805@gmail.com' )
->subject('使用框架模板引擎渲染模板测试' )
->view('mail:test' , array (
'param1' => '参数1' ,
'param2' => '参数2' ,
'param3' => '参数3'
));
});
public static function write ($content, $level = 'debug' )
{
echo '日志内容:' . $content;
echo '日志级别:' . $level;
}
$mailer = Mailer::instance(function () {
return \Swift_SmtpTransport::newInstance(
'host' , 'port' , 'security'
)
->setUsername($config['addr' ])
->setPassword($config['pass' ]);
});
Mailer::instance()
->to('tianpian0805@gmail.com' , 'yuan1994' )
->subject('测试邮件模板中嵌入图片元数据' )
->html('<img src="{image}" />图片测试' , [
'embed:image' => ROOT_PATH . 'image.jpg' ,
])
->send();
$mailer->attach(ROOT_PATH . 'foo.ext' , function ($attachment, $mailer) {
$attachment->setFilename($mailer->cnEncode('文件名.jpg' ));
});
$mailer->send(function ($mailer, $message) {
$mailer->to('tianpian0805@gmail.com' )
->line('你好' )
->line('这是一封测试邮件' )
->subject('测试邮件' );
});
$mailer->send(
function ($mailer, $message) {
$mailer->to('tianpian0805@gmail.com' )
->line('你好' )
->line('这是一封测试邮件' )
->subject('测试邮件' );
},
function () {
return \Swift_SmtpTransport::newInstance(
'host' , 'port' , 'security'
)
->setUsername($config['addr' ])
->setPassword($config['pass' ]);
});
$mailer->send(
function ($mailer, $message) {
$mailer->to('tianpian0805@gmail.com' )
->line('你好' )
->line('这是一封测试邮件' )
->subject('测试邮件' );
},
null ,
function ($swiftMailer, $mailer) use ($lotsOfRecipients {
// Continue sending as normal
for ($lotsOfRecipients as $recipient) {
// $swiftMailer->send($mailer->message, $fails) ;
$swiftMailer->send($mailer->message) ;
}
}) ;
class Config
{
public static function init ($config = [])
{
}
public static function get ($name = null, $default = null)
{
}
public static function set ($name, $value)
{
}
}
use mailer \lib \Mailer
use think \View
Mailer ::addMethod ('view ', function ($template , $param = [], $config = [])
{
$view = View ::instance (Config ::get ('template '), Config ::get ('view_replace_str '));
foreach ($param as $k => $v) {
$this ->embedImage($k, $v, $param);
}
$content = $view->fetch($template, $param, [], $config);
return $this ->html($content);
});
$ret = Mailer::instance()
->to('tianpian0805@gmail.com' )
->subject('测试邮件' )
->view('index@mail/index' , [
'date' => date('Y-m-d H:i:s' ),
])
->send();
public function view ($template, $param = [], $config = [])
{
$view = View::instance(ThinkConfig::get('template' ), ThinkConfig::get('view_replace_str' ));
foreach ($param as $k => $v) {
$this ->embedImage($k, $v, $param);
}
$content = $view->fetch($template, $param, [], $config);
return $this ->html($content);
}
use mailer \lib \Config
use mailer \lib \Mailer
// 第一步:初始化配置项
// 配置格式参见前面的配置
$config = [
'driver ' => 'smtp ',
'host ' => 'smtp .qq .com ',
...
];
Config::init($config);
Mailer::addMethod('view' , function ($template, $param = [], $config = [])
{
$view = \think\View::instance(Config::get('template' ), Config::get('view_replace_str' ));
foreach ($param as $k => $v) {
$this ->embedImage($k, $v, $param);
}
$content = $view->fetch($template, $param, [], $config);
return $this ->html($content);
});
$ret = Mailer::instance()
->to('tianpian0805@gmail.com' )
->subject('测试邮件' )
->view('index@mail/index' , [
'date' => date('Y-m-d H:i:s' ),
])
->send();
$ret = Mailer::instance()
->to('tianpian0805@gmail.com' )
->subject('测试邮件' )
->html('<img src="{image}"/>图片测试' , [
'embed:image' => 'http://image34.360doc.com/DownloadImg/2011/08/2222/16275597_64.jpg'
])
->send();