Skip to content

About

Serverless newsletter translator - auto-translate emails from English to Romanian using DeepL + AWS Lambda

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

📧 Automated Newsletter Translator (EN → RO)

Automatically translate English AI newsletters to Romanian using DeepL API and AWS Lambda.

🎯 Overview

This system monitors incoming email newsletters, extracts the content, translates it from English to Romanian using DeepL's high-quality translation API, and emails the result back to you.

Architecture:

Newsletter Email → Gmail Forwarding → Webhook → AWS Lambda → DeepL API → Email Back

Key Features:

  • ✅ Automatic email processing via webhook
  • ✅ High-quality EN→RO translation with DeepL
  • ✅ Smart HTML parsing (removes footers, unsubscribe links)
  • ✅ Includes both translated and original text
  • ✅ Serverless architecture (AWS Lambda)
  • ✅ Cost-effective: $0/month for typical usage (30 newsletters/month)

💰 Cost Estimate

Component Service Monthly Cost
Email Webhook CloudMailin Free / SendGrid $0
Translation DeepL API Free Tier $0 (up to 500k chars)
Email Sending AWS SES $0 (free tier: 62k emails/month)
Lambda Execution AWS Lambda $0 (free tier: 1M requests)
TOTAL $0/month

For 30 newsletters/month (~5,000 words each = 200k characters), you'll stay well within free tiers.


📋 Prerequisites

  1. AWS Account (for Lambda and SES)
  2. DeepL API Account (free tier available)
  3. Gmail Account (or any email provider that supports forwarding)
  4. Email Webhook Service (CloudMailin free tier or SendGrid)

🚀 Setup Guide

Step 1: Get DeepL API Key

  1. Go to DeepL API
  2. Sign up for the DeepL API Free plan
    • 500,000 characters/month free
    • No credit card required
  3. Get your Authentication Key
    • Will end with :fx for free tier
    • Example: abc123def-456-789-ghi:fx
  4. Save this key - you'll need it later

Step 2: Set Up AWS SES (Email Sending)

2.1 Verify Your Email Address

# Using AWS CLI
aws ses verify-email-identity --email-address [email protected]

Or via AWS Console:

  1. Go to AWS SES Console → Verified Identities
  2. Click Create Identity
  3. Select Email Address
  4. Enter your email and click Create Identity
  5. Check your inbox and click the verification link

2.2 Request Production Access (Optional)

By default, SES is in Sandbox Mode (can only send to verified addresses). For production use:

  1. In SES Console, click Account Dashboard
  2. Click Request production access
  3. Fill out the form explaining your use case
  4. Approval usually takes 24-48 hours

For testing: Just verify both sender and recipient emails.

Step 3: Create Lambda Function

3.1 Build Deployment Package

# Clone this repository
git clone <your-repo-url>
cd newsletter-translator

# Run deployment script
./deploy.sh

This creates newsletter-translator-lambda.zip containing:

  • lambda_function.py (main code)
  • All Python dependencies (requests library)

3.2 Create Lambda Function in AWS

Option A: Using AWS Console

  1. Go to AWS Lambda Console

  2. Click Create function

  3. Choose Author from scratch

  4. Configure:

    • Function name: newsletter-translator
    • Runtime: Python 3.11
    • Architecture: x86_64
  5. Click Create function

  6. Upload deployment package:

    • In Code tab, click Upload from → .zip file
    • Upload newsletter-translator-lambda.zip
    • Click Save
  7. Configure function:

    • Timeout: 60 seconds (Configuration → General configuration → Edit)
    • Memory: 512 MB

Option B: Using AWS CLI

# Create IAM role first (if not exists)
aws iam create-role \
  --role-name lambda-newsletter-translator-role \
  --assume-role-policy-document '{
    "Version": "2012-10-17",
    "Statement": [{
      "Effect": "Allow",
      "Principal": {"Service": "lambda.amazonaws.com"},
      "Action": "sts:AssumeRole"
    }]
  }'

# Attach policies
aws iam attach-role-policy \
  --role-name lambda-newsletter-translator-role \
  --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole

aws iam attach-role-policy \
  --role-name lambda-newsletter-translator-role \
  --policy-arn arn:aws:iam::aws:policy/AmazonSESFullAccess

# Create function
aws lambda create-function \
  --function-name newsletter-translator \
  --runtime python3.11 \
  --role arn:aws:iam::YOUR_ACCOUNT_ID:role/lambda-newsletter-translator-role \
  --handler lambda_function.lambda_handler \
  --zip-file fileb://newsletter-translator-lambda.zip \
  --timeout 60 \
  --memory-size 512

3.3 Configure Environment Variables

In Lambda Console → Configuration → Environment variables → Edit

Add these variables:

Key Value Example
DEEPL_API_KEY Your DeepL API key abc123-456-789:fx
RECIPIENT_EMAIL Where to send translations [email protected]
SENDER_EMAIL SES verified sender address [email protected]
AWS_REGION AWS region for SES us-east-1

Step 4: Set Up API Gateway (Webhook Endpoint)

4.1 Create REST API

  1. Go to API Gateway Console
  2. Click Create API → REST API → Build
  3. Configure:
    • API name: newsletter-webhook
    • Endpoint Type: Regional
  4. Click Create API

4.2 Create POST Method

  1. Click Actions → Create Resource

    • Resource Name: translate
    • Resource Path: /translate
  2. Click Create Resource

  3. With /translate selected, click Actions → Create Method → POST

  4. Configure:

    • Integration type: Lambda Function
    • Lambda Function: newsletter-translator
    • Use Lambda Proxy integration: ✅ Check this
  5. Click Save → OK

4.3 Deploy API

  1. Click Actions → Deploy API
  2. Deployment stage: New Stage → prod
  3. Click Deploy
  4. Copy the Invoke URL (e.g., https://abc123.execute-api.us-east-1.amazonaws.com/prod)

Your webhook URL is: https://abc123.execute-api.us-east-1.amazonaws.com/prod/translate

Step 5: Set Up Email Webhook

You need a service that converts incoming emails to HTTP POST requests.

Option A: CloudMailin (Recommended - Free Tier)

  1. Go to CloudMailin
  2. Sign up (free tier: 10,000 emails/month)
  3. Click Create Address
  4. Configure:
    • Target URL: https://your-api-gateway-url/prod/translate
    • HTTP Format: JSON
  5. Copy your CloudMailin email address (e.g., [email protected])

Option B: SendGrid Inbound Parse

  1. Go to SendGrid
  2. Sign up (free tier: 100 emails/day)
  3. Go to Settings → Inbound Parse
  4. Click Add Host & URL
  5. Configure:
    • Domain: Use SendGrid's domain or your own
    • URL: https://your-api-gateway-url/prod/translate
  6. Get your inbound email address

Option C: AWS SES Receiving (More Complex)

See AWS SES Email Receiving Guide

Step 6: Configure Gmail Forwarding

Now set up Gmail to automatically forward newsletters to the webhook email.

Method 1: Auto-Forward with Filter

  1. In Gmail, click the gear icon → See all settings
  2. Go to Filters and Blocked Addresses tab
  3. Click Create a new filter
  4. Configure filter criteria (example):
    From: [email protected]
    OR
    From: [email protected]
    OR
    Subject: (AI Weekly OR AI Newsletter)
    
  5. Click Create filter
  6. Check Forward it to → Enter CloudMailin/SendGrid address
  7. Click Create filter

Method 2: Manual Forwarding

When you receive a newsletter:

  1. Open the email
  2. Click More (⋮) → Forward
  3. Send to your webhook email address (CloudMailin/SendGrid)

🧪 Testing

Local Testing (Before Deployment)

  1. Install dependencies:
pip install -r requirements.txt
pip install python-dotenv  # For loading .env file
  1. Create .env file (copy from .env.example):
cp .env.example .env
  1. Edit .env with your credentials:
DEEPL_API_KEY=your-key-here:fx
RECIPIENT_EMAIL=[email protected]
SENDER_EMAIL=[email protected]
AWS_REGION=us-east-1
  1. Run test script:
python test_local.py

This will:

  • Process a sample AI newsletter
  • Translate it to Romanian
  • Send it to your email

Testing the Deployed Lambda

Test with API Gateway

curl -X POST https://your-api-gateway-url/prod/translate \
  -H "Content-Type: application/json" \
  -d '{
    "subject": "Test Newsletter",
    "from": "[email protected]",
    "html": "<html><body><h1>Test</h1><p>This is a test newsletter about AI and machine learning.</p></body></html>"
  }'

Expected response:

{
  "message": "Newsletter translated and sent successfully",
  "original_length": 65,
  "translated_length": 72
}

Test Email Forwarding

  1. Send a test email to your CloudMailin/SendGrid address
  2. Check CloudWatch Logs:
    aws logs tail /aws/lambda/newsletter-translator --follow
  3. Check your inbox for the translated email

📧 Email Output Format

The translated newsletter will be sent to your RECIPIENT_EMAIL with:

Subject: [RO] Original Newsletter Subject

Body:

NEWSLETTER TRADUS (EN → RO)
============================================================

Subiect original: AI Weekly Newsletter - Dec 2024

============================================================
TRADUCERE ROMÂNĂ:
============================================================

[Romanian translation here...]

============================================================
TEXT ORIGINAL (ENGLEZĂ):
============================================================

[Original English text here...]

---
Tradus automat folosind DeepL API

🔧 Troubleshooting

Issue: Lambda times out

Solution: Increase timeout in Lambda Configuration → General configuration → Timeout (set to 60-90 seconds)

Issue: "DEEPL_API_KEY not set"

Solution: Add environment variable in Lambda Configuration → Environment variables

Issue: Email not sent

Solutions:

  1. Verify both sender and recipient emails in SES
  2. Check SES is out of sandbox mode (or both emails are verified)
  3. Check CloudWatch Logs for errors:
    aws logs tail /aws/lambda/newsletter-translator --follow

Issue: "Could not extract meaningful content"

Solution: The email might be:

  • Too short (< 50 characters after cleaning)
  • Heavily image-based
  • Not properly formatted HTML

Try forwarding the raw email or check Lambda logs.

Issue: Translation quality issues

Solution: DeepL is excellent for Romanian, but if you need more control over technical terminology:

  • Consider switching to GPT-4o-mini (see RESEARCH.md for alternative implementation)
  • Can provide custom glossaries with DeepL Pro plan

📊 Monitoring & Logs

View Lambda Logs

Using AWS Console:

  1. Go to Lambda → Functions → newsletter-translator
  2. Click Monitor tab → View logs in CloudWatch

Using AWS CLI:

# Tail live logs
aws logs tail /aws/lambda/newsletter-translator --follow

# Get recent logs
aws logs tail /aws/lambda/newsletter-translator --since 1h

Check DeepL Usage

  1. Go to DeepL Account
  2. View Characters translated this month
  3. Free tier limit: 500,000 characters/month

Check SES Sending Statistics

aws ses get-send-statistics

💡 Tips & Best Practices

1. Organize Newsletters in Gmail

Create labels for different newsletter types:

AI/Newsletters/Translated
AI/Newsletters/Original

Set up filters to auto-label translated emails:

2. Cost Optimization

  • DeepL Free Tier: 500,000 chars/month

    • Average newsletter: ~5,000 words = ~6,500 characters
    • Can process ~77 newsletters/month free
  • If exceeding free tier:

    • Consider GPT-4o-mini (~$0.75 per 1M characters)
    • See RESEARCH.md for implementation

3. Handle Multiple Newsletter Sources

Update Gmail filter to include all sources:

4. Preserve Formatting

The current implementation extracts text only. To preserve HTML formatting:

  • Modify lambda_function.py to pass HTML directly to DeepL
  • DeepL API supports HTML translation with tag_handling: "html"
  • Update email sending to use HTML MIME type

5. Batch Processing

If you receive many newsletters:

  • Add SQS queue between API Gateway and Lambda
  • Process in batches to optimize API calls
  • Reduces costs and improves reliability

🔄 Updating the Lambda Function

When you make changes to lambda_function.py:

# Rebuild deployment package
./deploy.sh

# Update Lambda function
aws lambda update-function-code \
  --function-name newsletter-translator \
  --zip-file fileb://newsletter-translator-lambda.zip

Or via AWS Console:

  1. Go to Lambda → Functions → newsletter-translator
  2. Code tab → Upload from → .zip file
  3. Upload newsletter-translator-lambda.zip

🔐 Security Best Practices

  1. Never commit API keys to version control

    • Use .env for local development
    • Use Lambda environment variables for production
  2. Restrict API Gateway access (optional):

    • Add API key requirement
    • Use AWS WAF to block unauthorized IPs
    • Enable CloudWatch Logs for monitoring
  3. Use IAM least privilege:

    • Lambda execution role should only have:
      • CloudWatch Logs write access
      • SES send email access
  4. Rotate API keys periodically:

    • DeepL API keys
    • AWS access keys (if using CLI)

📚 Additional Resources


🤝 Contributing

Feel free to submit issues or pull requests for improvements!


📄 License

MIT License - feel free to use and modify for your needs.


🎉 Acceptance Criteria

✅ The project is complete when:

  1. You can forward an English AI newsletter to the webhook email
  2. The system processes it automatically (no manual intervention)
  3. A readable Romanian translation appears in your inbox within 1-2 minutes

Enjoy your automated newsletter translations! 🚀

About

Serverless newsletter translator - auto-translate emails from English to Romanian using DeepL + AWS Lambda

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages