this is my email tester script. worth a shot.
#!/bin/env ruby
# This is used to send email.
require 'net/smtp'
# Helper method.
# Append _appendage_ to end of string if it already doesn't exist
# at the end of the string.
class String #:nodoc:
def appendIfMissing(appendage)
return self if appendage.nil? || appendage.size < 1
return appendage if size < 1
if self[-appendage.size..-1] == appendage
self
else
self + appendage
end
end
end
class String #:nodoc:
def blank?
empty? || strip.empty?
end
end
# Print an error message.
def err(msg, printRubyErr=false, fatal=true) #:nodoc:
STDERR.print "\n### ERROR: #{msg}\n\n"
STDERR.print "\n#{$!}\n\n" if printRubyErr
exit if fatal
"### ERROR: #{msg}\n#{$!}\n"
end
# simple check of email address.
def checkMailAddress(address, descr) #:nodoc:
address.strip!
addr = address[/<?\w+@\w+\.\w+>?$/]
if addr.blank?
err(descr,false,false)
return nil
end
addr
end
# Send email.
def mail( od )
# The email is composed of the 'From', 'To', and 'Subject' fields, followed by the body text.
# Strip off leading white space.
(msgstr = <<-END_OF_MAIL_MESSAGE).gsub!(/^\s+/, '')
···
From: #{od['-=']}
To: #{od['-t']}
Subject: #{od['-j'].blank? ? 'No Subject' : od['-j']}
END_OF_MAIL_MESSAGE
msgstr += od['-b']
return if (addr_from = checkMailAddress(od['-='], "no 'From' address (#{od['-=']})") ).nil?
return if (addr_to = checkMailAddress(od['-t'], "no 'To' address (#{od['-t']})") ).nil?
# The email is actually sent here.
begin
Net::SMTP.start(od['-m'], 25, od['-n'], od['-S'], od['-P'], :login) { |smtp|
smtp.send_message msgstr, addr_from, addr_to
}
puts "-- mail sent to #{addr_to}" if od['-V']
rescue Exception => ex
err("unable to send mail.\n#{msgstr}\n#{$!}\naddr from:#{addr_from}\naddr to:#{addr_to}", true)
end
end # mail
od = { '-=' => 'My Long Name<user@XXX.net>', # mail from
'-t' => 'My Long Name<user@XXX.net>', # mail to
'-j' => 'This is a test', # mail subject
'-b' => 'Body text of the email.', # mail body text
'-m' => 'smtp.XXX.net', # mail server
'-n' => 'www.XXX.net', # domain sending mail from
'-S' => 'mailusername', # mail user name
'-P' => 'super_secret_password', # mail password
'-V' => true } # verbose?
mail od
__END__
Dan Diebolt wrote:
I am trying to send email from my comcast email account (say me@comcast.net) to my yahoo email accout (say me@yahoo.com) from a ruby script using net/smtp:
require 'net/smtp'
Net::SMTP.start("smtp.comcast.net", 25,"localhost","user","pass") do |smtp|
smtp.send_message "hello","me@comcast.net","me@yahoo.com" end
After trying the above ruby in irb I get the following message with no email delivered:
=> "250 Mail queued for delivery.\n"
I don't understand how the third argument to Net::SMTP.start should be specified; its called "helo" and documentation says it defaults to 'localhost.localdomain'. I would appreciate it if anyone could point out what I am doing wrong.
I am pretty sure I have the smtp, pop3 and ports identified correctly as I can send email using the command line utility postie (http://www.infradig.com/postie/index.shtml\):
postie -esmtp -host:smtp.comcast.net -to:me@yahoo.com -from:me@comcast.net -s:subject -msg:hello -user:user -pass:pass
#Incoming mail (POP3): mail.comcast.net #Incoming mail (POP3): 110
#Outgoing mail (SMTP): smtp.comcast.net #Outgoing mail (SMTP): port is set to 25