In my Rails 3 project, I want to send some simple notification emails. I don't need to make a template for them or do any logic. I just want to fire them off from various places in the system.
If I were doing this in an arbitrary ruby script I would use pony. However, I'd like to still use the rails mail facilities and configuration, so that I get the same reliability and setup that I have for the rest of the mail in my system.
What's the most simple way to do this? Ideally there would be some method like
ActionMailer.send(:to => '[email protected]', :subject =>"the subject", :body =>"this is the body")
To send the mail you use Net::SMTP to connect to the SMTP server on the local machine and then use the send_message method along with the message, the from address, and the destination address as parameters (even though the from and to addresses are within the e-mail itself, these aren't always used to route mail).
rails generates a mail preview if you use rails g mailer CustomMailer . You will get a file CustomMailerPreview inside spec/mailers/previews folder. Here you can write your method that will call the mailer and it'll generate a preview.
The default_url_options setting is useful for constructing link URLs in email templates. Usually, the :host , i.e. the fully qualified name of the web server, is needed to be set up with this config option. It has nothing to do with sending emails, it only configures displaying links in the emails.
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: "[email protected]", to: "[email protected]", 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With