簡體   English   中英

如何使用Sendgrids API發送電子郵件以使用rails 4發送電子郵件? 我似乎把所有的東西放在一起

[英]How do I send emails using Sendgrids API to send emails with rails 4? I can seem to put all the pieces together

我想從谷歌雲平台實例發送帶有sendgrids API的電子郵件。 他們有一個關於如何執行此操作簡短教程,但它有電子郵件消息,來自發送者,發送者以及app.rb其他信息,這不是在rails應用程序中發送消息的正常方式。

我查看了sendgrid ruby​​文檔 ,他們也沒有很好的信息。 在一個地方的所有信息,他們不說明要放入哪些文件,甚至提及要使用的任何smtp設置。

這是我到目前為止所擁有的

development.rb

config.action_mailer.delivery_method = :smtp
# SMTP settings
config.action_mailer.smtp_settings = {
 :address              => "smtp.sendgrid.net",
 :port                 => 465,
 :domain               => "mydomain.com",
 :user_name            => ENV['sendgrid_username'],
 :password             => ENV['sendgrid_password'],
 :authentication       => :plain,
 :ssl                  => true,
 :enable_starttls_auto => true
}

的Gemfile

gem "sendgrid-ruby"

電子郵件視圖在app / views中,郵件程序方法在app / mailer中與普通rails 4應用程序設置相同。

所以我想這是我的主要問題:

  1. 我在哪里調用包含sendgrid api密鑰的環境變量?
  2. 我在哪里告訴Action Mailer使用sendgrid或SendGrid::Mail就像我在幾個地方看到的那樣?
  3. 我甚至需要sendgrid寶石嗎?
  4. 使用sendgrid通過SSL發送smtp設置是否正確?

我是新手在rails應用程序中發送這樣的電子郵件,非常感謝幫助。

apikey作為名稱,將實際的apikey作為密碼:

config.action_mailer.smtp_settings = {
  address:        "smtp.sendgrid.net",
  port:           587,
  domain:         "yourwebsite.com",
  authentication: :plain,
  user_name:      'apikey',
  password:       Rails.application.secrets.sendgrid_api_key
}

資源

  1. 使用Figaro gem: https//github.com/laserlemon/figaro它將為您生成一個application.yml文件,您可以在其中存儲sendgrid_username和sendgrid_password。

  2. 你有沒有生成郵件? 例如,生成郵件程序user_notifier,您可以在其中定義默認的電子郵件,以及一些類似這樣的方法:

[R

# send a signup email to the user, pass in the user object that contains the 
user email address
default :from => 'hello@yourdomain.com'
def send_signup_email(user)
    @user = user
    mail( :to => @user.email,
        :subject => 'Thanks for signing up!'
    )
end
  1. 不,你不需要它。
  2. https://sendgrid.com/docs/Classroom/Basics/Email_Infrastructure/smtp_ports.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM