Ruby
You need to have Mail gem:
$ gem install mail
Code
require 'mail'
# Get username and password from environment variables
username = ENV["DOPPLERRELAY_USERNAME"]
password = ENV["DOPPLERRELAY_PASSWORD"]
# Relay SMTP service configuration
host = "smtp.dopplerrelay.com"
port = 587
# Custom data
from = "[email protected]"
to = "[email protected]"
subject = "Hello from Doppler Relay, Ruby style!"
text = "Doppler Relay speaks plaintext"
html = "Doppler Relay speaks <b>HTML</b>"
# Send message using Mikel/Mail Library
Mail.defaults do
delivery_method :smtp, {
:address => host,
:port => port,
:user_name => username,
:password => password,
:authentication => :login # This line is IMPORTANT, Relay only supports this authentication type
}
end
mail = Mail.deliver do
to to
from from
subject subject
text_part do
body text
end
html_part do
content_type 'text/html; charset=UTF-8'
body html
end
end
Python
Python has libraries for handling email via SMTP: smtplib and email. Also, we’ll use this os libraries.
Code
import os
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# Get username and password from environment variables
username = os.environ['DOPPLERRELAY_USERNAME']
password = os.environ['DOPPLERRELAY_PASSWORD']
# Relay SMTP service configuration
host = 'smtp.dopplerrelay.com'
port = 587
# Custom data
mailfrom = "[email protected]"
mailto = "[email protected]"
subject = "Hello from Doppler Relay, Python style!"
text = "Doppler Relay speaks plaintext"
html = "Doppler Relay speaks <b>HTML</b>"
# Send message using smtplib
msg = MIMEMultipart('alternative')
msg['Subject'] = subject
msg['From'] = mailfrom
msg['To'] = mailto
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')
msg.attach(part1)
msg.attach(part2)
s = smtplib.SMTP(host, port)
s.login(username, password)
s.sendmail(mailfrom, mailto, msg.as_string())
s.quit()
PHP
We’ll use SwiftMailer:
<?php
// SwiftMailer previously installed
// See https://github.com/DopplerRelay/docker-php/blob/master/Dockerfile
include_once "/usr/lib/vendor/swiftmailer/swiftmailer/lib/swift_required.php";
// Get username and password from environment variables
$username = getenv('DOPPLERRELAY_USERNAME');
$password = getenv('DOPPLERRELAY_PASSWORD');
// Relay SMTP service configuration
$host = 'smtp.dopplerrelay.com';
$port = 587;
// Custom data
$from = array('[email protected]' => 'Your Name');
$to = array(
'[email protected]' => 'Recipient1 Name',
'[email protected]' => 'Recipient2 Name'
);
$subject = 'Hello from Doppler Relay, PHP!';
$text = "Doppler Relay speaks plaintext";
$html = "Doppler Relay speaks <b>HTML</b>";
// Send message using SwiftMailer
$transport = Swift_SmtpTransport::newInstance($host, $port);
$transport->setUsername($username);
$transport->setPassword($password);
$transport->setAuthMode('LOGIN');
$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);
}
?>
- What is Transactional Email?
- Documentation
- Help
- Blog