Menu

Sending Emails

Code examples for sending emails through Mailloop using popular languages and frameworks.

2 min read

Learn how to send emails through Mailloop using various programming languages and frameworks.

Node.js with Nodemailer

Nodemailer is the most popular Node.js email library.

JAVASCRIPT
const nodemailer = require('nodemailer');

const transporter = nodemailer.createTransport({
  host: 'smtp.mailloop.io',
  port: 587,
  secure: false, // true for 465, false for other ports
  auth: {
    user: 'your-sandbox-username',
    pass: 'your-sandbox-password'
  }
});

const mailOptions = {
  from: 'sender@example.com',
  to: 'recipient@example.com',
  subject: 'Test Email',
  text: 'This is a test email',
  html: '<p>This is a <strong>test email</strong></p>'
};

transporter.sendMail(mailOptions, (error, info) => {
  if (error) {
    console.log('Error:', error);
  } else {
    console.log('Email sent:', info.messageId);
  }
});

Python with smtplib

Python's built-in SMTP library.

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

# Create message
msg = MIMEMultipart('alternative')
msg['Subject'] = 'Test Email'
msg['From'] = 'sender@example.com'
msg['To'] = 'recipient@example.com'

# Create plain text and HTML versions
text = 'This is a test email'
html = '<p>This is a <strong>test email</strong></p>'

part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')

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

# Send email
with smtplib.SMTP('smtp.mailloop.io', 587) as server:
    server.starttls()
    server.login('your-sandbox-username', 'your-sandbox-password')
    server.send_message(msg)
    print('Email sent successfully')

PHP with PHPMailer

PHPMailer is a popular email library for PHP.

PHP
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php';

$mail = new PHPMailer(true);

try {
    // Server settings
    $mail->isSMTP();
    $mail->Host = 'smtp.mailloop.io';
    $mail->SMTPAuth = true;
    $mail->Username = 'your-sandbox-username';
    $mail->Password = 'your-sandbox-password';
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
    $mail->Port = 587;

    // Recipients
    $mail->setFrom('sender@example.com');
    $mail->addAddress('recipient@example.com');

    // Content
    $mail->isHTML(true);
    $mail->Subject = 'Test Email';
    $mail->Body = '<p>This is a <strong>test email</strong></p>';
    $mail->AltBody = 'This is a test email';

    $mail->send();
    echo 'Email sent successfully';
} catch (Exception $e) {
    echo "Email could not be sent. Error: {$mail->ErrorInfo}";
}

Ruby with Mail Gem

RUBY
require 'mail'

Mail.defaults do
  delivery_method :smtp, {
    address: 'smtp.mailloop.io',
    port: 587,
    user_name: 'your-sandbox-username',
    password: 'your-sandbox-password',
    authentication: :plain,
    enable_starttls_auto: true
  }
end

mail = Mail.new do
  from 'sender@example.com'
  to 'recipient@example.com'
  subject 'Test Email'
  text_part do
    body 'This is a test email'
  end
  html_part do
    content_type 'text/html; charset=UTF-8'
    body '<p>This is a <strong>test email</strong></p>'
  end
end

mail.deliver!
puts 'Email sent successfully'

Best Practices

Use Environment Variables - Never hardcode credentials in your source code.

Handle Errors - Always implement proper error handling for email sending.

Test Locally First - Verify your email templates in Mailloop before sending to production.

Use HTML and Text - Provide both HTML and plain text versions for better compatibility.