INCLUDE_DATA

 
»
S
I
D
E
B
A
R
«
aquisto cialis levitra svizzera tadalafil bestellen viagra prix acheter du viagra acquista levitra cialis sur internet citrate de sildenafil compro viagra levitra ricetta kamagra pharmaceuticals cialis vente libre cialis suisse levitra italia vente de cialis viagra suisse kamagra gel viagra quanto costa cialis 20 mg prix cialis 10mg viagra verkauf cialis moins cher viagra prezzo levitra kopen acheter tadalafil acheter kamagra tadalafil generico impuissance sexuelle cialis preco levitra donna viagra 100 mg comprar cialis generico comprar levitra cialis a vendre procurer du cialis generische cialis vardenafil generico vardenafil generika levitra sur internet generische viagra acheter cialis generique acquista viagra achat cialis viagra 100 mg cialis generico kamagra en france viagra ordonnance acheter viagra achat vardenafil pastilla levitra viagra cialis differenze impotenza sessuale venta viagra medicament cialis curare impotenza kamagra te koop achat cialis 20mg levitra pharmacie cialis receta acquisto viagra net cialis en ligne achat de viagra cialis generique acheter acheter du cialis cialis 20 mg vardenafil 10 mg viagra alternativo citrate de sildenafil cialis sin receta viagra kopen acheter cialis en pharmacie kamagra bestellen comprar viagra pela internet viagra prescrizione levitra donne vente cialis venta de sildenafil achete levitra acheter cialis france venta de levitra viagra kosten cialis marche pas comprar vardenafil disfunzione erettile rimedi vardenafil generique viagra recensioni cialis generico prezzo viagra versand cialis europe viagra venta libre impotenza rimedi cialis rezeptfrei acheter kamagra france levitra ohne rezept acquisto viagra in contrassegno prix de cialis cialis prescrizione viagra acquisto online achat viagra pildoras cialis kamagra generique cialis prezzo cialis inde cialis sur le net acheter cialis en espagne levitra ordonnance viagra naturel cialis 10 mg acquistare levitra procurer du viagra acquisto viagra senza ricetta viagra controindicazioni levitra 20 mg compra viagra impuissance erection acheter cialis pharmacie prezzi levitra viagra ohne rezept kamagra apcalis comprar viagra commander du cialis cialis ricetta medica sildenafil 50 mg sildenafil venta viagra italia pilule levitra sildenafil generico viagra prijs
Sending Email in Ruby
Jan 26th, 2009 by Jason

I came across a rather interesting discovery today that email is quite simple to send within Ruby. Check it out:

Simple Email

require 'net/smtp'
message = 'Hello Dude'
Net::SMTP.start('127.0.0.1',25) do |smtp|
smtp.send_message(message, 'from@from.com','to@to.com')
end

Granted, this is a very simple example, but the principles are still there. Write a message as a string, start a new SMTP connection, tell it where it’s from and going, and send it on its way.

The problem with this method is that you wind up with some very ugly(nonexistent) headers that spam catchers may not like. One way around this is to include them in the message as follows:

Simple Email with Headers

require 'net/smtp'
message = <<MESSAGE_END
From: test@hello.com
To: to@to.com
Subject: Sample Message

Hello, this is a message
MESSAGE_END
p message
Net::SMTP.start('10.245.27.10',25,'localhost') do |smtp|
smtp.send_message(message, 'test@hello.com','to@to.com')
end


This gives you greater control over the headers. However, I still found this to be a bother for the simple sake of having to type the to field multiple times. That’s the sort of thing that can lead to errors in code (it actually messed me up for a a minute while writing this). So my last bit moves the message into class form, allowing us to keep our sending portion short, clean, and less error prone. This involves two files, emailMessage.rb and email.rb. They are shown below:

emailMessage.rb

class EmailMessage
attr_accessor :to
attr_accessor :from
attr_accessor :message
attr_accessor :subject
def initialize(attributes)
@to = attributes[:to]
@from = attributes[:from]
@message = attributes[:message]
@subject = attributes[:subject]
end

def to_s
<<MESSAGE_END
From: #{@from}
To: #{@to}
Subject: #{@subject}
#{@message}
MESSAGE_END
end
end


email.rb

require 'net/smtp'
require 'emailMessage'
em = EmailMessage.new :to=>;'to@to.com', :from=>;'test@test.com', :subject=>;'mysubject'
em.message = 'This is the message'
Net::SMTP.start('127.0.0.1',25) do |smtp|
smtp.send_message(em.to_s, em.from,em.to)
end

We’re back to a short, clean, email.rb that can be used for sending email with most SMTP.

Using SSL

Some SMTP hosts, including Gmail, requires the usage of SSL to send emails. This is not built into the standard Ruby libraries. However, Stephen Chu has a great post covering how to do it in Ruby.

»  Substance: WordPress   »  Style: Ahren Ahimsa Blog Flux Local - Utah
© Jason Staten 2009