申请官网
绑定
生成密码
PHP脚本发送代码(465端口)
require_once __DIR__ . "/vendor/autoload.php";
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
...
$mail = new PHPMailer(true);
try {
$mail->SMTPDebug = SMTP::DEBUG_CLIENT; // Enable verbose debug output
$mail->isSMTP(); // Send using SMTP
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` also accepted
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Host = 'smtp.exmail.qq.com'; // Set the SMTP server to send through
$mail->Port = 465; // TCP port to connect to , 465
$mail->CharSet = "UTF-8";
$mail->FromName = "lvpeilin";
$mail->Username = 'lvpeilin@dyspace.net'; // SMTP username
$mail->Password = '******'; // SMTP password, 授权码
$mail->setFrom('lvpeilin@dyspace.net', '*****');
$mail->addAddress('lvpeilin@dyspace.net', 'lvpeilin'); // Add a recipient
$mail->isHTML(false); // Set email format to HTML
$mail->Subject = '一番赏';
$mail->Body = '一番赏有新的已支付订单';
$mail->send();
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
Yii框架发送代码(587端口)
1.配置 components
:
'components' => [
...
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'useFileTransport' => false, // must have
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.exmail.qq.com', //每种邮箱的host配置不一样
'username' => 'lvpeilin@dyspace.net',
'password' => '*****************',
'port' => '587',
'encryption' => 'tls',
'timeout' => 5,
'StreamOptions' => ['tls' => ['allow_self_signed' => true, 'verify_peer' => false]],
],
'messageConfig' => [
'charset' => 'UTF-8',
],
],
...
]
2.发送代码
namespace app\common;
class MailUtils
{
public static function mailDevelop($message = '我就试试!<br>', $mail_to = 'lvpeilin@dyspace.net')
{
$mail = \Yii::$app->mailer->compose();
$mail->setFrom('lvpeilin@dyspace.net');
$mail->setTo($mail_to);
$mail->setSubject('一番开发bug提醒');
$mail->setHtmlBody($message);
try {
$mail->send();
}catch (\Exception $e){
echo $e->getMessage();
}
}
}