You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Heroku生成SendGrid API密钥?遇登录及配置问题求助

Heroku + SendGrid 问题解决步骤

一、无法跳转SendGrid账户 & Heroku邮箱登录提示

  • 用Heroku CLI获取关联信息:执行heroku addons:open sendgrid尝试直接打开,若失败,再运行heroku config:get SENDGRID_USERNAME,得到的appname-xxxxxx@heroku.com是SendGrid自动生成的中转邮箱,不用纠结拥有它。
  • 重置凭证并验证:登录Heroku进入SendGrid插件页,找到「Reset Credentials」重置,SendGrid会把验证邮件发到你注册Heroku的邮箱,查收邮件完成验证后,就能正常登录SendGrid后台。

二、生成SendGrid API密钥适配ActionMailer

  • 登录SendGrid后台,左侧菜单进入「Settings」→「API Keys」。
  • 点击「Create API Key」,命名后选择权限(选「Mail Send」就足够发邮件用),生成后立刻复制密钥(页面关闭后无法再查看)。
  • 配置Rails的ActionMailer(以生产环境为例):
# config/environments/production.rb
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
  address: 'smtp.sendgrid.net',
  port: 587,
  domain: '你的域名', # 替换成实际域名
  user_name: 'apikey', # 固定值,不要填用户名
  password: ENV['SENDGRID_API_KEY'], # 用环境变量存密钥更安全
  authentication: :plain,
  enable_starttls_auto: true
}
  • 把API密钥存入Heroku环境变量:执行heroku config:set SENDGRID_API_KEY="你的密钥内容",这样代码里就能通过ENV读取。

内容的提问来源于stack exchange,提问作者railsnoob

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 11:25:57