pepipost / pepipost-laravel-driver

此库为Laravel添加了'pepipost'邮件驱动。

v3.0.0 2022-09-02 10:20 UTC

This package is not auto-updated.

Last update: 2024-09-25 19:37:15 UTC


README

pepipostlogo

Packagist Packagist Packagist Open Source Helpers Twitter Follow

Laravel的Pepipost驱动

支持Pepipost Send Email Web API的邮件驱动,使用原始的Laravel API。此库扩展了原始的Laravel类,因此使用完全相同的方法。

使用此包需要您的Pepipost API密钥。请在此处设置

我们正在努力使我们的库由社区驱动 - 这意味着我们需要您的帮助来正确地构建正确的东西,我们请求您通过分享评论、创建新的问题拉取请求来帮助我们。

我们欢迎任何形式的对此库的贡献。

此库的最新3.0.0版本完全兼容最新的Pepipost v5.1 API。

有关此库的任何更新,请检查发行版

目录

安装

先决条件

PHP >= 8.0

Composer v2.3.0

Laravel >= 9.x

Illuminate Mail ^9.0

Guzzle ^7.2

在Pepipost上拥有免费账户。如果您还没有,请点击此处注册。

用法

配置laravel项目

步骤1 - 创建新的Laravel项目

laravel new testproject

步骤2 - 将包添加到composer.json中,并运行composer update。

"require": {
    "pepipost/pepipost-laravel-driver": "~3.0.0"
},

或使用composer安装

$ composer require pepipost/pepipost-laravel-driver

步骤3 - 配置

  1. 在config/services.php中添加pepipost api密钥和端点

    'pepipost' => [
        'api_key' => env('PEPIPOST_API_KEY'),
    ],
    Endpoint config:
    If you need to set custom endpoint, you can set any endpoint by using endpoint key.
    For example,calls to Pepipost Web API through a proxy,configure endpoint in config/services.php.
    
      'pepipost' => [
          'api_key' => env('PEPIPOST_API_KEY'),
          'endpoint' => 'https://api.pepipost.com/v5/mail/send',
      ],
  2. 在.env文件中添加以下内容

    MAIL_MAILER=pepipost # Needed to send through pepipipost api
    PEPIPOST_API_KEY='YOUR_PEPIPOST_API_KEY'
  3. 在config/mail.php中添加以下内容

    'mailers' => [
      'pepipost' => [
          'transport' => 'pepipost',
      ],
    ],

步骤4 - Laravel步骤创建控制器和视图

  1. 定义控制器

    php artisan make:controller TestController
  2. 更新控制器,在TestController中包含以下函数sendMail

    <?php
      namespace App\Http\Controllers;
      use Illuminate\Support\Facades\Mail;
      use App\Mail\TestEmail;
      
      use Illuminate\Http\Request;
      
      class TestController extends Controller
      {
      	function sendMail(){
      		try {
      			Mail::to('mail.recipient@gmail.com')
      			->send(new TestEmail(['message' => 'Just a test message']));
      			return 'Email sent successfully';
      		}
      		catch (Exception $e) {
      			echo $e->getResponse();
      		}
      	}
      }
  3. 在resources/views/mailtemplates/test.blade.php中创建文件并包含您的电子邮件内容

    <!DOCTYPE html>
    <html lang="en-US">
      <head>
        <meta charset="utf-8" />
      </head>
      <body>
        <h2>Test Email</h2>
        <p>{{ $test_message }}</p>
      </body>
    </html>
  4. 在routes/web.php中创建新的路由

    Route::get('/send/email', [TestController::class, 'sendMail'])->name('sendEmail');
  5. 创建邮件模板

    php artisan make:mail TestEmail

    此命令将在app/Mail/TestEmail.php下创建新文件

  6. 更新您的邮件模板,将mailable更新为以下代码

    <?php
        namespace App\Mail;
        
        use Illuminate\Bus\Queueable;
        use Illuminate\Contracts\Queue\ShouldQueue;
        use Illuminate\Mail\Mailable;
        use Illuminate\Queue\SerializesModels;
        use Pepipost\PepipostLaravelDriver\Pepipost;
        
        class TestEmail extends Mailable
        {
          /**
           * Create a new message instance.
           *
           * @return void
           */
        
          use Pepipost;
        
          public $data;
          public function __construct($data)
          {
            $this->data = $data;
          }
        
          /**
           * Build the message.
           *
           * @return $this
           */
          public function build()
          {
            return $this
              ->view('mailtemplate.test')
              ->from('mail@sendingdomain.com')
              ->subject("Demo email from laravel")
              ->with([ 'test_message' => $this->data['message'] ]);
          }
        }

