问题描述
我在使用纯 Ruby 发送电子邮件时遇到问题。这是我的脚本的样子:
Mail.defaults do
delivery_method(
:smtp,address: 'smtp.sendgrid.net',port: 587,user_name: 'sendgrid_username',password: 'sendgrid_password',authentication: :login,enable_starttls_auto: false,openssl_verify_mode: "none"
)
end
Mail.deliver do
from 'Test Email'
to 'user@example.com'
subject 'Here is the image you wanted'
body "Test Email"
end
此脚本引发以下错误:
/Users/mateuszurbanski/.rubies/ruby-2.7.2/lib/ruby/2.7.0/net/smtp.rb:975:in `check_auth_response': 535 Authentication Failed: Bad username / password (Net::SMTPAuthenticationError)
我正在使用来自我的 Ruby on Rails 项目之一的凭据,它们很好。有什么想法吗?
解决方法
Sendgrid 最近过渡到 api 密钥,将拒绝普通身份验证,请参阅 details here。 可能不接受您仍然有效的旧凭据。
在 sendgrid 中生成一个新的 api 密钥并用它代替密码。用户名将是 apikey
不要在此配置中使用您的 SendGrid 用户名和密码,而是使用以下内容,
user_name: 'apikey',password: '<sendgrid_api_key>',
在为 ActionMailer 定义 smtp 设置时这对我有用,所以它也应该在这里工作。