【问题标题】:How to send email in rails如何在 Rails 中发送电子邮件
【发布时间】:2017-01-17 11:20:47
【问题描述】:

以下是我的控制器文件:

class BiodataController < ApplicationController
  def store
    @user=params[:username]
    @age=params[:age]
    @gender=params[:gender]
    @mstatus=params[:mstatus]
    @language=params[:language]
    @email=params[:email]
    @mobile=params[:mobile]

    if params[:username].present? && params[:age].present? && params[:gender].present? && params[:mstatus].present? && params[:language].present? && params[:email].present? && params[:mobile].present?
        Biodatum.create(name: @user, age: @age, gender: @gender, mstatus: @mstatus, language: @language, email: @email, mobile: @mobile)
    Infomail.sendmail(@email)
    render 'store'
    else
        render 'Error'
    end
  end
end

我的要求是将电子邮件发送到存储在@email 中的地址。所以我将邮件程序创建为“Infomail”。以下是我的邮件文件。

class Infomail < ApplicationMailer
    default from: 'abc@xyz.co.in'

    def sendmail(user)
        @user = user
        mail(to: @user.email, subject: 'sample mail')
    end
end

而且我在“app/views/infomail/sendmail.html.erb”下也有 html 文件。但它不起作用。任何人都可以解释我的错误是什么 我的代码。

【问题讨论】:

  • Infomail.sendmail(@email) 在这里您创建了电子邮件,但从不对其进行任何操作。试试这个:Infomail.sendmail(@email).deliver_now。此外,请检查环境配置中的电子邮件配置。

标签: ruby-on-rails ruby ruby-on-rails-4 model-view-controller actionmailer


【解决方案1】:

我建议你先看看http://guides.rubyonrails.org/action_mailer_basics.html

要发送邮件,您必须使用deliver_now(立即)或deliver_later(工作队列)方法,如下所示:

InfoMailer.sendmail(@user).deliver_now

请注意,我将其重命名为 InfoMailer,因为这是邮件程序的标准命名。

【讨论】:

  • 它显示错误为“abc@xyz.co.in”的“未定义方法`email':String”
  • 那是因为您将字符串传递为@user 而没有传递@bio
【解决方案2】:

如果您想在开发环境中测试您的邮件程序,只需打开您的 development.rb 文件并在其中写入以下代码以进行 smtp 配置。

  config.action_mailer.default_url_options  = { host: 'url of your application' }
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    address:              'address of your mailer',
    port:                 465,
    domain:               ENV['YOUR_DOMAIN_NAME'],
    user_name:            ENV['YOUR_USER_NAME'],
    password:             ENV['YOUR_PASSWORD'],
    authentication:       :plain,
    ssl:                  true,
    tls:                  true,
    enable_starttls_auto: true
  }

请根据您的邮件 smtp 设置更改上述配置。 现在根据您的要求创建您的邮件的 tamp let。文件路径如下: app -> view -> common_mailer 在这里面请制作 sendmail.html.erb 文件。

【讨论】:

  • 如果您不需要通过第二步完成,请直接使用 ActionMailer::Base 继承您的 Infomail。如下所示:类 Infomail
【解决方案3】:

您正在发送@email,这是参数中的String,而您期待Object,您可以调用.email

@bio = Biodatum.create(name: @user, age: @age, gender: @gender, mstatus: @mstatus, language: @language, email: @email, mobile: @mobile)
Infomail.sendmail(@bio)

这将传递Biodatum 实例,您可以在该实例上调用.email

为了避免混淆,您可以更改您的邮寄方式

def sendmail(bio)
  @bio = bio
  mail(to: @bio.email, subject: 'sample mail')
end

如果您要进行这些更改,您还需要在 sendmail.html 中进行更改

编辑

确保您已配置/config/environments/development.rb/config/environments/production.rb 中的设置

config.action_mailer.delivery_method = :smtp
# SMTP settings for gmail
config.action_mailer.smtp_settings = {
  :address              => "smtp.gmail.com",
  :port                 => 587,
  :user_name            => ENV['gmail_username'],
  :password             => ENV['gmail_password'],
  :authentication       => "plain",
  :enable_starttls_auto => true
}

【讨论】:

  • rails 如何知道“to”地址是什么。因为@bio 包含许多细节,如姓名、年龄、性别等......
  • 您在邮件中的@bio 上调用.email ---> @bio.email
  • 它成功渲染了“商店”页面。但它不发送任何邮件
  • 你配置action_mailer.smtp设置了吗
猜你喜欢
  • 2011-10-25
  • 1970-01-01
  • 1970-01-01
  • 2011-10-02
  • 2011-07-01
  • 1970-01-01
  • 2023-02-10
  • 2016-07-17
  • 2023-03-26
相关资源
最近更新 更多