How can I send mail with rails without a template?
The simplest way to send mail in rails 3 without a template is to call the mail
method of ActionMailer::Base
directly followed by the deliver
method,
For ex, the following would send a plain text e-mail:
ActionMailer::Base.mail(
from: "me@example.com",
to: "you@example.com",
subject: "test",
body: "test"
).deliver
http://api.rubyonrails.org/classes/ActionMailer/Base.html#method-i-mail gives you all the header options and also ideas about the how to send a multipart/alternative email with text/plain and text/html parts directly.
ActionMailer without template and multipart email
class TestMailer < ActionMailer::Base
def welcome_email
mail(to: 'example@example.com', subject: 'Welcome to My Awesome Site') do |format|
format.html { render html: '<h1>Welcome XYZ!</h1>'.html_safe }
format.text { render plain: 'Welcome XYZ' }
end
end
end
To use it call: TestMailer.welcome_email.deliver_now
.
Documentation, section 2.4
How to send Email sending without html format
The email client will render those tags once the email is received by the user and you will not see any of them. An email template is treated very much like a View in Rails. if you take everything between <html>
and </html>
and save it in a file and open it in a browser, that's what the email will look like for the user.
Also you want to send an absolute path in your email link, that is a relative link you have right now and if the user clicks it outside your website (from an email client) it will not work.
Rails mailer without view
I found the way by experimentation :
mail(:to => email, :subject => 'we found the answer') do |format|
format.text do
render :text => '42 owns the World'
end
end
This is also said in the Rails Guide :
http://guides.rubyonrails.org/action_mailer_basics.html at section 2.4
How to send HTML form data as email in Rails? (without using a gem)
At first, you need to install smtp
server for sending email, Heroku has an add-on for Sendgrid
email install and configure this.
Then run below command for creating email class and template
rails generate mailer ContactMailer
it will generate like below
create app/mailers/contact_mailer.rb
create app/mailers/application_mailer.rb
invoke erb
create app/views/contact_mailer
create app/views/layouts/mailer.text.erb
create app/views/layouts/mailer.html.erb
invoke test_unit
create test/mailers/contact_mailer_test.rb
create test/mailers/previews/contact_mailer_preview.rb
You can follow this for basic Rails Mailer, also here is a full tutorial
for sending an email with a contact form and mail folders structure how mail classes and templates work together.
ActionMailer sending Blank emails, not rendering templates, Rails 3
It was a silly mistake, like i knew it would be! A Git commit pointed it out...
create mode 100644 app/views/user_mailer/ welcome_email.html.erb
create mode 100644 app/views/user_mailer/ welcome_email.text.erb
Whitespace at the start of the file name, which is really hard to notice in Sublime Text!
So easy to loose 2 days on silly mistakes, But i learned lots trying to figure it out!
Related Topics
Installing Ruby 2.3.X on Ubuntu 18.04 Is Causing an Error by the End of the Installation Process
Are There Unintended Consequences of Ruby's 'Begin ... End' Without 'Rescue' Used as a Code Block
How to Split a String in Ruby and Get All Items Except the First One
How to Write Specs for Code That Depends on Environment Variables
Ruby: How to Count the Number of Times a String Appears in Another String
Capistrano & Bash: Ignore Command Exit Status
How to Open a File and Search for a Word
Getting a Dns Txt Record in Ruby
Mongodb and Mongoid in Production
Ruby Remove Empty Lines from String
Run Ruby Script in the Background