yii框架中关于邮箱封装的示例代码分享
<?php
class mailer
{
private static $obj;
private static $config;
public static function getmailer()
{
if (!is_object(self::$obj)) {
self::$config = [
'class' => 'swift_smtptransport',
'host' => 'smtp.163.com',
'username' => 'xxx@163.com',
'password' => 'xxx',
'port' => '994',
'encryption' => 'ssl', //ssl tls
];
self::$obj = \yii::createobject([
'class' => 'yii\swiftmailer\mailer',
'viewpath' => '@common/mail',
'usefiletransport' => false,
'transport' => self::$config,
]);
}
return self::$obj;
}
public static function send($toemail, $subject, array $compose)
{
$user = \wskm::getuser();
if ($compose) {
//同时设置2种内容,让用户的偏好自己选择
self::getmailer()->compose(
//['html' => 'passwordresettoken-html', 'text' => 'passwordresettoken-text'], ['user' => $user]
//['html' => 'passwordresettoken-html'], ['user' => $user]
$compose
);
}else{
self::getmailer()->setbody('my <em>amazing</em> body', 'text/html');
self::getmailer()->addpart('my amazing body in plain text', 'text/plain');
}
//https://swiftmailer.symfony.com/docs/messages.html
//addto addcc addbcc
//$message->setto(['some@address.tld', 'other@address.tld']);
//$message->setcc([
// 'person1@example.org', 'person2@otherdomain.org' => 'person 2 name',
//]);
//->attach(swift_attachment::frompath('my-document.pdf')->setfilename('cool.jpg'))
/*
// create the message
$message = new swift_message('my subject');
// set the body
$message->setbody(
'<html>' .
' <body>' .
' here is an image <img src="' . // embed the file
$message->embed(swift_image::frompath('image.png')) .
'" alt="image" />' .
' rest of message' .
' </body>' .
'</html>',
'text/html' // mark the content-type as html
);
*/
/*
* 验证
use egulias\emailvalidator\emailvalidator;
use egulias\emailvalidator\validation\rfcvalidation;
$validator = new emailvalidator();
$validator->isvalid("example@example.com", new rfcvalidation());
*/
/*
* 加密
$smimesigner = new swift_signers_smimesigner();
$smimesigner->setsigncertificate('/path/to/certificate.pem', ['/path/to/private-key.pem', 'passphrase']);
$message->attachsigner($smimesigner);
*/
/*
* 回执
$message->setreadreceiptto('你@地址。 tld ');
*/
/**
* ->setcharset('iso-8859-2'); 编码
* ->setpriority(2); 设置优先级,1-5
*/
return self::getmailer()->compose(
//['html' => 'passwordresettoken-html', 'text' => 'passwordresettoken-text'], ['user' => $user]
['html' => 'passwordresettoken-html'], ['user' => $user]
)
->setfrom([ self::$config['username'] => 'test robot'])
->setto($toemail)
->setsubject($subject)
->send();
}
}
以上就是yii框架中关于邮箱封装的示例代码分享的详细内容。