SendVerificationMessage.php 3.02 KB
<?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());
        }


    }
}