headoo / headoo-mailjet-bundle
初始化 Mailjet PHP API v3 封装的 Symfony2 Bundle
dev-master / 1.0.x-dev
2017-03-03 13:16 UTC
Requires
- php: >=5.3
- symfony/framework-bundle: 2.*||3.*
This package is not auto-updated.
Last update: 2024-09-24 03:09:42 UTC
README
HeadooMailjetBundle 是一个 Symfony2 Bundle,主要用于处理 MailJet API v3。
此封装器使用已弃用的 API。一些文档可在此处找到:https://github.com/mailjet/mailjet-apiv3-php-simple 您应迁移到此封装器: https://github.com/mailjet/mailjet-apiv3-php
安装
-
将以下内容添加到
composer.json
文件{ "require": { "headoo/headoo-mailjet-bundle" : "1.0.x-dev" } }
-
使用以下命令安装我们的依赖项
$ php composer.phar update
-
在您的
app/AppKernel.php
中注册 Bundle<?php ... public function registerBundles() { $bundles = array( ... new Headoo\HeadooMailjetBundle\HeadooMailjetBundle(), ... ); ...
用法
将 RESTful API 作为服务使用
在 parameters.yml
中添加 api/secret 密钥即可
# app/parameters.yml mailjet_api_key: <your api key> mailjet_secret_key: <your secret key>
现在您可以通过调用 headoo_mailjet_wrapper
服务通过 DIC 访问 RESTful API
$mailjet = $this->container->get('headoo_mailjet_wrapper');
响应(使用 API 后)
您可以访问以下返回值
$mailjet->_response_code
: 例如,200
表示成功$mailjet->_response
: 返回 Mailjet 数据(如 ID)
API 示例用法
电子邮件信息
$params = array( "method" => "VIEW", "ID" => $id ); $resutl = $mailjet->message($params);
发送电子邮件
- 发送电子邮件
$params = array( "method" => "POST", "from" => "ms.mailjet@example.com", "to" => "mr.mailjet@example.com", "subject" => "Hello World!", "text" => "Greetings from Mailjet." ); $result = $mailjet->sendEmail($params);
- 发送带有一些附件的电子邮件(计算机上的绝对路径)
$params = array( "method" => "POST", "from" => "ms.mailjet@example.com", "to" => "mr.mailjet@example.com", "subject" => "Hello World!", "text" => "Greetings from Mailjet.", "attachment" => array("@/path/to/first/file.txt", "@/path/to/second/file.txt") ); $result = $mailjet->sendEmail($params);
- 发送带有一些内联附件的电子邮件(计算机上的绝对路径)
$params = array( "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" => array("@/path/to/photo1.jpg", "@/path/to/photo2.jpg") ); $result = $mailjet->sendEmail($params); }
账户设置
- 获取您的个人资料信息
$result = $mailjet->myprofile();
- 更新您的个人资料中的
AddressCity
字段
$params = array( "method" => "PUT", "AddressCity" => "New York" ); $result = $mailjet->myprofile($params);
联系人列表
- 打印您的联系人列表
$result = $mailjet->contact(); }
- 使用数组更新 ID 为
$id
的 contactData 资源
$data = array(array('Name' => 'lastname', 'Value' => 'Jet'), array('Name' => 'firstname', 'Value' => 'Mail')); $params = array( 'ID' => $id, 'Data' => $data, 'method' => 'PUT' ); $result = $mailjet->contactdata($params);
- 创建一个名为
$Lname
的列表
$params = array( "method" => "POST", "Name" => $Lname ); $result = $mailjet->contactslist($params);
- 获取 ID 为
$listID
的列表
$params = array( "method" => "VIEW", "ID" => $listID ); $result = $mailjet->contactslist($params);
注意:您可以使用资源的不同字段而不是 ID,例如,在这个示例中您的 params
数组中的 "unique" => "test@gmail.com"
- 创建一个具有电子邮件
$Cemail
的联系人
$params = array( "method" => "POST", "Email" => $Cemail ); $result = $mailjet->contact($params);
- 将 ID 为
$contactID
的联系人添加到 ID 为$listID
的列表中
$params = array( "method" => "POST", "ContactID" => $contactID, "ListID" => $listID, "IsActive" => "True" ); $result = $mailjet->listrecipient($params);
- 删除 ID 为
$listID
的列表
$params = array( "method" => "DELETE", "ID" => $listID ); $result = $mailjet->contactslist($params);
时事通讯
- 获取时事通讯
$newsletter_id
的 HTML 正文
$params = array( "method" => "GET", "ID" => $newsletter_id ); $result = $mailjet->getHTMLbody($params);
注意:在 GET 请求中,您需要在非过滤参数之前放置一个下划线字符
- 将 HTML 正文
$html_content
添加到时事通讯$newsletter_id
$params = array( "method" => "PUT", "ID" => $newsletter_id, "html_content" => $html_content ); $result = $mailjet->addHTMLbody($params);