Rails – Mail, getting the body as Plain Text

The code above: message = Mail.new(params[:message]) will create a new instance of the mail gem from the full message. You can then use any of the methods on that message to get the content. You can therefore get the plain content using: message.text_part or the HTML with message.html_part These methods will just guess and find … Read more

Errno::ECONNREFUSED: Connection refused – connect(2) for action mailer

In my situation, I encountered similar problems when I was trying to making through a sending-email Rails app tutorial, the Heroku logs kept telling my that …… Errno::ECONNREFUSED (Connection refused – connect(2) for “localhost” port 25): …… After I compared my code with author’s code, I got to find out that I had not setup … Read more

Rails – How do you test ActionMailer sent a specific email in tests

As of rails 3 ActionMailer::Base.deliveries is an array of Mail::Message’s. From the mail documentation: # mail[‘from’] = ‘mikel@test.lindsaar.net’ # mail[:to] = ‘you@test.lindsaar.net’ # mail.subject ‘This is a test email’ # mail.body = ‘This is a body’ # # mail.to_s #=> “From: mikel@test.lindsaar.net\r\nTo: you@… From that it should be easy to test your mail’s in an … Read more

Sending mail with Rails 3 in development environment

Well I have resolved the issue, but quite why this works and the other methods did not, I don’t know. The solution was to create an initialiser in config/initialisers/setup_mail.rb containing the following if Rails.env != ‘test’ email_settings = YAML::load(File.open(“#{Rails.root.to_s}/config/email.yml”)) ActionMailer::Base.smtp_settings = email_settings[Rails.env] unless email_settings[Rails.env].nil? end I then added config/email.yml containing the details of the dev … Read more

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 … Read more

Send to multiple recipients in Rails with ActionMailer

In the Rails guides (Action Mailer Basics) it says the following regarding multiple emails: The list of emails can be an array of email addresses or a single string with the addresses separated by commas. So both “test1@gmail.com, test1@gmail.com” and [“test1@gmail.com”, “test1@gmail.com”] should work. See more at: http://guides.rubyonrails.org/action_mailer_basics.html

Clearing out ActionMailer::Base.deliveries after RSpec test

RSpec.describe UserMailer do before do # ActionMailer::Base.deliveries is a regular array ActionMailer::Base.deliveries = [] # or use ActionMailer::Base.deliveries.clear end it “sends welcome email” do user = create(:user) UserMailer.welcome_email(user).deliver_now expect(ActionMailer::Base.deliveries).to be_present end end

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)