How to disable Premailer for individual emails - ruby-on-rails

We have a couple of instances where we don't want Premailer to process the emails in our rails project.
Is it possible to turn off Premailer for individual triggers? I have tried the following without success:
mail :to => user.email, :subject => subject, :premailer => false

replace :premailer => false with skip_premailer: true

Related

Mail gem not sending emails after upgrading to Rails 5.0.6

I upgraded my Rails app to 5.0.6 from 4.2.5. Now emails are not being sent by the Mail gem. I don't seem to be getting errors. I updated the Mail gem to the latest version with no luck. I am not sure what else to try.
I am running:
Rails 5.0.6
Ruby 2.3.0
Mail 2.7.0
controllers/send_email.rb
class SendEmail < ApplicationController
def initialize(to_address, from_address, email_pass, subject, body)
begin
options = {
:address => 'abc.prod.1234.secureserver.net',
:port => '465',
:domain => 'mydomain.com',
:user_name => from_address,
:password => email_pass,
:authentication => :login,
:ssl => true,
:openssl_verify_mode => 'none'
}
Mail.defaults do
delivery_method :smtp, options
end
Mail.deliver do
to to_address
from from_address
subject subject
body body
end
puts("\nSent message. From: #{from_address} To: #{to_address} \nMessage body: \n#{body}")
return true
rescue Exception => e
puts e.to_s
return false
end
end
end
Update:
I tried sending an email with Action Mailer as well. It says it sent in the console but it never gets delivered. I am using similar settings for Action Mailer as I am for the Mail gem.
config/environments/development.rb
config.action_mailer.smtp_settings = {
:address => 'abc.prod.1234.secureserver.net',
:port => '465',
:domain => 'mydomain.com',
:user_name => ENV['default_username'],
:password => ENV['default_password'],
:authentication => :login,
:ssl => true,
:openssl_verify_mode => 'none'
}
I am running this from the console:
EmailMailer.sample_email(UserEmail.first)
Console output:
Rendering email_mailer/sample_email.html.erb within layouts/mailer
Rendered email_mailer/sample_email.html.erb within layouts/mailer (0.1ms)
Rendering email_mailer/sample_email.text.erb within layouts/mailer
Rendered email_mailer/sample_email.text.erb within layouts/mailer (0.0ms)
EmailMailer#sample_email: processed outbound mail in 9.4ms
Solution:
I have some code that sends an email when there is an unknown error that I need to look at. When I updated Rails that code got caught in a loop that sent a bunch of emails really fast. That caused my server provider to mark the email account as a spam account. I am not sure why my original code was not showing any errors but when I ran EmailMailer.sample_email(UserEmail.first).deliver_now it gave me an error message that helped me track it down.
Well, wild guessing here but try this:
EmailMailer.sample_email(UserEmail.first).deliver_now
If you had Rails version below 4.2.1 it was not necessary to call deliver on the mail object for it to be delivered. From that point on you can operate on the mail object before delivering it, which can be now using
.deliver_now or later, using .deliver_later, which goes along with the use of ActiveJob or other queueing library.

How to retrieve emails from multiple emails addresses using pop3 of gem mail in Rails?

in my project I will have to write different cron jobs to read emails from different email addresses to do different tasks. I am using mail gem but the problem is, retriever_method is singleton. So when I'll mention the new email address and new password the previous settings of retriever_method will be changed. So, I am not able to retrieve the emails when cron jobs are running at the same time.
Suppose, In my first cron job I have something like the following settings
Mail.defaults do
retriever_method :pop3, :address => "pop.gmail.com",
:port => 995,
:user_name => '<username1>',
:password => '<password1>',
:enable_ssl => true
end
In my second cron job, If I use something like
:user_name => '<username2>',
:password => '<password1>'
In that case both will be changed to username2
is there any workaround.
Or any other suggestion to do this job. I don't want to IMAP for some other reason.
Any suggestion will be appreciated.
Mail retrieve_method is singleton. so I am using Net::IMAP directly with the Mail to retrieve the attachment. One can use Net::Pop3 with Mail as well to resolve this issue.

Checking Mail Bock with Rails

