Laravel 5.1 的 SendCloud 驱动

由于公司项目需要用到发送邮件模块,但是 Laravel 自带的都是国外的服务,所以写了个 SendCloud 的驱动。

GitHub: https://github.com/NauxLiu/Laravel-SendClo...

有问题欢迎反馈, 同时欢迎 star :rose:

优点:

普通发送方式完全兼容官方用法,可随时修改配置文件改为其他驱动,而不需要改动代码

安装

在项目目录下执行

composer require naux/sendcloud

配置

修改 config/app.php

'providers' => [
    // 添加这行
    Naux\Mail\ SendCloudServiceProvider::class,
];

.env 中配置你的密钥, 并修改邮件驱动为 sendcloud

MAIL_DRIVER=sendcloud

SEND_CLOUD_USER=   # 创建的 api_user
SEND_CLOUD_KEY=    # 分配的 api_key

使用

普通发送:

用法完全和系统自带的一样, 具体请参照官方文档: http://learnku.com/docs/laravel/5.1/mail

Mail::send('emails.welcome', $data, function ($message) {
    $message->from('us@example.com', 'Laravel');

    $message->to('foo@example.com')->cc('bar@example.com');
});

模板发送

用法和普通发送类似,不过需要将 body 设置为 SendCloudTemplate 对象,达到目的有几种方法

第一种用法:
Mail::send('随便传个空view', [], function ($message) {
    $message->from('us@example.com', 'Laravel');

    $message->to('foo@example.com')->cc('bar@example.com');

    // 模板变量
    $bind_data = ['url' => 'http://naux.me'];
    $template = new SendCloudTemplate('模板名', $bind_data);

    $message->getSwiftMessage()->setBody($template);
});
第二种用法:
// 模板变量
$bind_data = ['url' => 'http://naux.me'];
$template = new SendCloudTemplate('模板名', $bind_data);

Mail::raw($template, function ($message) {
    $message->from('us@example.com', 'Laravel');

    $message->to('foo@example.com')->cc('bar@example.com');
});
其他用法:

看了上面两种用法,其他用法对照官方文档也能猜出来了吧,如使用 queue 发送等 ~

本帖已被设为精华帖!
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 19
Summer

啥时候偷偷整了个这个

8年前 评论
Summer

@NauxLiu 牛逼死

8年前 评论
Destiny

@NauxLiu

 SEND_CLOUD_USER=   # 创建的 api_user
SEND_CLOUD_KEY=    # 分配的 api_key

这是什么邮箱帐号密码??

7年前 评论

请问一下对php版本的要求是什么呢?

7年前 评论

@NauxLiu

file

请问这是什么情况?该怎么解决呢?

7年前 评论

@mrq7514 记错了,,php 版本需要 5.6 以上

7年前 评论
betaban 1年前
Junting

使用SendCloud ,如何拿到返回信息?发送成功后返回的是 null!

7年前 评论

@Twitchboy 成功了就是返回 null, 失败了会抛出异常。

7年前 评论

@Twitchboy 刚更新了包,版本 1.1.1 发送成功会返回 true, 并修复了 bug,可以更新下。

7年前 评论
Junting

@NauxLiu 666!

7年前 评论

Swift_TransportException in AbstractSmtpTransport.php line 383:
Expected response code 250 but got code "530", with message "530 5.7.1 Authentication required
"
这是什么原因

7年前 评论

@王白水 检查你的 sendcloud 配置

7年前 评论
wanghan

@NauxLiu SEND_CLOUD_USER和SEND_CLOUD_KEY是啥。。

4年前 评论
晓鹤 4年前

你好,我使用composer安装失败

Your requirements could not be resolved to an installable set of packages. 您的要求无法解析为可安装的包集

3年前 评论

laravel7.*安装失败
file

3年前 评论

@NauxLiu 您好,能否更新一下版本号,支持laravel9,谢谢

1年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!