You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Nodemailer中配置自定义域名发件邮箱?

Fixing Custom Domain Sender Issue with Nodemailer

Hey there! Let's break down why you're having trouble setting support@domain.com as your sender in Nodemailer, and how to fix it.

First, let's spot the immediate issue in your code: you've placed the auth object inside the sendMail() options, but it should actually be part of your transporter configuration. Plus, if you're authenticating with your Gmail account (zeshanvirk@gmail.com), most email providers (including Gmail) won't let you use an unrelated custom domain email as the sender unless you set up an alias first.

Here's how to resolve this step by step:

1. Use the Correct Transporter Configuration

The auth details belong to the transporter you create with nodemailer.createTransport(), not the sendMail() call. Let's cover two common scenarios:

Scenario A: Using Gmail with a Custom Domain Alias

If you want to keep using your Gmail account but send from support@domain.com, you first need to:

  • Add support@domain.com as an alias in your Gmail account (Settings > Accounts and Import > Send mail as > Add another email address)
  • Verify ownership of the custom domain email
  • Enable 2-step verification on your Gmail account and create an App Password (since regular passwords won't work with Nodemailer anymore)

Then your code should look like this:

const nodemailer = require('nodemailer');

// Create transporter with Gmail SMTP
const transporter = nodemailer.createTransport({
  service: 'Gmail',
  auth: {
    user: 'zeshanvirk@gmail.com',
    pass: 'your-gmail-app-password' // Use the app password you created
  }
});

// Send email with your custom alias as sender
transporter.sendMail({
  from: 'support@domain.com', // Must match the alias you added to Gmail
  to: 'recipient@example.com',
  subject: 'Message',
  text: 'I hope this message gets through!'
}, (err, info) => {
  if (err) console.error(err);
  else console.log(`Message sent: ${info.messageId}`);
});

Scenario B: Using Your Custom Domain's SMTP Server

If you want to authenticate directly with your support@domain.com email (no Gmail middleman), you'll need the SMTP details from your domain's email provider (like Google Workspace, Outlook 365, or a self-hosted server). Here's a typical setup:

const nodemailer = require('nodemailer');

// Create transporter with your custom domain's SMTP
const transporter = nodemailer.createTransport({
  host: 'smtp.domain.com', // Your provider's SMTP host (e.g., smtp.google.com for Workspace)
  port: 587, // Usually 587 for TLS, 465 for SSL
  secure: false, // Set to true if using port 465
  auth: {
    user: 'support@domain.com', // Your custom domain email
    pass: 'your-support-email-password' // Password for this email
  }
});

// Send email (sender matches the authenticated email)
transporter.sendMail({
  from: 'support@domain.com',
  to: 'recipient@example.com',
  subject: 'Message',
  text: 'I hope this message gets through!'
}, (err, info) => {
  if (err) console.error(err);
  else console.log(`Message sent: ${info.messageId}`);
});

2. Avoid Common Pitfalls

  • Never put auth in sendMail(): This is a common mistake—authentication is tied to the transporter, not individual emails.
  • DNS Records: For custom domains, make sure your DNS has valid SPF and DKIM records set up. These help email providers trust your sender address and avoid marking emails as spam.
  • Firewall/Port Access: Ensure your server can reach the SMTP port (587 or 465) without being blocked by a firewall.

内容的提问来源于stack exchange,提问作者Zeshan Virk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 04:21:42