PHP code example of yuan1994 / tp-mailer
1. Go to this page and download the library: Download yuan1994/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');
/* Start to develop here. Best regards https://php-download.com/ */
yuan1994 / tp-mailer example snippets
use mailer\tp31\Mailer;
$mailer = Mailer::instance();
$mailer->send(function($mailer, $message) {
$mailer->to('[email protected] ')
->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('[email protected] ', 'yuan1994')
->subject('测试邮件模板中嵌入图片元数据')
->html('<img src="{image}" />图片测试', [
'embed:image' => ROOT_PATH . 'image.jpg',
// 'embed:image' => 'http://image34.360doc.com/DownloadImg/2011/08/2222/16275597_64.jpg',
// 'embed:image' => [file_get_contents(ROOT_PATH . 'image1.jpg')],
// 'embed:image' => [file_get_contents(ROOT_PATH . 'image1.jpg', 'image/png', '图片.png')],
])
->send();
$mailer->attach(ROOT_PATH . 'foo.ext', function($attachment, $mailer) {
$attachment->setFilename($mailer->cnEncode('文件名.jpg'));
});
$mailer->send(function ($mailer, $message) {
$mailer->to('[email protected] ')
->line('你好')
->line('这是一封测试邮件')
->subject('测试邮件');
});
$mailer->send(
function ($mailer, $message) {
$mailer->to('[email protected] ')
->line('你好')
->line('这是一封测试邮件')
->subject('测试邮件');
},
function () {
return \Swift_SmtpTransport::newInstance(
'host', 'port', 'security'
)
->setUsername($config['addr'])
->setPassword($config['pass']);
});
$mailer->send(
function ($mailer, $message) {
$mailer->to('[email protected] ')
->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
{
/**
* 初始化配置项
*
* @param array $config 请参考配置项里的配置格式,其他非ThinkPHP框架不支持自动探测自动初始化配置项,务必使用该方法初始化配置项
*/
public static function init($config = [])
{
}
/**
* 获取配置参数 为空则获取所有配置
*
* @param string $name 配置参数名
* @param mixed $default 默认值
*
* @return mixed
*/
public static function get($name = null, $default = null)
{
}
/**
* 设置配置参数
*
* @param string|array $name 配置参数名
* @param mixed $value 配置值
*/
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);
});
// 不用use mailer\tp5\Mailer,直接使用mailer\lib\Mailer调用view方法发送邮件
$ret = Mailer::instance()
->to('[email protected] ')
->subject('测试邮件')
->view('index@mail/index', [
'date' => date('Y-m-d H:i:s'),
])
->send();
/**
* 载入一个模板作为邮件内容
*
* @param string $template
* @param array $param
* @param array $config
*
* @return Mailer
*/
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);
// 第二步:注册view方法
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('[email protected] ')
->subject('测试邮件')
->view('index@mail/index', [
'date' => date('Y-m-d H:i:s'),
])
->send();
// 第三步 (2):如果你不需要使用view方法,可以忽略第二步
$ret = Mailer::instance()
->to('[email protected] ')
->subject('测试邮件')
->html('<img src="{image}"/>图片测试', [
'embed:image' => 'http://image34.360doc.com/DownloadImg/2011/08/2222/16275597_64.jpg'
])
->send();