SendVerificationMessage.php
3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
namespace App\Jobs;
use App\Mail\SendVerifyCode;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Redis;
class SendVerificationMessage implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    public $email;
    /**
     * Create a new job instance.
     * @param $email
     * @return void
     */
    public function __construct($email)
    {
        //
        $this->email = $email;
    }
    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $code = mt_rand(100000, 999999);
        $redis = Redis::connection();
        $redis->setex($this->email,1800,$code);  //过期时间30分钟
        /*$api      = 'http://api.sendcloud.net/apiv2/mail/sendtemplate';
        $API_USER = 'mofunsky_noreply';
        $API_KEY  = '8EkR0XnMuJn6V5yQ';
        $from     = 'noreply@yiyan.pub';
        $vars = json_encode( array("to" => array($this->email),
                "sub" => array("%code%" => Array($code))
            )
        );
        $param = array(
            'apiUser' => $API_USER, // 使用api_user和api_key进行验证
            'apiKey' => $API_KEY,
            'from' => $from, // 发信人,用正确邮件地址替代
            'fromName' => 'Parlando',
            'xsmtpapi' => $vars,
            'templateInvokeName' => 'parlando_mail_verify',
            'subject' => '[Parlando] Register Verify',
            'respEmailId' => 'true'
        );
        $data = http_build_query($param);
        $options = array(
            'http' => array(
                'method' => 'POST',
                'header' => 'Content-Type: application/x-www-form-urlencoded',
                'content' => $data
            ));
        $context     = stream_context_create($options);
        $result_json = file_get_contents($api, FILE_TEXT, $context);
        $result = json_decode($result_json,true);
        if ($result['result'] == false){
            Log::channel('daily')->debug($result_json);
            // 用开发模板发送。
            Mail::to($this->email)->send(new SendVerifyCode($code));
        }*/
        $email = new \SendGrid\Mail\Mail();
        $email->setFrom("hello@parlando.ink", "Parlando");
        $email->setSubject("Verification Code");
        $email->addTo($this->email, $this->email);
        $email->addContent(
            "text/html", "Your verification code is: <strong>$code</strong><br/>(The code will expire in 30 minutes.)"
        );
        $sendgrid = new \SendGrid(env('SENDGRID_API_KEY'));
        try {
            $sendgrid->send($email);
        } catch (\Exception $e) {
            Log::channel("daily")->error("send email error :".$e->getMessage());
        }
    }
}