sh0umik/laravel5-mailjet-5.3-fix
Laravel 5的Mailjet驱动程序,包含5.3修复
Requires
- php: >=5.4.0
- guzzlehttp/guzzle: 5.*|6.*
- illuminate/support: 5.1.*|5.2.*|5.3.*
This package is not auto-updated.
Last update: 2024-09-23 15:13:24 UTC
README
- Laravel: 5
- 作者: Ramon Ackermann
- 作者主页: https://github.com/sboo
本扩展包扩展了Laravel 5的MailService,以支持基于Mailjet API v3的Mailjet集成。
安装
首先,您需要在composer.json文件中包含此包。
"require": { "sboo/laravel5-mailjet" : "1.0.*" }
现在,您可以通过composer更新或安装。
composer update
接下来,打开app/config/app.php,将MailServiceProvider替换为
'Sboo\Laravel5MailjetFix\MailjetServiceProvider'
注意 替换默认的服务提供者非常重要,以避免冲突。您不会丢失任何原始的邮件驱动功能,它们仍然可用。
配置非常简单:将以下条目添加到您的config/services.php中
'mailjet' => [ 'key' => 'APIKEY', 'secret' => 'APISECRET', ],
并使用您的相应API密钥和密钥替换值。
接下来,将config/mail.php或您的.env文件中的邮件驱动更改为'mailjet',并确保您已配置有效的授权发件人地址。
这样就可以了。有关用法,请参阅Laravel 5邮件文档
##API访问##
我还集成了对Mailjet API的直接访问,基于他们的示例代码。
要安装,请将别名追加到config/app.php中
'Mailjet' => 'Sboo\Laravel5MailjetFix\Facades\Mailjet',
要在代码中使用API,请添加
use Mailjet;
示例
基于https://github.com/mailjet/mailjet-apiv3-php-simple/blob/master/README.md
SendAPI
- 发送电子邮件的功能
function sendEmail() { $params = [ "method" => "POST", "from" => "ms.mailjet@example.com", "to" => "mr.mailjet@example.com", "subject" => "Hello World!", "text" => "Greetings from Mailjet." ]; $result = Mailjet::sendEmail($params); if (Mailjet::getResponseCode() == 200) echo "success - email sent"; else echo "error - ".Mailjet::getResponseCode(); return $result; }
- 发送带有附件的电子邮件的功能(您的电脑上的绝对路径)
function sendEmailWithAttachments() { $params = [ "method" => "POST", "from" => "ms.mailjet@example.com", "to" => "mr.mailjet@example.com", "subject" => "Hello World!", "text" => "Greetings from Mailjet.", "attachment" => ["@/path/to/first/file.txt", "@/path/to/second/file.txt"] ]; $result = Mailjet::sendEmail($params); if (Mailjet::getResponseCode() == 200) echo "success - email sent"; else echo "error - ".Mailjet::getResponseCode(); return $result; }
- 发送带有内联附件的电子邮件的功能(您的电脑上的绝对路径)
function sendEmailWithInlineAttachments() { $params = [ "method" => "POST", "from" => "ms.mailjet@example.com", "to" => "mr.mailjet@example.com", "subject" => "Hello World!", "html" => "<html>Greetings from Mailjet <img src=\"cid:photo1.jpg\"><img src=\"cid:photo2.jpg\"></html>", "inlineattachment" => ["@/path/to/photo1.jpg", "@/path/to/photo2.jpg"] ]; $result = Mailjet::sendEmail($params); if (Mailjet::getResponseCode() == 200) echo "success - email sent"; else echo "error - ".Mailjet::getResponseCode(); return $result; }
账户设置
- 获取您的个人资料信息的功能
function viewProfileInfo() { $result = Mailjet::myprofile(); if (Mailjet::getResponseCode() == 200) echo "success - got profile information"; else echo "error - ".Mailjet::getResponseCode(); }
- 更新您的个人资料中
AddressCity
字段的功能
function updateProfileInfo() { $params = [ "method" => "PUT", "AddressCity" => "New York" ]; $result = Mailjet::myprofile($params); if (Mailjet::getResponseCode() == 200) echo "success - field AddressCity changed"; else echo "error - ".Mailjet::getResponseCode(); return $result; }
联系人列表
- 打印您的联系人列表的功能
function listContacts() { $result = Mailjet::contact(); if (Mailjet::getResponseCode() == 200) echo "success - listed contacts"; else echo "error - ".Mailjet::getResponseCode(); return $result; }
- 使用数组更新ID为
$id
的联系人数据资源的功能
function updateContactData($id) { $data = [ ['Name' => 'lastname', 'Value' => 'Jet'], ['Name' => 'firstname', 'Value' => 'Mail'] ]; $params = [ 'ID' => $id, 'Data' => $data, 'method' => 'PUT' ]; $result = Mailjet::contactdata($params); if (Mailjet::getResponseCode() == 200) echo "success - data changed"; else echo "error - ".Mailjet::getResponseCode(); return $result; }
- 创建一个名为
$Lname
的列表的功能
function createList($Lname) { $params = [ "method" => "POST", "Name" => $Lname ]; $result = Mailjet::contactslist($params); if (Mailjet::getResponseCode() == 201) echo "success - created list ".$Lname; else echo "error - ".Mailjet::getResponseCode(); return $result; }
- 获取ID为
$listID
的列表的功能
function getList($listID) { $params = [ "method" => "VIEW", "ID" => $listID ]; $result = Mailjet::contactslist($params); if (Mailjet::getResponseCode() == 200) echo "success - got list ".$listID; else echo "error - ".Mailjet::getResponseCode(); return $result; }
注意:您可以使用资源的不同字段而不是ID,例如,在此示例中,在params
数组中使用"unique" => "test@gmail.com"
- 创建一个具有电子邮件
$Cemail
的联系人功能
function createContact($Cemail) { $params = [ "method" => "POST", "Email" => $Cemail ]; $result = Mailjet::contact($params); if (Mailjet::getResponseCode() == 201) echo "success - created contact ".$Cname; else echo "error - ".Mailjet::getResponseCode(); return $result; }
- 将ID为
$contactID
的联系人添加到ID为$listID
的列表的功能
function addContactToList($contactID, $listID) { $params = [ "method" => "POST", "ContactID" => $contactID, "ListID" => $listID, "IsActive" => "True" ]; $result = Mailjet::listrecipient($params); if (Mailjet::getResponseCode() == 201) echo "success - contact ".$contactID." added to the list ".$listID; else echo "error - ".Mailjet::getResponseCode(); return $result; }
- 删除ID为
$listID
的列表的功能
function deleteList($listID) { $params = [ "method" => "DELETE", "ID" => $listID ]; $result = Mailjet::contactslist($params); if (Mailjet::getResponseCode() == 204) echo "success - deleted list"; else echo "error - ".Mailjet::getResponseCode(); return $result; }
- 从ID为
$listID
的列表中获取未订阅的联系人(s)的功能
function getUnsubscribedContactsFromList($listID) { $params = [ "method" => "GET", "ContactsList" => $listID, "Unsub" => true ]; $result = Mailjet::listrecipient($params); if (Mailjet::getResponseCode() == 200) echo "success - got unsubscribed contact(s) "; else echo "error - ".Mailjet::getResponseCode(); return $result; }
- 获取ID为
$contactID
的联系人功能
function getContact($contactID) { $params = [ "method" => "VIEW", "ID" => $contactID ]; $result = Mailjet::contact($params); if (Mailjet::getResponseCode() == 200) echo "success - got contact ".$contactID; else echo "error - ".Mailjet::getResponseCode(); return $result; }
注意:您可以使用资源的不同字段而不是ID,例如,在此示例中,在params
数组中使用"unique" => "test@gmail.com"
新闻通讯
您可以使用DetailContent
操作来管理新闻通讯的内容,在文本和Html中。它有两个属性:Text-part
和Html-part
。您可以使用GET
、POST
、PUT
和DELETE
对这两个操作进行请求
GET
:您获取新闻通讯的Text-part
和Html-part
属性POST
:更新文本部分
和HTML部分
的内容。如果只指定其中一个,另一个将被清空PUT
:更新文本部分
和HTML部分
的内容。您可以指定其中一个,不会清空另一个DELETE
:更新文本部分
和HTML部分
的内容,并将两者都置为空。
以下是一个对DetailContent
进行GET
请求的示例
function getNewsletterDetailcontent($newsletter_id) { $params = [ "method" => "GET", "ID" => $newsletter_id ]; $result = Mailjet::newsletterDetailContent($params); if (Mailjet::getResponseCode() == 200) echo "success - got content for the newsletter ". $newsletter_id; else echo "error - ".Mailjet::getResponseCode(); return $result; }
使用schedule
操作稍后发送新闻简报。您只需执行一个用于安排新的发送并将date
属性填充为ISO 8601时间戳格式的POST
请求:http://www.iso.org/iso/home/standards/iso8601.htm您也可以在此处DELETE
一个安排。以下是一个示例
function scheduleNewsletter($newsletter_id) { $params = [ "method" => "POST", "ID" => $newsletter_id, "date" => "2014-11-25T10:12:59Z" ); $result = Mailjet::newsletterSchedule($params); if (Mailjet::getResponseCode() == 201) echo "success - schedule done for the newsletter ". $newsletter_id; else echo "error - ".Mailjet::getResponseCode(); return $result; }
要立即发送新闻简报,您有两种可能性
POST
一个新的安排,其时间戳值为NOW
- 使用发送(仅支持
POST
)对于第二种情况,以下是一个示例
function sendNewsletter($newsletter_id) { $params = [ "method" => "POST", "ID" => $newsletter_id ]; $result = Mailjet::newsletterSend($params); if (Mailjet::getResponseCode() == 201) echo "success - newsletter ". $newsletter_id . " has been sent"; else echo "error - ".Mailjet::getResponseCode(); return $result; }
您还可以在正式发送前,通过向一些指定的收件人发送新闻简报来测试新闻简报。为此,您需要在以下示例中向新闻简报执行一个带有test
操作的POST
请求
function testNewsletter($newsletter_id) { $recipients = [ ['Email' => 'mailjet@example.org', 'Name' => 'Mailjet'] ]; $params = [ "method" => "POST", "ID" => $newsletter_id, "Recipients" => $recipients ]; $result = Mailjet::newsletterTest($params); if (Mailjet::getResponseCode() == 201) echo "success - newsletter ". $newsletter_id . " has been sent"; else echo "error - ".Mailjet::getResponseCode(); return $result; }