Skip to content

Instantly share code, notes, and snippets.

@JellyBool
Last active April 28, 2022 01:11
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save JellyBool/9a518192c8ce6f325b8fcb09c93cdaac to your computer and use it in GitHub Desktop.
Save JellyBool/9a518192c8ce6f325b8fcb09c93cdaac to your computer and use it in GitHub Desktop.
SendCloud Email Version_2
<?php
namespace App\Mailer;
/**
* Class QQMailer
* @package App\Mailer
*/
class Mailer {
protected $url = 'http://api.sendcloud.net/apiv2/mail/sendtemplate';
/**
* @param $email
* @param $token
* @return string
*/
protected function sendTo($user, $subject, $view, $data = [])
{
$vars = json_encode(['to' => [$user->email], 'sub' => $data]);
$param = [
'apiUser' => env('SENDCLOUD_API_USER'), # 使用api_user和api_key进行验证
'apiKey' => env('SENDCLOUD_API_KEY'),
'from' => config('mail')['from']['address'], # 发信人,用正确邮件地址替代
'fromName' => config('mail')['from']['name'],
'xsmtpapi' => $vars,
'subject' => $subject,
'templateInvokeName' => $view,
'respEmailId' => 'true'
];
$sendData = http_build_query($param);
$options = [
'http' => [
'method' => 'POST',
'header' => 'Content-Type: application/x-www-form-urlencoded',
'content' => $sendData
]];
$context = stream_context_create($options);
return file_get_contents($this->url, FILE_TEXT, $context);
}
public function welcome($user)
{
$subject = 'Laravist 邮箱确认';
$view = 'welcome';
$data = ['%name%' => [$user->name],'%token%' => [str_random(40)]];
$this->sendTo($user, $subject, $view, $data);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment