Help Center

Sending Email from Cloudflare Workers using MailChannels Send API

MailChannels Send API lets you send emails from your Cloudflare Workers apps. Unlike other transactional email services that lack powerful anti-abuse capabilities, MailChannels automatically identifies and blocks harmful content such as spam and phishing, letting Cloudflare Workers customers email without any domain verification or login required.

You do not need an account with MailChannels in order to start sending email. You also do not have to verify your domain with us.

How it Works

The following code snippet illustrates how you can use the MailChannels Send API to deliver email through MailChannels infrastructure to any recipient. This example renders web visitors a simple form with a "Send" button that causes an email to be sent to recipient@example.com.

addEventListener("fetch", event => {
    event.respondWith(handleRequest(event.request))
})
 
async function handleRequest(request) {
    let content = "";
    for( var i of request.headers.entries() ) {
        content += i[0] + ": " + i[1] + "\n";
    }
    let send_request = new Request("https://api.mailchannels.net/tx/v1/send", {
        "method": "POST",
        "headers": {
            "content-type": "application/json",
        },
        "body": JSON.stringify({
            "personalizations": [
                { "to": [ {"email": "recipient@example.com",
                        "name": "Test Recipient"}]}
            ],
            "from": {
                "email": "sender@example.com",
                "name": "Test Sender",
            },
            "subject": "Test Subject",
            "content": [{
                "type": "text/plain",
                "value": "Test message content\n\n" + content,
            }],
        }),
    });
 
    let respContent = "";
    // only send the mail on "POST", to avoid spiders, etc.
    if( request.method == "POST" ) {
        const resp = await fetch(send_request);
        const respText = await resp.text();
 
        respContent = resp.status + " " + resp.statusText + "\n\n" + respText;
 
    }
 
    let htmlContent = "<html><head></head><body><pre>" +
        "</pre><p>Click to send message: <form method="post"><input type="submit" value="Send"/></form></p>" +
        "<pre>" + respContent + "</pre>" +
        "</body></html>";
    return new Response(htmlContent, {
        headers: { "content-type": "text/html" },
    })
}

Frequently Asked Questions

  • Can I send email from any domain? Yes. You can send from any domain and there is no need to verify the sending domain.
  • Do I need an SPF record? If you use Sender Policy Framework (SPF) with your domain, you should add MailChannels SPF records to authorize sending from our IPs. Instructions are here: https://mailchannels.zendesk.com/hc/en-us/articles/200262610-Set-up-SPF-Records
  • How do you prevent abuse? MailChannels operates the world's largest email sending service for the web hosting industry. We have extensive anti-abuse protections built into the platform to prevent spam and phishing and identify abusive senders.
  • What if I need support? Please submit a ticket and we will be happy to assist you. Please include your Cloudflare Workers zone in the support request so that we can locate your logs.
  • Is my privacy protected? As an email service provider, we take privacy seriously.
    • Messages can be stored for up to six hours in our outgoing message queue; typically, messages are delivered within seconds.
    • After delivery, message content is permanently destroyed. We have no ability to recover messages after they have been sent.
    • Some meta-information about the message data is retained for anti-abuse purposes, including the subject line, sender, and recipient address, and your Cloudflare worker zone.
    • If you have more sophisticated needs around privacy, please contact our support team.
  • What are your terms and conditions? Use of our APIs and other services are governed by our Terms of Service.
Was this article helpful?
0 out of 0 found this helpful
Have more questions? Submit a request

Comments

  • Avatar
    Vladislav

    If you using CloudFlare Workers with your code - getting syntax error on line 42
    It seems that you need to use single quotes to mask the line with double quotes inside.
    Here is the fixed line code:

    '</pre><p>Click to send message: <form method="post"><input type="submit" value="Send"/></form></p>' +
    Edited by Vladislav
    Comment actions Permalink
  • Avatar
    Ken Simpson

    Thanks very much for this correction, Vladislav.

    Comment actions Permalink

Please sign in to leave a comment.