铀伽/yii2-manual-sendmail-command

从命令行发送邮件给Yii框架用户!

v2.5 2016-07-21 10:06 UTC

This package is not auto-updated.

Last update: 2024-09-18 18:36:57 UTC


README

需求

Yii2 PHP框架 Yii2 Swiftmailler 扩展

通过Composer下载

composer require uguranyum/yii2-manual-sendmail-command

迁移到数据库

yii migrate --migrationPath=@vendor/uguranyum/yii2-manual-sendmail-command/migration --interactive=0

使用扩展

此扩展需要您将邮件列表导入到'mail_list'表中。列包括:'email'、'special_id'、'template'、'json_values' 和 'create_date'。

例如,您想重置用户密码

$users      = Yii::$app->db2->createCommand('SELECT * FROM `users`')->queryAll();

foreach($users as $user)
{
    $sendmail       =   new Sendmail();
    $email          =   $user['email'];
    $special_id     =   $user['id'];
    $template       =   10;             // Your template number. You can make false...
    $create_date    =   time();         // Created user date.
    $password       =   rand(9999999,99999999999); // Create simple random password

    $verification_code    = $sendmail->generateVerificationCode(10); //if you need just help.

    Yii::$app->db2->createCommand()->update('users', [
            'password'          =>  md5($password),              //new md5 password
            'verification_code' =>  $verification_code,          //new verification code
        ], 'id = '.$user['id'])->execute();

    $json_values    =   json_encode(['username' => $user['username'], 'password' => $password ]);

    $sendmail->saveDb($email, $special_id, $template, $json_values, $create_date);
}

此代码是导入带有新密码的用户。

使用swiftmailler:在您的应用程序配置中添加以下代码

return [
    //....
    'components' => [
        'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
        ],
    ],
];

配置邮件设置:在common/main-local.php的components部分中

'mail' => [
        'class' => 'yii\swiftmailer\Mailer',
        'viewPath' => '@backend/mail',
        'useFileTransport' => false,//set this property to false to send mails to real email addresses
        //comment the following array to send mail using php's mail function
        'transport' => [
            'class' => 'Swift_SmtpTransport',
            'host' => 'smtp.gmail.com',
            'username' => 'username@gmail.com',
            'password' => 'password',
            'port' => '587',
            'encryption' => 'tls',
                        ],
    ],
],

使用示例模板

path\vendor\uguranyum\yii2-manual-sendmail-command\stemplate\alltemplates.php (all template documents)
Copy to:
path\common\mail\

如何使用

您可以在控制器中这样使用扩展

    $sendmail       = new Sendmail();
    $this->saveDb();
    $sendmail->run();
    //$sendmail->turnicateMailList();