I am sending an email in my Rails 2 app and after a bit of hacking and learning about emails in Rails it now works fine.
Now I am just trying to add an attachment which in theory should probably be straightforward but I seem to be having issues with it.
I am using mailcatcher to preview the emails in the development environment and I can see the attachment headers in the email source but nothing is shown in mailcatcher (docs say it supports attachments)
emailer.rb
class Emailer < ActionMailer::Base
def quotation_notification(q)
#recipients = q.recipient_email
#from = q.partner_name + "<#{q.partner_email}>"
#subject = "New Quotation from " + q.partner_name
#sent_on = Time.now
#quote_id = q.quote_id
#customer_id = q.customer_id
#customer_name = q.customer_name
#recipient_email = q.recipient_email
#partner_name = q.partner_name
#partner_email = q.partner_email
#partner_ref = q.partner_ref
#version_no = q.version_no
#line_items = q.line_items
#quotation_date = q.quotation_date
content_type "multipart/alternative"
part "text/html" do |p|
p.body = render_message("quotation_notification.text.html.rhtml", :message => q)
end
attachment :content_type => "application/pdf",
:body => File.read(RAILS_ROOT + '/pdfs/' + q.quote_id + '.pdf')
#body[:q] = q
end
end
The email is being sent in a controller like so
q = QuotationEmail.new(quote_id, customer_id, customer_name, recipient_email, partner_name, partner_email, partner_ref, version_no, line_items, quotation_date)
# send email
Emailer.deliver_quotation_notification(q)
Just for completeness, my view is app/views/emailer/quotation_notification.text.html.rhtml
quotation_email.rb
class QuotationEmail
attr_accessor :quote_id, :customer_id, :customer_name, :recipient_email, :partner_name, :partner_email, :partner_ref, :version_no, :line_items, :quotation_date
def initialize(quote_id, customer_id, customer_name, recipient_email, partner_name, partner_email, partner_ref, version_no, line_items, quotation_date)
#quote_id = quote_id
#customer_id = customer_id
#customer_name = customer_name
#recipient_email = recipient_email
#partner_name = partner_name
#partner_email = partner_email
#partner_ref = partner_ref
#version_no = version_no
#line_items = line_items
#quotation_date = quotation_date
end
end
In the source of the email, after the closing html tag I can see
--mimepart_522da7e7e5959_a0885cd9314396
Content-Type: application/pdf
Content-Transfer-Encoding: Base64
Content-Disposition: attachment
*base 64 encoded stuff*
--mimepart_522da7e7e5959_a0885cd9314396--
If I have done anything bonkers then it is because I haven't sent any emails in Rails yet so still figuring things out.
I can only assume this was a problem with mailcatcher, by setting up development emails to be sent to a gmail account, the emails were sent correctly with attachments.
environments/development.rb
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "domain.co.uk",
:user_name => "gmailaddress#domain.co.uk",
:password => "accountpassword",
:authentication => :plain,
:enable_starttls_auto => true #This line is must to ensure the tls for Gmail
}
I used an existing app email address hosted on gmail, so not strictly an username#gmail.com / username#googlemail.com but I would think that those addresses would also work with the above.
Related
In order_mailer.rb:
default from: 'notifications#example.com'
def welcome_email(order)
#user = "Uday kumar das"
#url = 'http://example.com/login'
mail(to: 'dasudaykumar017#gmail.com', subject: 'Welcome to My Awesome Site')
end
In orders_conroller:
def delivery
#order1 = Order.where(:orderId=>params[:orderId])
#order = Order.find(#order1)
OrderMailer.welcome_email(#order).deliver
end
In environments/development.rb:
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
I am new to mails in rails.I am referring http://guides.rubyonrails.org/action_mailer_basics.html to learn. I am getting error like:
Net::SMTPAuthenticationError in OrdersController#delivery`
530-5.5.1 Authentication Required. Learn more at`
I did the same using my gmail, following are my configurations, try and see it if works
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:authentication => :plain,
:user_name => "<my gmail>#gmail.com",
:password => "<my gmail password>",
:openssl_verify_mode => 'none' }
Please note the:
:openssl_verify_mode => 'none'
section to skip the ssl errors
I'm just about in the finishing stages of my website, however I am having trouble with the ActionMailer. It prints out the message just fine, I'm just eager to know how to wire it so it can send to gmail account. I'm primary confused how to route it and configure it properly. I have a contact page that has a model that takes parameters like the recipient, subject, message and the time it was sent: Mailer model: Note all this code is on a local machine
class UserEmail < ActionMailer::Base
default from: 'XXX#gmail.com'
def contact(sender, subject, message, sent_at = Time.now)
#sender = sender
#message = message
#sent_at = sent_at.strftime("%B %e, %Y at %H:%M")
mail(:subject => subject)
end
end
Here's the about controller which the contact methods lie in:
class AboutController < ApplicationController
# ...\controllers\home_controller.rb
#----------------------------------------------------
# show contact form
def contact
#title = "Contact"
#sender = ''
#subject = ''
#message = ''
end
def sendmail
#sender = params[:sender]
#subject = params[:subject]
#message = params[:message]
if validate(#sender, #subject, #message)
UserEmail.contact(#sender, #subject, #message).deliver
flash[:success] = "Your message sent sucessfully!"
redirect_to about_index_path
else
flash.now[:error] = "Your message did not send"
redirect_to about_index_path
end
end
private
def validate(sender, subject, message)
#email_regex = /\A[\w+\-.]+#[a-z\d\-.]+\.[a-z]+\z/i
if sender.blank? || subject.blank? || message.blank?
#error = "Message not sent: Required information not filled"
return false
elsif subject.length >= 50
#error = "Message not sent: Subject must be smaller than 50 characters"
return false
elsif sender[#email_regex].nil?
#error = "Message not sent: Email not valid"
return false
else
return true
end
end
end
Now this is where I am lost.
Here's what my route like to the mailer. Is this routed appropriately?:
match '/contact_email', :to => 'about#sendmail'
When I configure the mailer, does the code rest in the application.rb or the development.rb? Here's what I have in my application.rb:
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:user_name => 'XXX#gmail.com',
:password => 'XXX',
:authentication => 'plain',
:enable_starttls_auto => true,
}
Thanks in advance!
Change
def contact(sender, subject, message, sent_at = Time.now)
#sender = sender
#message = message
#sent_at = sent_at.strftime("%B %e, %Y at %H:%M")
mail(:subject => subject)
end
to
def contact(sender, subject, message, recipient, sent_at = Time.now)
#sender = sender
#message = message
#sent_at = sent_at.strftime("%B %e, %Y at %H:%M")
#recipient = recipient
mail(:subject => subject, :to => #recipient)
end
And don't forget to set recipient in your calling function.
Have you put the following lines in development.rb
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
I have created a database of users in my Ruby on Rails app, and now I'm trying to create a mailer that send emails to all users in my database whenever I want.
Here's my model:
class MailMessage < ActionMailer::Base
def contact(recipient, subject, message)
# host = Hobo::Controller.request_host
# app_name = Hobo::Controller.app_name || host
#subject = subject
# #body = { :user => user, :host => host, :app_name => app_name }
#body["title"] = 'This is title'
#body["email"] = 'mark#doc.org.uk'
#body["message"] = message
#recipients = recipient
#from = 'no-reply#doc.org.uk'
#sent_on = Time.now
#headers = {}
end
end
Here's my controller:
class MailMessageController < ApplicationController
def sendmail
email = #params["email"]
recipient = email["recipient"]
subject = email["subject"]
message = email["message"]
MailMessage.deliver_contact(recipient, subject, message)
return if request.xhr?
render :text => 'Message sent successfully'
end
def index
render :file => 'app/views/mail_message/index.html'
end
end
Here's my views/mail_message:
<h1>Send Email</h1>
<%= form_tag :action => 'sendmail' %>
<p>
<label for="email_subject">Subject</label>
<%= text_field 'email', 'subject' %>
</p>
<p>
<label for="email_recipient">Recipient</label>
<%= text_field 'email', 'recipient' %>
</p>
<p>
<label for="email_message">Message</label>
<%= text_area 'email', 'message' %>
</p>
<%= submit_tag "Send" %>
<%= form_tag %>
Here's my enviroment.rb:
ActionMailer::Base.delivery_method = :sendmail
ActionMailer::Base.sendmail_settings = {
:location => '/usr/sbin/sendmail',
:arguments => '-i -t'
}
ActionMailer::Base.perform_deliveries = true # the "deliver_*" methods are available
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.default_charset = "utf-8"
ActionMailer::Base.default_content_type = "text/html" # default: "text/plain"
ActionMailer::Base.default_mime_version = "1.0"
ActionMailer::Base.default_implicit_parts_order = [ "text/html", "text/plain"]
When I run a test message, I get the following error:
You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]
app/controllers/mail_message_controller.rb:4:in `sendmail'
It doesn't seem to recognise sendmail, but I have given its location. Any clues for how to fix this error will be very appreciated.
It looks like this line is the problem:
#params["email"]
If it's meant to be the data from the form, drop the #.
#params isint initialized in your controller.
You probably simple want to use params to get your http action parameters.
I use the God gem to monitor my delayed_job processes, so far the gem is doing its job as it should but from some reason I can't get him to send email notifications (i use google apps).
Here are my god file configuration:
God::Contacts::Email.defaults do |d|
d.from_email = 'system#example.com'
d.from_name = 'Process monitoring'
d.delivery_method = :smtp
d.server_host = 'smtp.gmail.com'
d.server_port = 587
d.server_auth = true
d.server_domain = 'example.com'
d.server_user = 'system#example.com'
d.server_password = 'myPassword'
end
God.contact(:email) do |c|
c.name = 'me'
c.group = 'developers'
c.to_email = 'me#example.com'
end
w.start_if do |start|
start.condition(:process_running) do |c|
c.interval = 20.seconds
c.running = false
c.notify = {:contacts => ['me'], :priority => 1, :category => 'staging'}
end
Any thoughts?
According to this post on the mailing list:
gem install tlsmail
add Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE) in the email
part of god's config
Use :login intead of true for your server_auth setting.
The content in the view is not being displayed. Only the attachment is being sent. Help would be appreciated!
def send
#subject = "Status of PIS App"
#recipients = "ssg#gmail.com"
#from = APP_CONFIG[:email]
#sent_on = Time.now
##content_type = "text/html"
content_type = "multipart/alternative"
attachment :filename => "Report.html",:content_type => "text/html",
:body => File.read("/home/shreyas/repos/mysorepoc/app/models/new1.html")
end
When using attachments, you need to specify the text part separately:
def send
#subject = "Status of PIS App"
#recipients = "ssg#gmail.com"
#from = APP_CONFIG[:email]
#sent_on = Time.now
##content_type = "text/html"
content_type = "multipart/mixed"
part :content_type => "text/plain", :body => "contents of body"
attachment :filename => "Report.html",:content_type => "text/html", :body => File.read("/home/shreyas/repos/mysorepoc/app/models/new1.html")
end
And you probably want to send the mail as a multipart/mixed, not as multipart/alternative (unless the attachment really is an alternative representation of the text part).