简体   繁体   English

如何使用 gem devise&mailgun 发送电子邮件确认

[英]How to send email confirmation using gem devise&mailgun

I am having troubles with sending email confirmation using devise and mailgun.我在使用 devise 和 mailgun 发送电子邮件确认时遇到问题。 I have installed devise and changed setting to send email but whenever I signup as a newcomer there is no confirmation email sent.我已经安装了设计并更改了发送电子邮件的设置,但是每当我以新人身份注册时,都不会发送确认电子邮件。 Can anyone specifically explain how to do it?谁能具体解释一下怎么做? Thanks.谢谢。

config/environments/developments.rb配置/环境/developments.rb

Try this ...........试试这个…………

In your Gemfile在你的 Gemfile 中

gem 'mailgun_rails'宝石'mailgun_rails'

To configure your Mailgun credentials place the following code in the corresponding environment file (development.rb, production.rb...)要配置您的 Mailgun 凭据,请将以下代码放入相应的环境文件(development.rb、production.rb...)

config.action_mailer.delivery_method = :mailgun
config.action_mailer.mailgun_settings = {
        api_key: '<mailgun api key>',
        domain: '<mailgun domain>'
}

Hope this will work for you.希望这对你有用。

Just recently had this problem, and figured it out. 就在最近有这个问题,并解决了。 First you wanna setup your config/environments/production.rb file like so: 首先,您要像这样设置config / environments / production.rb文件:

config.action_mailer.delivery_method = :mailgun
config.action_mailer.mailgun_settings = {
 api_key: ENV["MAILGUN_API_KEY"], #this should stored in an ENV variable
 domain: "mail.your_domain.com" #the "mail" can be left out or replaced with whichever subdomain you've configured for outgoing email. I use "mail", others use "mg".
}

Your config/initializers/devise.rb should have a line like 您的config / initializers / devise.rb应该有如下一行

config.mailer = "MyMailer" 

Now you will need override the Devise method called send_on_create_confirmation_instructions . 现在,您将需要覆盖名为send_on_create_confirmation_instructions的Devise方法。 You should put this code in your User model. 您应该将此代码放在User模型中。

class User < ApplicationRecord
 def send_on_create_confirmation_instructions
  MyMailer.confirmation_instructions(user, token).deliver #If you're not using Sidekiq or anything else, you should just call the MyMailer class directly.
 end
end

Then you would edit that confirmation_instructions() method in MyMailer like so: 然后,您可以像这样在MyMailer编辑该MyMailer confirmation_instructions()方法:

class MyMailer < Devise::Mailer
 require 'mailgun-ruby' #this needs to be required
 include Devise::Controllers::UrlHelpers
 default template_path: 'users/mailer'

 def confirmation_instructions(record, token, opts={})
  mg_client = Mailgun::Client.new ENV['MAILGUN_API_KEY']
  message_params = {
   from: "whatever_email@your_domain.com",
   to: record.email,
   subject: "Please confirm your account",
   html: super
  }

  mg_client.send_message "mail.your_domain.com", message_params #don't forget the "mail." for the sending domain
 end
end

Devise docs say to add super to the end of the confirmation_instructions() method, but since message_params needs an html or text field for this to send, you would do it like html: super . Devise文档说要在confirmation_instructions()方法的末尾添加super ,但是由于message_params需要一个htmltext字段才能发送此消息,因此您可以像html: super Whatever template is at users/mailer/confirmation_instructions should then send to the email stored in record 无论users/mailer/confirmation_instructions发件人users/mailer/confirmation_instructions模板是什么,都应将其发送到record存储的电子邮件中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM