This article provides working code examples for sending transactional email through MigoSMTP using SMTP in six programming languages and frameworks. Each example follows security best practices — credentials are read from environment variables, not hard-coded.
Connection Details Recap
| Setting | Value |
|---|---|
| Host | smtp.migosmtp.com |
| Port (preferred) | 587 (STARTTLS) |
| Port (alternative) | 465 (SSL/TLS) |
| Auth | LOGIN — username and password from SMTP Accounts |
PHP — PHPMailer (Most Common)
<?php
use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerSMTP;
use PHPMailerPHPMailerException;
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
try {
// SMTP configuration
$mail->isSMTP();
$mail->Host = 'smtp.migosmtp.com';
$mail->SMTPAuth = true;
$mail->Username = getenv('SMTP_USERNAME');
$mail->Password = getenv('SMTP_PASSWORD');
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
// Sender and recipient
$mail->setFrom('noreply@yourcompany.com', 'Your Company');
$mail->addAddress('customer@example.com', 'Customer Name');
$mail->addReplyTo('support@yourcompany.com', 'Support');
// Content
$mail->isHTML(true);
$mail->Subject = 'Your order #12345 has been confirmed';
$mail->Body = '<p>Hi Customer, your order has been received.</p>';
$mail->AltBody = 'Hi Customer, your order has been received.';
$mail->send();
echo 'Message sent successfully';
} catch (Exception $e) {
echo 'Message could not be sent. Error: ' . $mail->ErrorInfo;
}
Python — smtplib (Standard Library)
import os
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
smtp_host = 'smtp.migosmtp.com'
smtp_port = 587
username = os.environ['SMTP_USERNAME']
password = os.environ['SMTP_PASSWORD']
msg = MIMEMultipart('alternative')
msg['Subject'] = 'Your order #12345 has been confirmed'
msg['From'] = 'noreply@yourcompany.com'
msg['To'] = 'customer@example.com'
html_part = MIMEText('<p>Hi Customer, your order has been received.</p>', 'html')
text_part = MIMEText('Hi Customer, your order has been received.', 'plain')
msg.attach(text_part)
msg.attach(html_part)
try:
with smtplib.SMTP(smtp_host, smtp_port) as server:
server.ehlo()
server.starttls()
server.login(username, password)
server.sendmail(msg['From'], [msg['To']], msg.as_string())
print('Email sent successfully')
except Exception as e:
print(f'Failed to send: {e}')
Node.js — Nodemailer
const nodemailer = require('nodemailer');
const transporter = nodemailer.createTransport({
host: 'smtp.migosmtp.com',
port: 587,
secure: false, // false = STARTTLS; true = SSL on port 465
requireTLS: true,
auth: {
user: process.env.SMTP_USERNAME,
pass: process.env.SMTP_PASSWORD,
},
});
async function sendEmail() {
try {
const info = await transporter.sendMail({
from: '"Your Company" <noreply@yourcompany.com>',
to: 'customer@example.com',
subject: 'Your order #12345 has been confirmed',
text: 'Hi Customer, your order has been received.',
html: '<p>Hi Customer, your order has been received.</p>',
});
console.log('Message sent:', info.messageId);
} catch (error) {
console.error('Error:', error);
}
}
sendEmail();
Ruby — Net::SMTP
require 'net/smtp'
smtp_username = ENV['SMTP_USERNAME']
smtp_password = ENV['SMTP_PASSWORD']
from_address = 'noreply@yourcompany.com'
to_address = 'customer@example.com'
message = <<~MESSAGE
From: Your Company <#{from_address}>
To: #{to_address}
Subject: Your order #12345 has been confirmed
MIME-Version: 1.0
Content-Type: text/html
<p>Hi Customer, your order has been received.</p>
MESSAGE
Net::SMTP.start('smtp.migosmtp.com', 587, 'yourcompany.com',
smtp_username, smtp_password, :login) do |smtp|
smtp.enable_starttls
smtp.send_message(message, from_address, to_address)
puts 'Email sent successfully'
end
Java — Jakarta Mail
import jakarta.mail.*;
import jakarta.mail.internet.*;
import java.util.Properties;
public class SendEmail {
public static void main(String[] args) throws MessagingException {
String smtpUser = System.getenv("SMTP_USERNAME");
String smtpPass = System.getenv("SMTP_PASSWORD");
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.migosmtp.com");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.ssl.protocols", "TLSv1.2 TLSv1.3");
Session session = Session.getInstance(props,
new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(smtpUser, smtpPass);
}
});
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("noreply@yourcompany.com", "Your Company"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("customer@example.com"));
message.setSubject("Your order #12345 has been confirmed");
MimeMultipart content = new MimeMultipart("alternative");
content.addBodyPart(createPart("Hi Customer, your order is confirmed.", "plain"));
content.addBodyPart(createPart("<p>Hi Customer, your order is confirmed.</p>", "html"));
message.setContent(content);
Transport.send(message);
System.out.println("Email sent successfully");
}
private static MimeBodyPart createPart(String text, String subtype)
throws MessagingException {
MimeBodyPart part = new MimeBodyPart();
part.setText(text, "utf-8", subtype);
return part;
}
}
Go — net/smtp
package main
import (
"crypto/tls"
"fmt"
"net/smtp"
"os"
)
func main() {
host := "smtp.migosmtp.com"
port := "587"
username := os.Getenv("SMTP_USERNAME")
password := os.Getenv("SMTP_PASSWORD")
auth := smtp.PlainAuth("", username, password, host)
tlsConfig := &tls.Config{
InsecureSkipVerify: false,
ServerName: host,
MinVersion: tls.VersionTLS12,
}
conn, err := smtp.Dial(host + ":" + port)
if err != nil { panic(err) }
defer conn.Close()
if err = conn.StartTLS(tlsConfig); err != nil { panic(err) }
if err = conn.Auth(auth); err != nil { panic(err) }
if err = conn.Mail("noreply@yourcompany.com"); err != nil { panic(err) }
if err = conn.Rcpt("customer@example.com"); err != nil { panic(err) }
wc, err := conn.Data()
if err != nil { panic(err) }
fmt.Fprintf(wc, "Subject: Your order confirmed
Content-Type: text/plain
Hi Customer, your order is confirmed.")
wc.Close()
conn.Quit()
fmt.Println("Email sent successfully")
}