简体   繁体   English

如何使用Node.js从用户帐户发送电子邮件?

[英]How can I send emails from a user's account using Node.js?

i'm working in a program that saves contacts. 我正在使用保存联系人的程序。 I want to add a new fuction that lets you send emails to your contacts form your email account. 我想添加一个新功能,使您可以通过电子邮件帐户向联系人发送电子邮件。 I tried to do it with nodemailer, taking the login data from a form that users need to fill in. 我尝试使用nodemailer进行此操作,从用户需要填写的表单中获取登录数据。

But nodemailer sends me a error, look this: 但是nodemailer给我发送了一个错误,看起来是这样的:

Error: Invalid login: 535-5.7.8 Username and Password not accepted.

I tried with applcation password and activating the less secure option, but the error continues. 我尝试使用applcation密码并激活了不太安全的选项,但错误仍然存​​在。 Look my code: 看我的代码:

Here i take the user's email and password: 在这里,我使用用户的电子邮件和密码:

router.post('/email', isLoggedIn, async (req, res) => {
    var { email } = req.body;
    var { password } = req.body;

    var parts = email.substring(email.lastIndexOf('@')+ 1);



    exports.email = email;
    exports.password = password;

    res.redirect('/links/message')



});

Here i take de message: 我在这里留言:

router.post('/sendmessage', isLoggedIn, async (req, res) => {

    const { message } = req.body;
    const { assumpt } = req.body;
    exports.message = message;
    exports.assumpt = assumpt;
    res.redirect('/links/contactsemail')


});

Finally, i take the category contact that you need to send the email and i send the email with the message and data login that users give me: 最后,我将选择您需要发送电子邮件的类别联系人,并发送用户给我的带有消息和数据登录名的电子邮件:

router.post('/contactsend', isLoggedIn, async (req, ress) => {
    const { contacts } = req.body;
    const row = await pool.query('SELECT count(email) AS email FROM links WHERE user_id=? AND category=? AND email IS NOT NULL',[req.user.id, contacts]);
    const rows = row[0].email;
    const assumpt = myModule.assumpt;
    const message = myModule.message;
    const email = myModule.email;
    const password = myModule.password
    var parts = email.substring(email.lastIndexOf('@')+ 1);
    const hosts = 'smtp.' + parts;
    var transporter = nodemailer.createTransport({
        host: hosts,
        port: 587,
        auth: {
            user: email,
            pass: password
        }
    });


   for (i = 1; i <= rows; i++ ){
    var email1 = await pool.query('SELECT email AS email FROM (SELECT  email, ROW_NUMBER() OVER (ORDER BY email) AS rn FROM links WHERE user_id= ? AND category=?) q WHERE  rn = ?', [req.user.id, contacts, i]);
    var email1 = email1[0].email;
    var mailOptions = {
        from: email,
        to: email1,
        subject: assumpt,
        text: message

    };



        transporter.sendMail(mailOptions, function (error, info) {
            if (error) {
                console.log(error);

            } else {
                console.log('Email sent: ' + info.response);
                res.flash('success', 'Missatge enviat correctament');
                res.redirect('/links');
            }
        });


    };


});

My questions are: 我的问题是:

How can I fix the error? 我该如何解决错误?

I can do it in different way that's it's easier and better? 我可以通过不同的方式来做到这一点,因为它更容易,更好?

Am I understanding this correctly that you want your users to enter their credentials for their private/external e-mail provider so you can send an e-mail using that provider? 我是否正确理解了这一点,是否希望您的用户输入其私人/外部电子邮件提供商的凭据,以便可以使用该提供商发送电子邮件?

In that case there is no generic answer to your question since the specifics of the required configuration highly depends on the e-mail provider. 在这种情况下,由于所需配置的细节在很大程度上取决于电子邮件提供商,因此无法通用回答您的问题。 For example in your code: 例如,在您的代码中:

    var parts = email.substring(email.lastIndexOf('@')+ 1);
    const hosts = 'smtp.' + parts;
    var transporter = nodemailer.createTransport({
        host: hosts,
        port: 587,
        auth: {
            user: email,
            pass: password
        }
    });

