Contact Form Mailer in Rails 4

Contact Form Mailer in Rails 4

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.

Action mailer - Sending an email through a contact form with Ruby on Rails

Gmail used to allow simply passing your valid email and password via a Rails app in order to send emails. But they killed that functionality off, and require that you configure an App Specific Password for this purpose:

https://support.google.com/accounts/answer/185833?hl=en

I think the change came in 2018 or so, I remember that our Rails app was humming along just fine and suddenly it stopped sending messages. At that time, it wasn't widely announced by Gmail that they had made this change, if announced at all, which caught us off guard.

Create your app specific password and use those credentials. See if that starts sending emails for you. Your code looks fine.

I've never been able to successfully use a Yahoo account with a Rails app by the way.

Add a contact form to Hartl's tutorial

4) app\controllers\static_pages_controller (or another location?)

This seems to be correct, if this is the github repo for said app.


def send_contact_form_email

You controller has an issue: this action will try to send the email, not matter if it's used in POST or GET. You should use two different actions, one for displaying the view (using GET), and one for sending the email (using the mailer class you created). (at this point, you might want to create another controller)


ContactFormMailer.send_contactform_email(visitor).deliver_now

Then, moving on: what you pass to your mailer is "visitor". There's no such variable.
You probably want to access something out of the params hash (which contains parameters for GET and POST requests), and use the same key as your form (form_for(:visitor ... => params[:visitor] (so you want to change that :static_pages)).

<p>On <$= ... %> <%= "#{@visitor.name} (#{@visitor.email} sent the following message:" %></p>

As this returns an object, and not a hash, @visitor.email needs to be @visitor[:email] inside the mailer.


One last thing: simply using params[:visitor] will mean people could leave the field blanks. You might want to look into strong parameters, that were added in Rails 4 (the book seems somewhat outdated?).

And lastly, you need to add routes to be able to reach these actions (one for the GET request - display the view - and one for the POST request - to submit the form).


PS:

mail( :to => myemail@example.com, :from => visitor.email, :subject => 'Contact form' )

Warning: here, you forgot to quote the email address. Also, you swapped the to/from parameters. You want to send TO your visitor email, not from it.


EDIT

params.require(:message).require(:name, :email, :content)

This will require said keys, but AFAIK on the same "level" as :message - the top one. You want to use permit:

params.require(:message) # require "namespace"
.permit(:name, :email, :content) # permit keys

    @message = message(message_params)

Where is the message function defined?

mail to: "myemail@example.com"
mail from: @message[:email]
mail subject: "Message from #{message[:name]}"

This sends 3 different emails, since you called the mail function 3 times.

Rails - Mail_form how to automatically set from field to current_user email

Trh this:

In your, app/views/contacts/new.html.erb make email field as hidden field and use current_user.email as value of hidden email field should work.

Note: I am assuming you are using Devise gem

Rails contact form send email to myself

You can clone the git repo at
https://github.com/RailsApps/learn-rails
to get the code from the book Learn Ruby on Rails. You'll see that the code works as implemented.

If you look at the example code, the SMTP settings are configured in the file config/environments/development.rb and config/environments/production.rb.

config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
domain: "example.com",
authentication: "plain",
enable_starttls_auto: true,
user_name: ENV["GMAIL_USERNAME"],
password: ENV["GMAIL_PASSWORD"]
}

The GMAIL_USERNAME and GMAIL_PASSWORD set up the SMTP origin for the mail.

The UserMailer code only creates (part of) the header and body of the email message. The "from" and "to" will be displayed, for appearances only. Have a look at the raw email message and you will see the full set of headers, that show the real origin of the email.

So, in short, the UserMailer code sets a fake "from" and the real "from" is set when the email is sent from the Gmail account.

Rails 4 sending email to other users

I can't see a way to set a value for default to: as the receiver email changes for different users(ie., they are dynamic). And if I'm not wrong the value for default to: should be static.

I want the user to be able to send to whichever email link they click
on

Instead change your code to below

<% if user.email.present? %>
<b>Email:</b>
<%= link_to "email", contact_path(to_email: user.email) %> #pass an extra parameter
<% end %>

And in the form, make a hidden field like below

<%= hidden_field_tag 'to_email', params[:to_email] %>

So now you can access it like params[:to_email] in the controller action.

def send_mail
name = params[:name]
email = params[:email]
body = params[:comments]
to_email = params[:to_email]
#pass to_email as argument to this method
UserMailer.contact_email(name, email, body, to_email).deliver_now
redirect_to contact_path, notice: 'Message sent'
end

And finally the mailer method will be like below

class UserMailer < ActionMailer::Base

def contact_email(name, email, body, to_email)
@name = name
@email = email
@body = body

mail(from: email, subject: '', to: to_email)
end
end

how to create a contact form emailer in rails 4 when the web page is on the client side

First your application has a weird design...

If you still want to do this, you have to make your form point to a custom action which will send the mail.

in order to do that run first

rails g controller contact deliver

to generate the controller, then change the route in config/routes.rb

get 'contact/deliver'

to

post 'contact/deliver'

Next generate the mailer

rails g mailer contact_mailer contact

you can now create the action in app/controllers/contact_controller.rb to send the mail :

protect_from_forgery except: :deliver
def deliver
ContactMailer.contact(params.permit(:email, :subject, :message))
redirect_to :back
end

and define your mailer in app/mailers/contact_mailer.rb

def contact(message)
@email = message[:email]
@subject = message[:subject]
@message = message[:message]

mail to: "me@me.org"
end

You can finally pass your instance variables to the view in app/views/contact_mailer/contact.text.erb

Hope this will help :)

Send email with rails 4, mail form, and ajax?

First step, add this config.action_mailer.perform_deliveries = true to config/environments/development.rb

With that you can send emails in development mode.

Second step rails 4 use strong parameters, this means that you need to whilelist your parameters so in your controller add the following method:

def contacts_params
params.require(:contact).permit(:name, :email, :message)
end

then change this line @contact = Contact.new(params[:contact]) with @contact = Contact.new(contacts_params)

Please do a try with these changes.

Edit:

In your routes you are calling to 'contacts#new' instead of 'contacts#create' so your code is not reached.

Also if you do not need remote: true since you are handling the submit stuff manually

Rails contact form not sending email

Add following smtp setting on config/application.rb file:

  config.action_mailer.default_url_options = { :host => 'http://www.xxxx.com' }
config.action_mailer.delivery_method = :smtp

config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "gmail.com",
:user_name => "xxxxxxxxx@gmail.com",
:password => "xxxxxxxx",
:authentication => "plain",
:enable_starttls_auto => true
}


Related Topics



Leave a reply



Submit