定制 prspace/mailjet 二次开发

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

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

prspace/mailjet

Composer 安装命令:

composer require prspace/mailjet

包简介

Mailjet driver for Laravel

README 文档

README

This package extends the Laravel 4 MailService to enable Mailjet integration, based on Mailjet API v3.

Installation

Firstly you want to include this package in your composer.json file.

"require": {
    "prspace/mailjet" : "1.0.*"
}

Now you'll want to update or install via composer.

composer update

Next you open up app/config/app.php and replace the MailServiceProvider with

'Prspace\Mailjet\MailjetServiceProvider'

NOTE It is very important that you replace the default service providers to avoid conflicts. You don't lose any original functionality regarding mail drivers, they are still available.

Configuration is pretty easy: add the following entry to your config/services.php:

'mailjet' => [
    'key' => 'APIKEY',
    'secret' => 'APISECRET',
],

And replace the values with your respective api key and secret key.

Next, change the mail driver in your config/mail.php or your .env file to 'mailjet', and make sure you have a valid and authorised from-address configured.

That's all and you're good to go. For usage, check the Laravel 4 mail documentation

##API access##

I have also integrated direct access to Mailtjet's API, based on their example code.

To install, append the aliases in config/app.php with

'Mailjet'   => 'Prspace\Mailjet\Facades\Mailjet',

Examples

Based on https://github.com/mailjet/mailjet-apiv3-php-simple/blob/master/README.md

SendAPI

  • A function to send an email :
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;
}
  • A function to send an email with some attachments (absolute paths on your computer) :
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;
}
  • A function to send an email with some inline attachments (absolute paths on your computer) :
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;
}

Account Settings

  • A function to get your profile information :
function viewProfileInfo() {
    
    $result = Mailjet::myprofile();

    if (Mailjet::getResponseCode() == 200)
       echo "success - got profile information";
    else
       echo "error - ".Mailjet::getResponseCode();
}
  • A function to update the field AddressCity of your profile :
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;
}

Contact Lists

  • A function to print the list of your contacts :
function listContacts()
{
    
    $result = Mailjet::contact();

    if (Mailjet::getResponseCode() == 200)
       echo "success - listed contacts";
    else
       echo "error - ".Mailjet::getResponseCode();

    return $result;
}
  • A function to update your contactData resource with ID $id, using arrays :
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;
}
  • A function to create a list with name $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;
}
  • A function to get a list with 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;
}

Note : You can use unique fields of resources instead of IDs, like "unique" => "test@gmail.com" in your params array for this example

  • A function to create a contact with email $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;
}
  • A function to add the contact which ID is $contactID to the list which ID is $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;
}
  • A function to delete the list which ID is $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;
}
  • A function to get unsubscribed contact(s) from a list with ID $listID :
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;   
}
  • A function to get a contact with 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;
}

Note : You can use unique fields of resources instead of IDs, like "unique" => "test@gmail.com" in your params array for this example

Newsletters

You can use the DetailContent action to manage the content of a newsletter, in Text and Html. It has two properties : Text-part and Html-part. You can use GET, POST, PUT and DELETE both requests on this action :

  • GET : you get the Text-part and Html-part properties of a newsletter
  • POST : update the content of Text-part and Html-part. If you specify only one, the other will be emptied
  • PUT : update the content of Text-part and Html-part. You can specify only one, it will not empty the other one
  • DELETE : update the content of Text-part and Html-part and put both to empty.

Example with a GET on DetailContent :

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;
}

Use the schedule action to send a newsletter later. You just need to perform a POST request to schedule a new sending and to fill the date property with a Timestamp format in ISO 8601 : http://www.iso.org/iso/home/standards/iso8601.htm You can also DELETE a schedule Here is an example :

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;
}

To send a newsletter immediately, you have two possibilities :

  • POST a new schedule with a Timestamp which value is NOW
  • use send (only POST is supported) For the second case, here is an example :
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;
}

You can also test a newsletter by sending it to some specified recipients before making the real sending. To do so, you have to perform a POST request on a newsletter with action test like in the following example :

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;
}

prspace/mailjet 适用场景与选型建议

prspace/mailjet 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 317 次下载、GitHub Stars 达 2, 最近一次更新时间为 2015 年 11 月 19 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「api」 「laravel」 「v3」 「Laravel 4」 「Mailjet」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 prspace/mailjet 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 prspace/mailjet 我们能提供哪些服务?
定制开发 / 二次开发

基于 prspace/mailjet 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 317
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 7
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-11-19