A simple Ruby method to send email
Posted: February 17th, 2009 | Author: Jerod | Filed under: Ruby | Tags: email | View CommentsI have tried many different Ruby mailers and they all have their problems. The Pony gem by Adam Wiggins is right up my alley but even that has given me a hard time sending emails. Plus, sometimes you just don’t want your little Ruby script having to require rubygems.
I always end up reverting to a simple method I wrote awhile back and it just works. Feel free to use it and adjust to your needs:
require 'net/smtp' def send_email(to,opts={}) opts[:server] ||= 'localhost' opts[:from] ||= 'email@example.com' opts[:from_alias] ||= 'Example Emailer' opts[:subject] ||= "You need to see this" opts[:body] ||= "Important stuff!" msg = <<END_OF_MESSAGE From: #{opts[:from_alias]} <#{opts[:from]}> To: <#{to}> Subject: #{opts[:subject]} #{opts[:body]} END_OF_MESSAGE Net::SMTP.start(opts[:server]) do |smtp| smtp.send_message msg, opts[:from], to end end
Everything but the “to” argument is optional. You can invoke the method like so:
send_email "admnistrator@example.com", :body => "This was easy to send"
Hey Jerod,
Do you know of a way to use SMTP and put a subject in the email? Rather than just having the header in the content of the actual email to an individual, it would be nice for the subject of the email in their mailbox to say something rather than than “no subject.”
Thanks!
Hi Tony-
I'm not sure what email client you're using to view the sent mail, but this method should do exactly what you've described.
Can you provide more detail?
Hi Jerod,
I used basically that same method. I'm using a webmail system set up on my localhost. The entire 'msg' is in the body of the email but, for example, if you saw the email that was sent in your inbox, it would say '(no subject)' rather than 'You need to see this.' I wasn't sure that using net/smtp would allow me access at all to creating a subject other than the 'subect' that is just put into the body content.
This problem might be specific to the client you're using to view the email. With SMTP the subject, to, from, etc are all actually part of the body of the message.
The email client parses those and puts them where they need to go. I've been using this method in a few scripts that email me regularly (gmail) and the subject always shows up right where it belongs.
Looks like you have a gmail account, perhaps try sending an email to it using this method and verify what I'm telling you.
If it still shows up how you describe, we can troubleshoot the method some.
Yeah, I sent the email to my gmail account the first time and it showed up without parsing the message. However, I noticed a couple of small differences that I just changed. I'll let you know what they are so if anyone else has a future error like mine (likely caused to me not paying attention to a real minor detail) they can see how I fixed it. Thanks!
Hi Tony-
I'm not sure what email client you're using to view the sent mail, but this method should do exactly what you've described.
Can you provide more detail?
Hi Jerod,
I used basically that same method. I'm using a webmail system set up on my localhost. The entire 'msg' is in the body of the email but, for example, if you saw the email that was sent in your inbox, it would say '(no subject)' rather than 'You need to see this.' I wasn't sure that using net/smtp would allow me access at all to creating a subject other than the 'subect' that is just put into the body content.
This problem might be specific to the client you're using to view the email. With SMTP the subject, to, from, etc are all actually part of the body of the message.
The email client parses those and puts them where they need to go. I've been using this method in a few scripts that email me regularly (gmail) and the subject always shows up right where it belongs.
Looks like you have a gmail account, perhaps try sending an email to it using this method and verify what I'm telling you.
If it still shows up how you describe, we can troubleshoot the method some.
Yeah, I sent the email to my gmail account the first time and it showed up without parsing the message. However, I noticed a couple of small differences that I just changed. I'll let you know what they are so if anyone else has a future error like mine (likely caused to me not paying attention to a real minor detail) they can see how I fixed it. Thanks!
Mary Kay Cosmetics Organic Skincare Suggestions
You will find numerous points you are able to do to improve the texture, elasticity and appearance of your skin. The very first 3actions are an important foundation to flawless skin.No matter how much you pay for costly moisturizers that promise younger, more healthy smooth skin, if you do not do the 3step cleansing routine (cleanse, tone & moisturize), it is not likely that these products will have any real benefit to the skin on your face.
1. Cleansing – Start your skincareregimen with a cleanser, by Mary KayCosmetics. That’s it! No additives that can cause irritation to your skin especially if you have sensitive skin. You can use the Mary Kay Cosmetics cleanser by simply wetting your face to rinse. Remove the cleanser by using a MaryKay® Facial Cleansing Cloths.
2. Tone – Next, using cotton wool and Mary Kay Purifying Freshener to remove the last traces of cleanser and tone the skin. Have you tried Flower Waters? They aresuper helpful to the health of your skin as they are natural and contain anti-bacterial properties which reduce bacteria growing which in turn inhibits rashes and spots. Some also have astringent properties which close pores, and make the skin more toned. Some excellent ones to try are good old water – cold, rosewater (good for mature skin too), orange flower water andorganicwitch hazel. Avoid toners with alcohol as these will dryout your skin which will damage your efforts.
3. Moisturise – This step is essential to wholesome skin. You need to replace lost moisture to the skin and this is best done with Mary Kay Balancing Moisturizer Cream to suit your skin type. The most common are sensitive skin, oily skin, dry skin and combination skin, so look out for these descriptions when purchasing creams.
4. Sun Cream – Stay out away from the sun – UV rays induce the skin to age, as a resultuse a factor that is proper for your skin type for maximum sun protection. Mary Kay Sun Care Collection is an exceptional choice.
5. Avoid Free Radicals – What are they? See our write-up on free radicals for further facts, but in a few words the yare molecules that induce the skin to grow older and crease. Use Anti-oxidants tocombat free radicals that age the skin. Vitamin A, C & E are particularly beneficial in addition to coconut and olive oil. A handful of almonds are loaded with enough vitamin E to counteract free radicals.
6. Water – yes, sipping water really does help to flush out toxins and re-hydrate your skin. Roughly 2 litres per day (8x 250ml glasses).
7. Serums – These are nutritious oils with an ingredient to help transfer the oils into the deeper levels of the skin so theyenhance the tone and elasticity. It is best to use the remedy twice a day, once in the morning hours a few minutes before you moisturise and again before you go to bed so that it can do its work while you sleep. Mary Kay Cosmetics TimeWise® Replenishing Serum+C is an excellent choice.
8. Face Mask – Use a Mary Kay TimeWise® Even Complexion Mask as a weekly treatment.
9. Rest – Sleep is one of the best things you can do for your skin.
These tips ought to not be used as a one off, butincorporated into a regular skin care regimen for best effects.
For more information on the Mary Kay Cosmetics Skin Care line please visit
Mary Kay Cosmetics