ohmysmtp / ohmysmtp-swiftmailer
MailPace的官方Swiftmailer传输工具
1.0.4
2022-05-09 12:15 UTC
Requires
- php: >=7.3
- guzzlehttp/guzzle: >=6.0
- swiftmailer/swiftmailer: >=6.2.7
Requires (Dev)
- egulias/email-validator: ^2.1.10|^3.1
- friendsofphp/php-cs-fixer: ^2.17
- phpunit/phpunit: ^9.5
- spatie/ray: ^1.10
- vimeo/psalm: ^4.3
README
MailPace 允许您通过易于使用的API从您的应用程序发送交易性电子邮件。
这个MailPace PHP包是SwiftMailer的传输,通过MailPace发送电子邮件,使从PHP应用程序发送电子邮件变得非常简单。您可以使用Laravel、Codeigniter和Symfony等流行框架发送交易性电子邮件,或者与独立的PHP应用程序一起使用。
此包使用MailPace HTTPS /send端点来发送电子邮件 - 这通常比SMTP更快、更可靠,尽管您当然可以使用SMTP发送电子邮件,而无需安装此包。
先决条件
您需要一个带有验证域名和活动的MailPace账户和组织计划。
安装
通过composer安装此包
composer require mailpace/mailpace-swiftmailer
账户设置
在MailPace上设置账户并完成登机步骤
配置包
首先,您需要从MailPace获取您的发送域的API令牌。您可以在“组织”->“域”->“API令牌”下找到它。
您需要将此API令牌存储在您的应用程序/运行时中,我们建议使用环境变量,下面的示例假设您有一个名为OHMYSMTP_API_TOKEN的环境变量,其中包含您的API令牌。
无框架发送
<?php require_once('./vendor/autoload.php'); $transport = new MailpaceSwiftmailerTransport(env('OHMYSMTP_API_TOKEN')); $mailer = new Swift_Mailer($transport); $message = (new Swift_Message('A transactional email from MailPace!')) ->setFrom(['php@yourdomain.com' => 'Your Name']) ->setTo(['someone@example.com']) ->setBody('<h1>HTML content</h1>', 'text/html') ->addPart('Text Body','text/plain'); // Attachments $data = 'Raw Attachment Data'; $attachment = new Swift_Attachment($data, 'attachment.txt', 'text/plain'); $message->attach($attachment); // Email Tags $headers = $message->getHeaders(); $headers->addTextHeader('MailPace-Tag', 'tag-1'); $headers->addTextHeader('MailPace-Tag', 'tag with spaces'); $mailer->send($message); ?>
使用Laravel发送
要使用Laravel发送,您需要进行一些小的调整,但这只需要很短的时间。
- 将mailpace添加到
config/mail.php配置文件
'mailpace' => [ 'transport' => 'mailpace', ],
- 将以下内容添加到您的
config/services.php配置文件
'mailpace' => [ 'apiToken' => env('OHMYSMTP_API_TOKEN'), ]
- 在
config/app.php中,将以下内容添加到提供者数组
App\Providers\MailpaceServiceProvider::class,
并删除/注释掉以下行
Illuminate\Mail\MailServiceProvider::class,
- 在您的
.env文件(或您存储环境变量的任何地方),按如下方式更改MAIL_MAILER变量
MAIL_MAILER=mailpace
- 在
App/Providers中创建一个新的文件,名为MailpaceServiceProvider.php,内容如下
<?php namespace App\Providers; use Illuminate\Mail\MailManager; use Illuminate\Mail\MailServiceProvider; use Mailpace\MailpaceSwiftmailer\MailpaceSwiftmailerTransport; class MailpaceServiceProvider extends MailServiceProvider { protected function registerIlluminateMailer() { $this->app->singleton('mail.manager', function ($app) { $manager = new MailManager($app); $this->registerOhMySmtpTransport($manager); return $manager; }); } protected function registerOhMySmtpTransport(MailManager $manager) { $manager->extend('mailpace', function ($config) { if (! isset($config['apiToken'])) { $config = $this->app['config']->get('services.mailpace', []); } return new MailpaceSwiftmailerTransport($config['apiToken']); }); } }
完成以上步骤后,所有电子邮件将通过MailPace发送。
支持
有关支持,请参阅MailPace文档或通过support@mailpace.com联系我们
贡献
请确保为任何更改添加测试。要运行测试,请执行以下操作
composer test
欢迎提交拉取请求
许可
该软件包作为开源软件提供,受MIT许可证条款约束。