is there any possibility to check a normal mail box with rails?
I want to check if there are new incoming mails, I want to open those and read the text to find a special string in it.
Is this possible? and how?
Mail Gem may helps you.
You can configure Mail to receive email using retriever_method within Mail.defaults:
Mail.defaults do
retriever_method :pop3, :address => "pop.gmail.com",
:port => 995,
:user_name => '<username>',
:password => '<password>',
:enable_ssl => true
end
You can access incoming email in a number of ways.
The most recent email:
Mail.all #=> Returns an array of all emails
Mail.first #=> Returns the first unread email
Mail.last #=> Returns the last unread email

Mandrill setup Ruby on Rails

I'm trying to setup the Mandrill API to send transactional emails. To start with I just want to get a welcome email sent to new users that have just successfully signed up. Once I've completed that I should be able to figure out any other additional emails. I've already coded a few bits, but to be honest I'm not entirely sure I'm doing it right! The code doesn't currently work during testing, which is really annoying.
All the code is included below, all input is welcome :)
I have both the following gems installed.
'mandrill-api'
'mandrill_mailer'
I'm unsure where to place the following code, the documentation for the gem says mail.rb in config/initializers https://github.com/renz45/mandrill_mailer (Look under 'Usage')
ActionMailer::Base.smtp_settings = {
:address => "smtp.mandrillapp.com",
:port => 587,
:user_name => ENV['MANDRILL_USERNAME'],
:password => ENV['MANDRILL_API_KEY'],
:domain => 'heroku.com'
}
ActionMailer::Base.delivery_method = :smtp
MandrillMailer.configure do |config|
config.api_key = ENV['MANDRILL_API_KEY']
end
I won't be using Action Mailer so I guess I'll just change
ActionMailer::Base.smtp_settings
to
MandrillMailer::TemplateMailer.smtp_settings
???
I have a transactional_mailer.rb file under app/mailers
class TransactionalMailer < MandrillMailer::TemplateMailer
default from: 'hello#randomemail.com'
def welcome(user)
mandrill_mail template: 'welcome_template',
subject: "hello",
to: {email: user.email, name: user.name},
vars: {
'USER_IDENTIFIER' => user.email,
'LIST_COMPANY' => 'Your Company'
},
important: true,
inline_css: true,
async: true
end
end
The app currently calls the method in the registration controller
MandrillMailer.welcome(resource).deliver
Should it be
TransactionalMailer.welcome(#user).deliver
???
It's not working during testing on the local server (rails s) I'm new to coding and trying to work out why.
All help is appreciated!
Thanks :)

Rails 2 - sending pdf via emailer

I'm using ruby 1.8.7, rails 2.3.4, rubygems 1.3.6, windows 7 home basic.
Objective: to convert a html page into pdf and send it via email. (like an online receipt)
Here's what i used: prawn (for pdf) emailer (for email)
question: how to do i send an email to a real email address? All i got from the tutorial is sending an "email" that can be seen in command prompt. that's all. another question is how to generate pdf's on the fly, meaning there should be no file generated and attach it to an email? It's really hard and I have been working on this for weeks now. thanks.
The precise answer to your question depends on precisely how you're generating your pdfs, but here's an example that should work:
1) In your controller file (as part of an action)
pdf_doc = Prawn::Document.new()
pdf.text "Hello, world" # etc, etc
data_string = pdf_doc.render
user = 'me#example.com'
Emailer.deliver_email_with_attachment(user,data_string)
2) In your mailer file (e.g. app/models/emailer.rb)
class Emailer < ActionMailer::Base
def email_with_attachment(user, data)
# setup your email as normal using #from, #subject, etc
#from = user
# now attach the pdf
attachment :content_type => "application/pdf", :body => data
end
end
for more information on attachments in ActionMailer see The rails docs
EDIT: You should also make sure you've edited your config file(s) to make sure your rails app can send emails. As you're using windows you'll need to configure sending by SMTP:
config/environments/development.rb
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "my.smtp-server.com",
:port => 25,
:domain => 'iwantgreatcare.org',
:user_name => 'username',
:password => 'password',
:authentication => 'plain',
}
For more information on configuring smtp setting see the Rails ActionMailer guide

Resources