Send email using Node.js Hosting
Node.js Hosting comes with an email gateway you can use to send transactional emails, contact form notifications, sign-up confirmations, password resets, and receipts, straight from your code. You don’t need an SMTP server, username, password, or third-party email service. Your code sends an HTTP request to the gateway, and the gateway handles delivery for you.
Select a link to see its answer:
- How does Node.js Hosting email work?
- Example code for sending email
- Email code requirements and options
- How do attachments work?
- My email isn't working
How does Node.js Hosting email work?
Your app sends a POST request with a JSON body to the endpoint, http://127.0.0.1:2525/api/email/send. The gateway will check the message, build the email, and deliver it. The gateway can only be reached from your app’s code. It can’t be reached from the internet, from your browser, or from your own computer.
How can I send email?
You'll need to create a file on your server and call it. Below is an example you can use.
const response = await fetch("http://127.0.0.1:2525/api/email/send", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({
to: ["you@example.com"],
subject: "Hello from my Node.js app",
text: "It works! This email was sent from my app on GoDaddy Node.js Hosting.",
}),
});
const result = await response.json();
console.log(result); // { success: true, messageId: "..." }
Email code requirements and options
Below are email code requirements and some common optional fields you can include.
| Campo | Obligatorio | Descripción |
|---|---|---|
to | Sí | Recipient addresses. At least one is required. |
subject | Sí | The subject line |
text or html | At least one (both are ok) | Text or HTML body |
cc | No | Carbon-copy recipients |
bcc | No | Blind-copy recipients |
replyTo | No | Where replies go (for example, the visitor that filled out a contact form) |
attachments | No | Files to attach |
contentType | No | How to interpret attached files |
from | No | Sender address. A default user will be used if left blank. For the best reliability, enter an email address that uses the same domain as your Node.js Hosting and can receive messages (for example, jane@coolexample.com). |
How do attachments work?
When sending email with attachments, there are requirements to keep mind.
| Campo | Obligatorio | Descripción |
|---|---|---|
filename | Sí | The file name the recipient sees (for example, invoice.pdf) |
content | Sí | The file’s contents. |
contentType | No | The MIME type. |
When sending email with attachments, there are requirements to keep in mind.
- Maximum 10 attachments per message
- 2MB per attachment
- Attachment files cannot contain line breaks, quotes, or backslashes
- Text-based files (for example, calendar invites) require converting the string:
Buffer.from(text, 'utf-8')
My email isn't working
Below are some common errors that can occur and what they mean.
| Error | Descripción |
|---|---|
Any status that's not 200 | Failure. |
| My email was accepted but not delivered | A successful call means the gateway accepted the message, but delivery happens out of your app or site. Check the spam mailbox of the recipient or log the messageId to trace delivery issues. |
| email ends in spam mailbox | Attach a custom domain so mail comes from your own domain. Send both text and html versions of your email. |
email send failed | Read the error text. Common causes can be a missing field (to, subject, text and html or the from address is invalid. |
email gateway unreachable: timed out after 30000ms | No response within 30 seconds. If multiple attempts fail, contact a GoDaddy Guide for help. |
email gateway unreachable | Make sure the code runs on the server of your deployed app (not in the browser or your own computer). |