我找不到任何特定于使用 nodemailer 從 Dreamhost 發送電子郵件的有用帖子。我確實找到了一篇關于通過 PHP 發送電子郵件的Dreamhost 文章,所以也許這可以幫助我們指明正確的方向......
無論哪種方式,這是我的mailer.ts檔案:
import { createTransport, getTestMessageUrl } from "nodemailer";
import { frontendURL } from "./urls";
var transport = createTransport({
host: "smtp.dreamhost.com",
port: 465,
auth: {
user: encodeURIComponent(process.env.PROD_MAIL_USER),
pass: encodeURIComponent(process.env.PROD_MAIL_PASS),
},
secure: true,
});
function generateHTML(resetToken: string) {
return `
<div>
<h2>Hello from HaBits ??</h2>
<p>
Your <b>Password Reset Token</b> is here!
</p>
<h3>
<a href="${frontendURL}/reset?token=${resetToken}" style="color: #75B748">Reset your password.</a>
</h3>
<p>HaBits ?</p>
</div>
`;
}
export interface MailResponse {
accepted?: string[] | null;
rejected?: null[] | null;
envelopeTime: number;
messageTime: number;
messageSize: number;
response: string;
envelope: Envelope;
messageId: string;
}
export interface Envelope {
from: string;
to?: string[] | null;
}
export async function sendPasswordResetEmail(
resetToken: string,
to: string
): Promise<void> {
// email the user a token
const info = await transport.sendMail({
to,
from: process.env.PROD_MAIL_USER,
subject: "? HaBits – password reset token",
html: generateHTML(resetToken),
});
}
生產中的錯誤回應:
{
"errors": [
{
"message": "Invalid login: 535 5.7.8 Error: authentication failed: ",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"sendUserPasswordResetLink"
],
"extensions": {
"code": "INTERNAL_SERVER_ERROR",
"exception": {
"code": "EAUTH",
"response": "535 5.7.8 Error: authentication failed: ",
"responseCode": 535,
"command": "AUTH PLAIN",
"stacktrace": [
"Error: Invalid login: 535 5.7.8 Error: authentication failed: ",
" at SMTPConnection._formatError (/workspace/backend/node_modules/nodemailer/lib/smtp-connection/index.js:784:19)",
" at SMTPConnection._actionAUTHComplete (/workspace/backend/node_modules/nodemailer/lib/smtp-connection/index.js:1536:34)",
" at SMTPConnection.<anonymous> (/workspace/backend/node_modules/nodemailer/lib/smtp-connection/index.js:540:26)",
" at SMTPConnection._processResponse (/workspace/backend/node_modules/nodemailer/lib/smtp-connection/index.js:947:20)",
" at SMTPConnection._onData (/workspace/backend/node_modules/nodemailer/lib/smtp-connection/index.js:749:14)",
" at TLSSocket.SMTPConnection._onSocketData (/workspace/backend/node_modules/nodemailer/lib/smtp-connection/index.js:189:44)",
" at TLSSocket.emit (node:events:390:28)",
" at addChunk (node:internal/streams/readable:315:12)",
" at readableAddChunk (node:internal/streams/readable:289:9)",
" at TLSSocket.Readable.push (node:internal/streams/readable:228:10)",
" at TLSWrap.onStreamRead (node:internal/stream_base_commons:199:23)"
]
}
},
"name": "GraphQLError"
}
]
}
Dreamhost“支持”剛剛給我發送了一篇關于 PHP 的文章,它建議使用,host: "ssl://smtp.dreamhost.com"但只回傳一個不太有用的“找不到地址”錯誤:
錯誤:getaddrinfo ENOTFOUND ssl://smtp.dreamhost.com
所以我相當有信心 nodemailer 配置是正確的——即。主機、埠、用戶和密碼??
uj5u.com熱心網友回復:
這是我自己的愚蠢錯誤???♂?
這是在生產中發送電子郵件的 nodemailer 配置(假設您有一個由 Dreamhost 托管的電子郵件):
import { createTransport, getTestMessageUrl } from "nodemailer";
var transport = createTransport({
host: "smtp.dreamhost.com",
port: 465,
auth: {
user: process.env.PROD_MAIL_USER, // your email address
pass: process.env.PROD_MAIL_PASS, // your webmail password
},
secure: true,
logger: true,
debug: true,
});
// ...
await transport.sendMail({ ... })
我的問題是encodeURIComponent()包裹在我的電子郵件和密碼周圍——所以電子郵件[email protected]被作為[email protected].
上面的logger和debugger屬性是可選的,但它們幫助我確定了服務器日志中的問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/312925.html
