定制 jlorente/laravel-sendgrid 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

jlorente/laravel-sendgrid

最新稳定版本:1.0.4

Composer 安装命令:

composer require jlorente/laravel-sendgrid

包简介

Laravel integration for the SendGrid php sdk.

README 文档

README

Laravel integration for the SendGrid PHP SDK including a notification channel.

Installation

The preferred way to install this extension is through composer.

With Composer installed, you can then install the extension using the following commands:

$ php composer.phar require jlorente/laravel-sendgrid

or add

...
    "require": {
        "jlorente/laravel-sendgrid": "*"
    }

to the require section of your composer.json file.

Configuration

  1. Register the ServiceProvider in your config/app.php service provider list.

config/app.php

return [
    //other stuff
    'providers' => [
        //other stuff
        \Jlorente\Laravel\SendGrid\SendGridServiceProvider::class,
    ];
];
  1. Add the following facade to the $aliases section.

config/app.php

return [
    //other stuff
    'aliases' => [
        //other stuff
        'SendGrid' => \Jlorente\Laravel\SendGrid\Facades\SendGrid::class,
    ];
];
  1. Publish the package configuration file.
$ php artisan vendor:publish --provider='Jlorente\Laravel\SendGrid\SendGridServiceProvider'
  1. Set the api_key in the config/sendgrid.php file or use the predefined env variables.

config/sendgrid.php

return [
    'api_key' => 'YOUR_API_KEY',
    //other configuration
];

or .env

//other configurations
SENDGRID_API_KEY=<YOUR_API_KEY>

Usage

You can use the facade alias SendGrid to execute api calls. The authentication params will be automaticaly injected.

SendGrid::send($mail);

Notification Channels

A notification channel is included in this package and allow you to integrate the SendGrid send email service.

You can find more info about Laravel notifications in this page.

SendGridEmailChannel

If you want to send an email through SendGrid, you should define a toSendGrid method on the notification class. This method will receive a $notifiable entity and should return a \SendGrid\Mail\Mail instance.

/**
 * Get the Mail object instance.
 *
 * @param  mixed  $notifiable
 * @return \SendGrid\Mail\Mail
 */
public function toSendGrid($notifiable)
{
    $mail = new \SendGrid\Mail\Mail();
    $mail->setFrom();
    $mail->setTemplateId('d-4n23blaasjdgdg3242');
    $mail->addDynamicTemplateData('username', 'John');

    return $mail;
}

Once done, you must add the notification channel in the array of the via() method of the notification:

/**
 * Get the notification channels.
 *
 * @param  mixed  $notifiable
 * @return array|string
 */
public function via($notifiable)
{
    return [SendGridEmailChannel::class];
}

Routing the Notifications

When sending notifications via SendGrid channel, the notification system will automatically look for an email attribute on the notifiable entity. If you would like to customize the number you should define a routeNotificationForSendGrid method on the entity:

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * Route notifications for the SendGrid SMS channel.
     *
     * @param  \Illuminate\Notifications\Notification  $notification
     * @return string
     */
    public function routeNotificationForSendGrid($notification)
    {
        return $this->email;
    }
}

License

Copyright © 2021 José Lorente Martín jose.lorente.martin@gmail.com.

Licensed under the BSD 3-Clause License. See LICENSE.txt for details.

统计信息

  • 总下载量: 4.05k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2021-06-16

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固