Rails 4 中的联系表单邮件程序 [英] Contact Form Mailer in Rails 4

查看:28
本文介绍了Rails 4 中的联系表单邮件程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Rails 4 中构建一个联系表单,其中表单接受姓名、电子邮件和正文并将其发送到我的电子邮件地址.点击提交"后,应用正确重定向回联系方式"页面,但似乎没有发送电子邮件.

routes.rb

匹配'/send_mail',到:'contact#send_mail',通过:'post'

contact_email.html.erb

<头><%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" =>真%><%= javascript_include_tag "application", "data-turbolinks-track" =>真%><meta name="viewport" content="width=device-width, initial-scale=1.0"><身体><p>您收到了来自 <%= "#{ @name } (#{ @email }):" %></p>的以下电子邮件<p><%=@body%></p>

contact_controller.rb

def send_mail名称 = 参数 [:名称]电子邮件 = 参数 [:电子邮件]body = params[:comments]ContactMailer.contact_email(name, email, body).deliverredirect_to contact_path,注意:'消息已发送'结尾

contact_mailer.rb

class ContactMailer 

contact.html.erb

<div class="容器"><%= form_tag(send_mail_path) do %><div class="form-group"><%= label_tag '姓名', '姓名' %><%= text_field_tag 'name', nil, class: 'form-control', placeholder: 'Your Name' %>

<div class="form-group"><%= label_tag 'email', 'Email' %><%= email_field_tag 'email', nil, class: 'form-control', placeholder: 'Your Email Address' %>

<div class="form-group"><%= label_tag 'comments', 'Comments' %><%= text_area_tag 'comments', nil, class: 'form-control', rows: 4, placeholder: 'Comments...' %>

<%= submit_tag nil, class: 'btn btn-default btn-about pull-right' %><%结束%>

application.rb

config.action_mailer.delivery_method = :sendmailconfig.action_mailer.perform_deliveries = trueconfig.action_mailer.raise_delivery_errors = true

解决方案

原来我的 Heroku 应用程序没有配置外部 SMTP 服务(因为我之前从未对电子邮件做过任何事情,所以我不知道去做这个).由于我对 MailChimp 比较熟悉,特别是因为他们的 Mandrill 服务有一个免费套餐(我正在为一个学生组织构建这个应用程序),我很容易地将 Mandrill 添加到我的 Heroku 应用程序中,并在 应用程序中包含了以下设置.回复:

ActionMailer::Base.smtp_settings = {地址:'smtp.mandrillapp.com',端口:587,用户名:ENV['MANDRILL_USERNAME'],密码:ENV['MANDRILL_APIKEY']}

加载项自动设置 ENV 变量的位置.

I am trying to build a contact form in Rails 4, where the form takes a name, email, and body and sends it to my email address. Upon clicking "Submit", the app redirects back to the Contact page correctly, but no email appears to get sent.

routes.rb

match '/send_mail', to: 'contact#send_mail', via: 'post'

contact_email.html.erb

<!DOCTYPE html>
<html>
    <head>
        <%= stylesheet_link_tag    "application", media: "all", "data-turbolinks-track" => true %>
        <%= javascript_include_tag "application", "data-turbolinks-track" => true %>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <p>You have received the following email from <%= "#{ @name } (#{ @email }):" %></p>
        <p><%= @body %></p>
    </body>
</html>

contact_controller.rb

def send_mail
    name = params[:name]
    email = params[:email]
    body = params[:comments]
    ContactMailer.contact_email(name, email, body).deliver
    redirect_to contact_path, notice: 'Message sent'
end

contact_mailer.rb

class ContactMailer < ActionMailer::Base
    default to: # my email address

    def contact_email(name, email, body)
        @name = name
        @email = email
        @body = body`enter code here`

        mail(from: email, subject: 'Contact Request')
    end
end

contact.html.erb

<div class="container-content">
    <div class="container">
        <%= form_tag(send_mail_path) do %>
            <div class="form-group">
                <%= label_tag 'name', 'Name' %>
                <%= text_field_tag 'name', nil, class: 'form-control', placeholder: 'Your Name' %>
            </div>
           <div class="form-group">
               <%= label_tag 'email', 'Email' %>
               <%= email_field_tag 'email', nil, class: 'form-control', placeholder: 'Your Email Address' %>
           </div>
           <div class="form-group">
               <%= label_tag 'comments', 'Comments' %>
               <%= text_area_tag 'comments', nil, class: 'form-control', rows: 4, placeholder: 'Comments...' %>
           </div>
           <%= submit_tag nil, class: 'btn btn-default btn-about pull-right' %>
       <% end %>
  </div>
</div>

application.rb

config.action_mailer.delivery_method = :sendmail
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true

解决方案

It turned out that I hadn't configured my Heroku app with an external SMTP service (since I have never done anything with email before, I didn't know to do this). Since I'm relatively familiar with MailChimp, and especially since their Mandrill service has a free tier (I am building this app for a student organization), I easily added Mandrill to my Heroku app and included the following settings in application.rb:

ActionMailer::Base.smtp_settings = {
    address: 'smtp.mandrillapp.com',
    port: 587,
    user_name: ENV['MANDRILL_USERNAME'],
    password: ENV['MANDRILL_APIKEY']
}

Where the ENV vars were set automatically by the add-on.

这篇关于Rails 4 中的联系表单邮件程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