Sending Mails with Other Popular Libraries
Info! This is Beta version, if you have any problem in using API, please mail us at support@sarv.com
PHP CodeIgniter
You can use this library to send email using CodeIgniter. Download it from here https://github.com/abhimanyu1310/SarvMTA_PHP_Codeigniter and unzip to any folder. Place our libraries folder to your CodeIgniter application folder and use it as below.
Example Controller
<?php
class EmailTest extends CI_Controller {
public function index() {
echo 'Hello From Test!';
}
public function sendMail() {
$this->load->library("Sarv");
$owner_id = 'owner_id';
$apiToken = 'token';
$SarvTES_APP_DOMAIN = 'SarvTES_APP_DOMAIN';
$smtp_user_name = "smtp12345";
$this->sarv->init($owner_id, $apiToken, $SarvTES_APP_DOMAIN); //You must call this method once before any other method call
$recipients = array(
array(
'email' => 'recipient1.email@example.com',
'name' => 'Recipient1 Name'
),
array(
'email' => 'recipient2.email@example.com',
'name' => 'Recipient2 Name'
)
);
foreach ($recipients as $recipient) {
$message = array();
$message["html"] = "Example HTML content";
$message["text"] = "Example text content";
$message["subject"] = "example subject";
$message["from_email"] = "from_email@example.com";
$message["to"] = array(
array("email" => $recipient['email'], "name" => $recipient['name'], "type" => "to")
);
$message["headers"] = array("Reply-To" => "message.reply@example.com", "X-Unique-Id" => "Id");
$message["attachments"] = array(array("type" => "text/plain", "name" => "myfile.txt", "content" => "ZXhhbXBsZSBmaWxl"));
$message["images"] = array(array("type" => "image/png", "name" => "IMAGECID", "content" => "dgfdddger"));
$result = $this->sarv->messages->sendMail($smtp_user_name, $message);
print_r($result);
/*
Array
(
[status] => "success"
[message] => "message have been Queued ... "
)
*/
}
}
}
?>
PHPMailer
A full-featured email creation and transfer class for PHP You can download it from here https://github.com/PHPMailer/PHPMailer
Example
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = '<SarvTES_APP_DOMAIN>'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '<smtp_user_name>'; // SMTP username
$mail->Password = '<smtp_password>'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->LE = "\r\n"; // For more https://sourceforge.net/p/phpmailer/bugs/99/
$mail->setFrom('from@example.com', 'Mailer');
$mail->addAddress('joe@example.net', 'Joe User'); // Add a recipient
$mail->addAddress('ellen@example.com'); // Name is optional
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->addCustomHeader('X-Unique-Id', 'id');
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body in bold!';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>
Swift Mailer
Comprehensive mailing tools for PHP. You can download it from here https://github.com/swiftmailer/swiftmailer
Example
<?php
require_once '/path/to/swift-mailer/lib/swift_required.php';
// Create the Transport
$transport = Swift_SmtpTransport::newInstance('<SarvTES_APP_DOMAIN>', 587)
->setUsername('<smtp_user_name>')
->setPassword('<smtp_password>')
;
// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
// Create the message
$message = Swift_Message::newInstance()
// Give the message a subject
->setSubject('Your subject')
// Set the From address with an associative array
->setFrom(array('john@doe.com' => 'John Doe'))
// Set the To addresses with an associative array
->setTo(array('receiver@domain.org', 'other@domain.org' => 'A name'))
// Give it a body
->setBody('Here is the message itself')
// And optionally an alternative body
->addPart('Here is the message itself
', 'text/html')
// Optionally add any attachments
->attach(Swift_Attachment::fromPath('my-document.pdf'))
;
// Add your custom headers
$message->getHeaders()->addTextHeader('X-Unique-Id', 'id');
// Send the message
$result = $mailer->send($message);
var_dump($result);
?>