Standard Library
Email (mail)

Email Module

The mail module provides SMTP email sending funcality.

Configuration

Configure SMTP settings in .env:

SMTP_HOST=smtp.gmail.com
SMTP_USER=your_email@gmail.com
SMTP_PASSWORD=your_app_password
SMTP_PORT=587

Note: For Gmail, use an App Password, not your regular password.

funcs

mail.send()

Send an email:

let result = mail.send(
    "recipient@example.com",
    "Subject: Test Email",
    "This is the email body."
);
 
if (result) {
    print("Email sent successfully");
} else {
    print("Failed to send email");
}

Examples

Welcome Email

func send_welcome_email(user_email, username) {
    let subject = "Subject: Welcome to Flowa!";
    let body = "Hello " + username + ",\n\n" +
               "Welcome to our platform!\n\n" +
               "Best regards,\nThe Flowa Team";
    
    return mail.send(user_email, subject, body);
}
 
send_welcome_email("alice@example.com", "Alice");

Password Reset

func send_password_reset(email, reset_token) {
    let subject = "Subject: Password Reset Request";
    let body = "You requested a password reset.\n\n" +
               "Reset token: " + reset_token + "\n\n" +
               "This token expires in 1 hour.";
    
    return mail.send(email, subject, body);
}

Notification System

func notify_admin(event_type, details) {
    let admin_email = env.ADMIN_EMAIL;
    let subject = "Subject: System Notification - " + event_type;
    let body = "Event: " + event_type + "\n" +
               "Details: " + details + "\n" +
               "Time: 2024-12-12 10:30:00";
    
    mail.send(admin_email, subject, body);
}
 
notify_admin("User Registered", "New user: alice@example.com");

Best Practices

  • Use environment variables for credentials
  • Validate email addresses before sending
  • Handle send failures gracefully
  • Implement retry logic for failed sends
  • Don't send sensitive data in emails
  • Use templates for consistent formatting

Next Steps

  • Examples - Complete applications with email