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

时间:2019-01-16 18:32:11

标签: node.js angular express https nodemailer

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

我得到的错误是“混合内容:通过HTTPS加载了'https://www.MyWebsite.com/home#contact-us'上的页面,但是请求了一个不安全的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中的某些内容。

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开发已经很陌生,因此将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:0)

结果是我在看错代码。 问题不在于nodemailer,而在于我如何在服务器上为node app服务。我已经按照@candybeer指向我的帖子中的内容更新了server.js文件。 NodeJS https server returning ERR_SSL_PROTOCOL_ERROR using express

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