This block already has some hard-coded assumptions, ie that the smtp host for john.doe@example.net is smtp.example.org and that the username for that e-mail is also john.doe@example.net which - both - is not necessarily the case for many e-mail providers. 该块已经有一些硬编码的假设,即john.doe@example.net的smtp主机是smtp.example.org ,该电子邮件的用户名也是john.doe@example.net ,两者- -对于许多电子邮件提供商来说不一定是这种情况。

So even if this would work for you it is very likely it won't work for all of your users ! 因此,即使这对您有用,也很可能不适用于所有用户

Instead you should use a single smtp gateway/relay your application can forward e-mails to. 相反,您应该使用单个smtp网关/中继,您的应用程序可以将电子邮件转发到该网关。 Then you also won't need the credentials of the users themselves but can use the login credentials for your application to that smtp gateway. 然后,您也将不需要用户自己的凭据,但可以将您的应用程序的登录凭据用于该smtp网关。

But it depends on the configuration of your smtp gateway whether you'll be allow to send with arbitrary sender addresses. 但是,是否允许您使用任意发件人地址发送邮件,取决于smtp网关的配置。 And even if your gateway might allow this the mails may be blocked due to spam filters since your gateway may not be allowed to send e-mails for the users' domain, see SPF . 而且即使您的网关可能允许这样做,由于可能不允许您的网关发送用户域的电子邮件,由于垃圾邮件过滤器,邮件也可能被阻止,请参阅SPF

Therefor I would advise you - besides using a dedicated smtp gateway - to send with an e-mail address/domain you own and control. 因此,除使用专用的smtp网关外,我建议您使用自己拥有和控制的电子邮件地址/域进行发送。 You may still augment the mail coming directly from the user, eg by setting the display name and Reply-To header appropriately: 您仍然可以增加直接来自用户的邮件,例如通过适当设置显示名称和Reply-To标头

From: "John Doe" <myapp@example.com>
To: jane.doe@example.org
Reply-To: "John Doe" <john.doe@example.org>
Subject: "A message from John Doe via MyApp"

For this mail the recipient would get the sending user's name displayed as sender (but your app's e-mail and not the user's) and when hitting the reply button the reply would by default - depending on the e-mail client - be sent back to the user, ie in this case john.doe@example.org . 对于此邮件,收件人将获得显示为发件人的发送用户名(但应用程序的电子邮件而非用户的名字),并且在单击“答复”按钮时,默认情况下(取决于电子邮件客户端),答复将发送回用户,即本例中的john.doe@example.org

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Node.js脚本中从Gmail帐户发送电子邮件? - How do I send email from my Gmail account in my Node.js script? SendGrid:如何使用 Node.JS 向我的联系人列表发送电子邮件? - SendGrid: How to send emails to my contact list using Node.JS? 我可以使用node.js发送命令吗? - Can I send commands using node.js? 如何使用 Pusher 将用户的存款和取款详细信息从客户端发送到 Node.js(快递)服务器? - How to send deposit and withdraw details of user from client to Node.js (express) server using Pusher? 如何将图像从Node.js发送到React.js? - How can I send an image from Node.js to React.js? 如何解压缩文件并在 Node.js 中发送响应? - How can I ungzip a file and send it in response in Node.js? 如何使用node.js express中的bodyParser通过AJAX正确发送对象? - How can I properly send Objects via AJAX using bodyParser in node.js express? 如何使用Swift将收据数据发送到Node.js服务器 - How can i send receipt data using Swift to Node.js server 如何从 promise 中提取值并通过 node.js 服务器将它们发送到客户端? - How can i extract values from promise and send them to client via node.js server? 如何将数据从 javascript 发送到 node.js TCP 服务器? - How can I send data from javascript to a node.js TCP server?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM