腾讯企业邮箱:创建邮件授权码

文章目录

  • 申请官网
  • 绑定
  • 生成密码
  • PHP脚本发送代码(465端口)
  • Yii框架发送代码(587端口)

申请官网

腾讯企业邮箱

绑定

腾讯企业邮箱:创建邮件授权码_第1张图片

生成密码

腾讯企业邮箱:创建邮件授权码_第2张图片

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 = '[email protected]';                     // SMTP username
    $mail->Password = '******';                     // SMTP password, 授权码
    $mail->setFrom('[email protected]', '*****');
    $mail->addAddress('[email protected]', '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' => '[email protected]',
                '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 = '我就试试!
'
, $mail_to = '[email protected]') { $mail = \Yii::$app->mailer->compose(); $mail->setFrom('[email protected]'); $mail->setTo($mail_to); $mail->setSubject('一番开发bug提醒'); $mail->setHtmlBody($message); try { $mail->send(); }catch (\Exception $e){ echo $e->getMessage(); } } }

你可能感兴趣的:(问题记录,php,开发语言,后端)