Guide : Configuration SMTP dans vos applications
Ce guide explique comment configurer la passerelle SMTP SendAs.me dans les outils et bibliothèques les plus courants.
Paramètres SMTP
Avant de commencer, récupérez vos paramètres SMTP depuis le portail SendAs.me :
| Paramètre | Valeur |
|---|---|
| Hôte SMTP | smtp.sendas.me |
| Port | 587 (STARTTLS) ou 465 (SSL) |
| Sécurité | STARTTLS (port 587) ou SSL (port 465) |
| Authentification | LOGIN ou PLAIN |
| Nom d'utilisateur | Votre app_id (ex: monapp) |
| Mot de passe | La send_key de votre application (sek_...) |
L'adresse From de vos e-mails doit correspondre à l'adresse du compte OAuth connecté. Si vous envoyez avec From: alice@gmail.com, le compte Gmail d'Alice doit être connecté à votre application.
Python — smtplib
Envoi simple
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
SMTP_HOST = "smtp.sendas.me"
SMTP_PORT = 587
SMTP_USERNAME = "votre_app_id" # app_id (ex: monapp)
SMTP_PASSWORD = "sek_votre_send_key" # send_key de l'application
def send_email(to: str, subject: str, body_html: str, from_addr: str):
msg = MIMEMultipart("alternative")
msg["Subject"] = subject
msg["From"] = from_addr
msg["To"] = to
# Version texte (fallback)
msg.attach(MIMEText("Veuillez utiliser un client email qui supporte HTML", "plain"))
# Version HTML
msg.attach(MIMEText(body_html, "html"))
with smtplib.SMTP(SMTP_HOST, SMTP_PORT) as server:
server.ehlo()
server.starttls()
server.ehlo()
server.login(SMTP_USERNAME, SMTP_PASSWORD)
server.sendmail(from_addr, [to], msg.as_string())
# Utilisation
send_email(
to="client@example.com",
subject="Bienvenue !",
body_html="<h1>Bienvenue sur notre plateforme</h1>",
from_addr="contact@votreentreprise.com"
)
Avec pièce jointe
import smtplib
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def send_with_attachment(to: str, subject: str, body: str,
from_addr: str, attachment_path: str):
msg = MIMEMultipart()
msg["Subject"] = subject
msg["From"] = from_addr
msg["To"] = to
msg.attach(MIMEText(body, "html"))
with open(attachment_path, "rb") as f:
part = MIMEApplication(f.read(), Name=attachment_path.split("/")[-1])
part["Content-Disposition"] = f'attachment; filename="{attachment_path.split("/")[-1]}"'
msg.attach(part)
with smtplib.SMTP(SMTP_HOST, SMTP_PORT) as server:
server.starttls()
server.login(SMTP_USERNAME, SMTP_PASSWORD)
server.sendmail(from_addr, [to], msg.as_string())
Node.js — Nodemailer
Installation
npm install nodemailer
Configuration et envoi
const nodemailer = require('nodemailer');
// Créer le transporteur (réutilisable)
const transporter = nodemailer.createTransport({
host: 'smtp.sendas.me',
port: 587,
secure: false, // false = STARTTLS sur port 587
auth: {
user: 'votre_app_id', // app_id (ex: monapp)
pass: 'sek_votre_send_key', // send_key de l'application
},
// Optionnel: désactiver la vérification SSL en dev
// tls: { rejectUnauthorized: false }
});
// Envoyer un e-mail
async function sendEmail() {
const info = await transporter.sendMail({
from: '"Mon App" <contact@votreentreprise.com>',
to: 'client@example.com',
cc: 'copie@example.com', // optionnel
subject: 'Confirmation de votre commande',
text: 'Merci pour votre commande.',
html: `
<h1>Merci pour votre commande !</h1>
<p>Nous traitons votre commande et vous contacterons prochainement.</p>
`,
attachments: [
{
filename: 'facture.pdf',
path: '/chemin/vers/facture.pdf',
},
],
});
console.log('Message envoyé: %s', info.messageId);
return info;
}
sendEmail().catch(console.error);
Avec TypeScript
import nodemailer, { Transporter } from 'nodemailer';
const transporter: Transporter = nodemailer.createTransport({
host: process.env.SMTP_HOST || 'smtp.sendas.me',
port: parseInt(process.env.SMTP_PORT || '587'),
secure: false,
auth: {
user: process.env.SMTP_USERNAME,
pass: process.env.SMTP_PASSWORD,
},
});
export async function sendEmail(options: {
to: string;
subject: string;
html: string;
from?: string;
}): Promise<void> {
await transporter.sendMail({
from: options.from || '"Mon Service" <noreply@votreentreprise.com>',
to: options.to,
subject: options.subject,
html: options.html,
});
}
PHP — PHPMailer
Installation (Composer)
composer require phpmailer/phpmailer
Envoi avec PHPMailer
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
function sendEmail(string $to, string $subject, string $htmlBody): bool
{
$mail = new PHPMailer(true);
try {
// Configuration serveur SMTP
$mail->isSMTP();
$mail->Host = 'smtp.sendas.me';
$mail->SMTPAuth = true;
$mail->Username = 'votre_app_id'; // app_id (ex: monapp)
$mail->Password = 'sek_votre_send_key'; // send_key de l'application
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
$mail->CharSet = 'UTF-8';
// Expéditeur et destinataire
$mail->setFrom('contact@votreentreprise.com', 'Mon Application');
$mail->addAddress($to);
// Contenu
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $htmlBody;
$mail->AltBody = strip_tags($htmlBody);
$mail->send();
return true;
} catch (Exception $e) {
error_log("Erreur envoi email: {$mail->ErrorInfo}");
return false;
}
}
// Utilisation
sendEmail(
'client@example.com',
'Votre facture du mois',
'<h1>Facture disponible</h1><p>Votre facture est disponible.</p>'
);
Postfix comme relay
Si vous utilisez Postfix pour relayer tous vos e-mails système vers SendAs.me :
/etc/postfix/main.cf
# Relay SMTP vers SendAs.me
relayhost = [smtp.sendas.me]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_use_tls = yes
smtp_tls_security_level = encrypt
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
/etc/postfix/sasl_passwd
[smtp.sendas.me]:587 votre_app_id:sek_votre_send_key
Appliquer la configuration
sudo postmap /etc/postfix/sasl_passwd
sudo chmod 600 /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db
sudo systemctl reload postfix
Django (Python)
Dans settings.py :
# Configuration email Django
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.sendas.me'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_USE_SSL = False
EMAIL_HOST_USER = 'votre_app_id' # app_id (ex: monapp)
EMAIL_HOST_PASSWORD = 'sek_votre_send_key' # send_key de l'application
DEFAULT_FROM_EMAIL = 'contact@votreentreprise.com'
Envoi :
from django.core.mail import send_mail
send_mail(
subject='Bienvenue',
message='Merci de vous être inscrit.',
from_email='contact@votreentreprise.com',
recipient_list=['user@example.com'],
html_message='<h1>Bienvenue !</h1>',
)
Ruby on Rails
Dans config/environments/production.rb :
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.sendas.me',
port: 587,
domain: 'votreentreprise.com',
user_name: 'votre_app_id', # app_id (ex: monapp)
password: 'sek_votre_send_key', # send_key de l'application
authentication: 'login',
enable_starttls_auto: true
}
Variables d'environnement recommandées
Ne stockez jamais les credentials SMTP directement dans votre code. Utilisez des variables d'environnement :
# .env de votre application cliente
SENDAS_SMTP_HOST=smtp.sendas.me
SENDAS_SMTP_PORT=587
SENDAS_SMTP_USERNAME=votre_app_id
SENDAS_SMTP_PASSWORD=sek_votre_send_key
EMAIL_FROM=contact@votreentreprise.com
Si vous suspectez que votre send_key a été compromise, régénérez-en une nouvelle immédiatement via le portail SendAs.me (POST /apps/{pushable_key}/reset-send-key). L'ancienne send_key est invalidée instantanément.