步骤5 - 测试

托管您的laravel项目,在浏览器中输入url- http://your_url.com/send/email

这将为发送电子邮件并在浏览器上显示“电子邮件已成功发送”。

附加用法

如果想要传递Pepipost SendEmail的其他参数,您必须更新邮件模板。根据您的需求添加参数。您可以使用此方法使用多个to、cc、bcc。请参阅官方Netcore API文档获取有关高级参数的更多详细信息。

这将在我们创建的app/Mail/TestEmail.php下。

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Pepipost\PepipostLaravelDriver\Pepipost;

class TestEmail extends Mailable
{
    /**
     * Create a new message instance.
     *
     * @return void
     */

    use Pepipost;

    public $data;
    public function __construct($data)
    {
        $this->data = $data;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this
            # To provide template provide empty array.
            ->view('mailtemplate.test')
            ->from('info@domain.com')
            # Not required for this example
            // ->cc('usercc@domain.com', 'username')
            ->subject("Demo email from laravel")
            # Store attachment in the "storage" path
            ->attach(storage_path('Discussions.pdf'))
            ->pepipost(
                [
                # Optional. For no options provide an empty array
                "personalizations" => [
                    [
                        // This will override the recipient specified in the mailer method "to"
                        "to" => [
                            [
                                "email" => "john@domain.com",
                                "name" => "John Doe"
                            ],
                            [
                                "email" => "emma@domain.com",
                                "name" => "Emma Watson"
                            ],
                        ],
                        // This will override the above cc_recipient specified in the mailer method, if provided.
                        "cc" => [
                            [
                                "email" => "ccrecipient1@domain.com",
                                "email" => "ccrecipient2@domain.com",
                            ]
                        ],
                        // This will override the above bcc_recipient specified in the mailer method, if provided.
                        "bcc" => [
                            [
                                "email" => "bccrecipient1@domain.com",
                                "email" => "bccrecipient2@domain.com",
                            ]
                        ],
                        # X-Api header for to mail
                        "token_to" => "tracker_phone",
                        # X-Api header for cc mail
                        "token_cc" => "tracker_cc",
                        # X-Api header for bcc mail
                        "token_bcc" => "tracker_bcc"
                    ],
                    [
                        # Different parameters for second recipient
                        "to" => [
                            [
                                "email" => "jenna@domain.com",
                                "name" => "Jenna Bane"
                            ]
                        ],
                        # X-Api header for to mail
                        "token_to" => "jenna_emp"
                    ]
                ],
                "settings" => [
                    "open_track" => true,
                    "click_track" => true,
                    "unsubscribe_track" => true,
                    "hepf" => false
                ],
                # For using pepipost templates instead of view templates
                "template_id" => 1234
            ]
        );
    }
}

公告

v3.0.0已发布!请参阅发行说明以获取详细信息。

本库的所有更新都记录在我们[发布记录]中。如有任何疑问,请随时通过devrel@netcorecloud.com联系我们。

路线图

如果您对该项目的未来方向感兴趣,请查看我们的公开问题拉取请求。我们非常欢迎您的反馈。

关于

pepipost-laravel库由Pepipost 开发者体验团队指导和支持。这个pepipost库由Pepipost Ltd维护和资助。pepipost gem的名称和标志是Pepipost Ltd的商标。

许可

MIT