Knowledge Base/SMTP Integration/Sending via SMTP in different languages

Sending via SMTP in various programming languages

Kaitlin
posted this on July 19, 2012 12:26 AM

Code examples provided favor environment variables over hard-coding credentials, so check to be sure you know how to set these in your chosen language. They will be referenced as 'MANDRILL_USERNAME' and 'MANDRILL_PASSWORD'.

Use environment variables for sensitive data, such as usernames and passwords, to keep this data only on the machines that need it.

Ruby

You will need the excellent Mail gem:

$ gem install mail

Code

require 'mail'

Mail.defaults do
  delivery_method :smtp, {
    :port      => 587,
    :address   => "smtp.mandrillapp.com",
    :user_name => ENV["MANDRILL_USERNAME"],
    :password  => ENV["MANDRILL_PASSWORD"]
  }
end

mail = Mail.deliver do
  to      'recipient@domain.com'
  from    'John Doe <john@doe.com>' # Your from name and email address
  subject 'A transactional email from Mandrill!'

  text_part do
    body 'Mandrill speaks plaintext'
  end

  html_part do
    content_type 'text/html; charset=UTF-8'
    body '<em>Mandrill speaks <strong>HTML</strong></em>'
  end
end

Python

Python has built-in libraries for handling email over SMTP: smtplib and email. We will also use the os library to so we can access our environment variables.

Code

import os
import smtplib

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

msg = MIMEMultipart('alternative')

msg['Subject'] = "Hello from Mandrill, Python style!"
msg['From']    = "John Doe <john@doe.com>" # Your from name and email address
msg['To']      = "recipient@example.com"

text = "Mandrill speaks plaintext"
part1 = MIMEText(text, 'plain')

html = "<em>Mandrill speaks <strong>HTML</strong></em>"
part2 = MIMEText(html, 'html')

username = os.environ['MANDRILL_USERNAME']
password = os.environ['MANDRILL_PASSWORD']

msg.attach(part1)
msg.attach(part2)

s = smtplib.SMTP('smtp.mandrillapp.com', 587)

s.login(username, password)
s.sendmail(msg['From'], msg['To'], msg.as_string())

s.quit()

PHP

Let's use SwiftMailer.

Code

<?php

include_once "swift_required.php";

$subject = 'Hello from Mandrill, PHP!';
$from = array('you@yourdomain.com' =>'Your Name');
$to = array(
 'recipient1@example.com'  => 'Recipient1 Name',
 'recipient2@example2.com' => 'Recipient2 Name'
);

$text = "Mandrill speaks plaintext";
$html = "<em>Mandrill speaks <strong>HTML</strong></em>";

$transport = Swift_SmtpTransport::newInstance('smtp.mandrillapp.com', 587);
$transport->setUsername('MANDRILL_USERNAME');
$transport->setPassword('MANDRILL_PASSWORD');
$swift = Swift_Mailer::newInstance($transport);

$message = new Swift_Message($subject);
$message->setFrom($from);
$message->setBody($html, 'text/html');
$message->setTo($to);
$message->addPart($text, 'text/plain');

if ($recipients = $swift->send($message, $failures))
{
 echo 'Message successfully sent!';
} else {
 echo "There was an error:\n";
 print_r($failures);
}

?>
 
Topic is closed for comments