首页 > 解决方案 > 如何更新 nodemailer 以使用 https (SSL)

问题描述

我正在建立一个有角度的网站,我需要做的最后一件事是修复联系表格。我让它在网站的不安全版本上工作,但由于通过 URL 重写规则强制用户使用 https 版本,我不再能够发送电子邮件。

我得到的错误是“混合内容:' https://www.MyWebsite.com/home#contact-us '的页面是通过 HTTPS 加载的,但请求了一个不安全的 XMLHttpRequest 端点' http://www.MyWebsite。 com:3000/send '。此请求已被阻止;内容必须通过 HTTPS 提供。"

我将端点更改为 https (' https://www.MyWebsite.com:3000/send '),现在我得到一个不同的错误:“OPTIONS https://www.MyWebsite.com:3000/send net::ERR_SSL_PROTOCOL_ERROR ”。我不确定问题出在哪里,但我想我需要更改在后端运行的 server.js 中的某些内容。

节点邮件服务:

import { Injectable } from "@angular/core";
import { HttpClient, HttpHeaders } from "@angular/common/http";
@Injectable({
    providedIn: "root"
})
export class NodeMailerService {
    constructor(private http: HttpClient) {}

    sendEmail(name, email, phone, message) {
        const url = "https://www.MyWebsite.com:3000/send";

        const obj = {
            name: name,
            email: email,
            phone: phone,
            message: message
        };

        return this.http.post(url, obj, {
            headers: new HttpHeaders({ "Content-Type": "application/json" }),
            responseType: "text"
        });
    }
}

server.js:

const express = require("express");
const nodemailer = require("nodemailer");
const app = express();
const port = 3000;
const bodyParser = require("body-parser");

const hostServer = "mail.MyWebsite.com";
const receiverAddress = ["website@MyWebsite.com"]; // Enter the email address to send the emails TO
const userAccount = "website@MyWebsite.com"; // Enter the email address to send the emails FROM
const userPass = "Password1" // Enter the password for the above user account

const transporter = nodemailer.createTransport({
    host: hostServer,
    port: 465,
    secure: true,
    auth: {
        user: userAccount,
        pass: userPass
    },
    tls: {
        ciphers:'SSLv3',
        rejectUnauthorized: false
    }
});

app.use(bodyParser.json());

app.use(function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header(
        "Access-Control-Allow-Headers",
        "Origin, X-Requested-With, Content-Type, Accept"
    );
    next();
});

app.post("/send", function(req, res) {
    let senderName = req.body.name;
    let senderEmail = req.body.email;
    let senderPhone = req.body.phone;
    let messageSubject = "Website - Contact Request";

    let messageText = "Name: " + senderName + "\n";
    messageText += "Email: " + senderEmail + "\n";
    messageText += "Phone: " + senderPhone + "\n";
    messageText += "Message: " + req.body.message + "\n";

    let mailOptions = {
        to: receiverAddress,
        from: senderEmail,
        subject: messageSubject,
        text: messageText,
        replyTo: senderEmail
    };

    transporter.sendMail(mailOptions, function(error, response) {
        if (error) {
            console.log(error);
            res.end("error");
        } else {
            console.log("Message sent: ", response);
            res.end("sent");
        }
    });
});

app.listen(port, function() {
    console.log("Express started on port: ", port);
});

我将其更新为使用 secure: true 但我仍然收到与以前相同的错误消息。

我错过了什么,还是我什至找对了地方?我对这种全栈 Web 开发还很陌生,所以任何帮助都将不胜感激。谢谢!!

标签: node.jsangularexpresshttpsnodemailer

解决方案


原来我正在为此查看错误的代码。问题不在于 nodemailer,而在于我如何在服务器上提供节点应用程序。我已经根据@candybeer 指向我的帖子中的内容更新了我的 server.js 文件。NodeJS https服务器使用express返回ERR_SSL_PROTOCOL_ERROR

从 Windows 证书管理器中,我能够将证书导出到 pfx 文件,使用在线工具将其转换为 .PEM 文件,然后将它们放在 server.js 文件夹中,并与 https 一起使用它们来监听端口。

const https = require('https');
const fs = require("fs");

const options = {
    key: fs.readFileSync('key.pem'),
    cert: fs.readFileSync('cert.pem')
}

// app.listen(port, function() {
//     console.log("Express started on port: ", port);
// });

https.createServer(options, app).listen(port, function() {
    console.log("Express started on port: ", port);
});

谢谢@candybeer。非常感激


推荐阅读