如何更新 nodemailer 以使用 https (SSL)

How do I update nodemailer to use https (SSL)

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

我得到的错误是 "Mixed Content: The page at 'https://www.MyWebsite.com/home#contact-us' was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint 'http://www.MyWebsite.com:3000/send'. This request has been blocked; the content must be served over HTTPS."

我将端点更改为 https ('https://www.MyWebsite.com:3000/send') and now I get a different error: "OPTIONS https://www.MyWebsite.com:3000/send net::ERR_SSL_PROTOCOL_ERROR”。 我不确定故障出在哪里,但我想我需要在后端的 server.js 运行 中更改一些内容。

NodeMailer 服务:

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 开发还很陌生,所以非常感谢任何帮助。谢谢!!

原来我看错了一段代码。 问题不在于 nodemailer,而在于我如何在服务器上提供节点应用程序。我已经根据@candybeer 向我指出的 post 中的内容更新了我的 server.js 文件。

从 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。非常感谢