gpt4 book ai didi

ruby-on-rails - Rails + SendGrid 不允许未经身份验证的发件人

转载 作者:行者123 更新时间:2023-12-04 05:17:09 27 4
gpt4 key购买 nike

我目前正在通过 sendgrid 发送电子邮件,目前正在向新用户发送电子邮件。 ...但奇怪的是,非常相似的电子邮件选项不起作用 - 包括收件人在内的完整电子邮件显示在日志中,但从未收到。

这是我所拥有的:

在初始值设定项中的 mail.rb 文件中:

ActionMailer::Base.smtp_settings = {
:address => 'smtp.sendgrid.net',
:port => '587',
:authentication => :plain,
:user_name => configatron.sendgrid.username,
:password => configatron.sendgrid.password,
:domain => 'heroku.com'
}

ActionMailer::Base.delivery_method = :smtp

用户名和密码在别处定义 - 它们被确认具有正确的值

当新用户注册时,我调用我的 UserMailer 类并执行以下操作:
def welcome_email(user)
@email = user.email
@name = user.full_name

mail(to: @email, subject: "Welcome!")
end

工作人员只需执行以下操作即可调用它:
UserMailer.welcome_email(user).deliver

这很有效;我收到了电子邮件,就是这样。

但是当我尝试用不同的方法发送电子邮件时,事情神奇地崩溃了。

在 SAME UserMailer 类中:
def request(org, recipient, item)
@org = org
@recipient = recipient
@item = item

mail(to: @org.email, subject: "A new request has been posted!")
end

使用 SAME 函数(虽然这次是在 worker 之外,所以我可以轻松调试):
UserMailer.request(org, recipient, item).deliver

电子邮件完美地出现在终端中:
Sent mail to mypersonalemail@test.com (4717ms)
Date: Mon, 31 Dec 2012 01:03:35 -0800
From: mysendingemail@test.com
To: mypersonalemail@test.com
Message-ID: <[longhexstring]@localhost.localdomain.mail>
Subject: A new request has been posted!
Mime-Version: 1.0
Content-Type: multipart/alternative;
boundary="--==_mimepart_50e154e769903_3c41bba7ec576d5";
charset=UTF-8
Content-Transfer-Encoding: 7bit



----==_mimepart_50e154e769903_3c41bba7ec576d5
Date: Mon, 31 Dec 2012 01:03:35 -0800
Mime-Version: 1.0
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: 7bit
Content-ID: <[longhex]@localhost.localdomain.mail>

Hello world!

----==_mimepart_50e154e769903_3c41bba7ec576d5
Date: Mon, 31 Dec 2012 01:03:35 -0800
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
Content-ID: <[longhex]@localhost.localdomain.
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
</head>
<body>
<p>
Hello world!
</p>
</body>
</html>

但是,我在我的个人电子邮件、垃圾邮件、垃圾箱中检查了我的收件箱,但什么也没收到。甚至 sendgrid 也没有我请求发送请求电子邮件的日志!但是欢迎电子邮件运行良好,甚至通过 sendgrid 记录。我不明白是什么导致一组电子邮件发送而不是另一组电子邮件,这真是令人费解。我没有明显的语法错误,我使用的值是正确的数据类型而不是零。

对这个奇怪问题的任何帮助将不胜感激。

最佳答案

解决方案最终非常简单,所以对于那些找到这个线程并需要帮助的人,我至少会发布我所做的。

在 development.rb 中,我执行了以下操作以查看引发的错误:

config.action_mailer.raise_delivery_errors = true

并收到此消息:
Net::SMTPFatalError - 550 Cannot receive from specified address <mypersonalemail@test.com>: Unauthenticated senders not allowed

这很奇怪,因为我认为我已经通过我放入 .env 的信息进行了身份验证输出我的 configatron.sendgrid.username 肯定是 nil。仍在试图弄清楚为什么会这样,我仍然不确定欢迎电子邮件是如何发送的。

果然,将我的 smtp 设置更改为用于身份验证的硬编码值非常有用。现在问题变成了如何让 ENV 不出现零...

最终的解决方案只是我在错误的本地主机上。我在端口 3000 上使用 rails 的默认 rails 服务器,但工头开始(不确定是否默认,但在我的项目中)使用端口 5000 代替。

关于ruby-on-rails - Rails + SendGrid 不允许未经身份验证的发件人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14099925/

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