yjballestero/yii2-phpmailer

Yii2 的 PHPMailer 适配器

安装: 38

依赖: 0

建议者: 0

安全: 0

星标: 1

关注者: 1

分支: 1

开放问题: 0

类型:yii2-extension

dev-master 2022-05-28 21:39 UTC

This package is auto-updated.

Last update: 2024-09-29 03:10:22 UTC


README

使用 PHPMailer 作为传输的 Yii2 邮件服务。

与标准 SwiftMailer 不同,它支持使用 php mail 函数发送邮件。

要求

此库使用

  • PHP 8.0+。
  • Yii2 2.0.39+

安装

建议您通过 composer 安装 PHP Browser 库 (点击这里)。为此,请运行以下命令

composer require yjballestero/yii2-phpmailer

或者将此行添加到您的 composer.json 文件中

"yjballestero/yii2-phpmailer": "dev-master"

设置

 $config = [
     'components' => [
        'mailer' => [
            'class' => yjballestero\phpmailer\PHPMailerMailer::class,            
            // config \PHPMailer\PHPMailer\PHPMailer
            'transportConfig' => [
                'Mailer'     => 'smtp', //Send using SMTP
                'CharSet'    => CHARSET, //us-ascii, iso-8859-1, utf-8
                'Encoding'   => ENCODING, //7bit, 8bit, base64, binary, quoted-printable
                'Host'       => 'smtp.example.com', //Set the SMTP server to send through
                'Username'   => 'user@example.com', //SMTP username
                'Password'   => 'secret', //SMTP password
                'Port'       => MAIL_PORT, //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
                'SMTPSecure' => SMTP_ENCRYPT, //TLS, SSL
                'SMTPAuth'   => true, //Enable SMTP authentication
            ],
            
            // default message config
            'messageConfig' => [
                'from' => FROM
            ]
        ]
    ]
];

使用示例

public function sendEmail() {
    $to = 'test@example.com';
    $title = 'test';
    $subject = 'test email';
    $message = 'Hello world';
    
    $email = Yii::$app->mailer->compose(['content'=>$message, 'title'=>$title])
                              ->setTo($to)
                              ->setSubject($subject);
    if($email->send()){
        return 'Message has been sent';
    }
    return $email->mailer->adapter->ErrorInfo;
}