gpt4 book ai didi

javascript - 使用 meteor js 用户注册成功后发送邮件

转载 作者:行者123 更新时间:2023-11-29 18:00:51 24 4
gpt4 key购买 nike

我想将电子邮件功能添加到我的应用程序中。我已经添加了电子邮件包并按照 documentation 中的步骤进行操作提供

我想当用户自己注册时,注册成功后应该发送一封电子邮件。

这是我尝试过的:

服务器/smtp.js:

Meteor.startup(function () {
smtp = {
username: 'abc@gmail.com', // eg: server@gentlenode.com
password: 'abc123', // eg: 3eeP1gtizk5eziohfervU
server: 'smtp.gmail.com', // eg: mail.gandi.net
port: 25
}

process.env.MAIL_URL = 'smtp://' + encodeURIComponent(smtp.username) + ':' + encodeURIComponent(smtp.password) + '@' + encodeURIComponent(smtp.server) + ':' + smtp.port;
});

这是我调用方法的 server/emp_details.js。以下代码放在 Meteor.methods() 中:

sendEmail: function (to, from, subject, text) {
check([to, from, subject, text], [String]);

// Let other method calls from the same client start running,
// without waiting for the email sending to complete.
this.unblock();

//actual email sending method
Email.send({
to: to,
from: from,
subject: subject,
text: text
});
}

最后我在客户端调用了方法,如下所示:

Template.register.onRendered(function()
{
var validator = $('.register').validate({
submitHandler: function(event)
{
var email = $('[name=email]').val();
var password = $('[name=password]').val();
var empProfile = Session.get('profileImage');
console.log(empProfile);
Accounts.createUser({
email: email,
password: password,
profile: {
name:'test',
image: empProfile
},
function(error)
{
if(error)
{
if(error.reason == "Email already exists.")
{
validator.showErrors({
email: "This email already belongs to a registered user"
});
}
}
else
{
Meteor.call('sendEmail',
'alice@example.com',
'abc@example.com',
'Hello from Meteor!',
'This is a test of Email.send.');
Router.go("home");
}
}
});
}
});
});

我不知道如何正确使用此电子邮件功能。

最佳答案

我可以建议替代解决方案吗?在服务器端向 Accounts.onCreateUser() 添加一个 Hook 以发送电子邮件:

Accounts.onCreateUser(function (options, user) {
Meteor.call(
'sendEmail',
'admin@yoursite.com',
user.profile.email, //use the path to the users email in their profile
'Hello from Meteor!',
'This is a test of Email.send.'
);
});

您真的在使用 gmail 吗?我相信 google 的 SMTP 端口是 465,而不是 25。使用该配置确认您的电子邮件发送在 meteor 之外有效,然后在 Meteor 中尝试。我还相信谷歌将通过 SMTP 发送的电子邮件数量限制为每天 99 封,所以要小心。如果您想验证用户的电子邮件地址,请使用 accounts 包中的内置电子邮件验证系统。

关于javascript - 使用 meteor js 用户注册成功后发送邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35062463/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com