一、安装mail composer包
这里使用的是 Packagist 上的一个邮件包 nette/mail
composer require nette/mail
二、注册一个网易126邮箱,然后开启smtp服务
这里需要开启相关服务,并且设置 smpt独立密码

三、利用代码实现发送邮件
use Nette\Mail\Message;
use Nette\Mail\SmtpMailer;
trait MailTraits
{
public static function sendEmail($userEmail, $title, $content)
{
$mail = new Message();
try {
$mail->setFrom('Mr.Chen <cxbbest@126.com>')//这里的邮箱需要与发送者邮箱一致
->addTo($userEmail)
->setSubject($title)
//->adAttachment('file.zip');//可以将附近添加到邮件当中
//->setHtmlBody($content);//支持HTML代码内容
->setBody($content);//普通文本内容
$mailer = new SmtpMailer([
'host' => 'smtp.126.com',
'username' => 'cxbbest@126.com',
'password' => 'CXB***', /* smtp独立密码 */
'secure' => 'ssl',
]);
$mailer->send($mail);
} catch (\Exception $e) {
return false;
}
return true;
}
}
四、发送邮件,效果图
