数据库:
CREATE TABLE `zhe_code` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL COMMENT '邮箱地址或者电话号码',
`code` int(11) DEFAULT NULL,
`type` int(2) DEFAULT '1' COMMENT '`1email 2phone`',
`status` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT '1已发送,2已使用',
`delete_time` int(11) DEFAULT NULL,
`create_time` int(11) DEFAULT NULL,
`update_time` int(11) DEFAULT NULL,
`use_time` int(11) DEFAULT NULL COMMENT '使用时间',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
代码:
//邮件发送 - 验证码类型
public static function send($toUser=['571031767@qq.com'])
{
// 创建Transport对象,设置邮件服务器和端口号,并设置用户名和密码以供验证
$transport = (new \Swift_SmtpTransport('smtp.mxhichina.com', 465,"ssl"))
->setUsername(config('email.username'))
->setPassword(config('email.password'));
// 创建mailer对象
$mailer = new \Swift_Mailer($transport);
$code = rand(100000,999999);//生成一个数字 6位的
// Create a message
$message = (new \Swift_Message('网站验证码:'))
->setFrom([config('email.username') => config('email.name')])
->setTo($toUser)
->setBody("米醋儿网验证码:{$code},若不是本人操作请忽略本邮件。");
// Send the message
$result = $mailer->send($message);
if($result){
$data["name"] = $toUser[1];
$data["code"] = $code;
$data["type"] = 1;//1邮箱 2电话
Code::create($data);//保存数据库
}
return $result;
}
邮件服务器使用的是阿里云的企业邮箱。腾讯云和其它云应该都有企业邮箱的吧,买域名赠送的。