PHP code example of prspace / mailjet
1. Go to this page and download the library: Download prspace/mailjet library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
prspace / mailjet example snippets
'Prspace\Mailjet\MailjetServiceProvider'
'mailjet' => [
'key' => 'APIKEY',
'secret' => 'APISECRET',
],
'Mailjet' => 'Prspace\Mailjet\Facades\Mailjet',
function sendEmail() {
$params = [
"method" => "POST",
"from" => "[email protected] ",
"to" => "[email protected] ",
"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" => "[email protected] ",
"to" => "[email protected] ",
"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" => "[email protected] ",
"to" => "[email protected] ",
"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();
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
function testNewsletter($newsletter_id) {
$recipients = [
['Email' => '[email protected] ', '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;
}
$id
POST
POST
POST
date
POST
POST
POST