Ruby and sending emails with Net::SMTP: How to specify email subject? - ruby

I have a ruby app and I am sending emails with this format found in the documentation http://ruby-doc.org/stdlib-2.0/libdoc/net/smtp/rdoc/Net/SMTP.html :
Net::SMTP.start('your.smtp.server', 25) do |smtp|
smtp.send_message msgstr, 'from#address', 'to#address'
end
This is my code:
def send_notification(exception)
msgstr = <<-END_OF_MESSAGE
From: Exchange Errors <exchangeerrors#5112.mysite.com>
To: Edmund Mai <emai#mysite.com>
Subject: test message
Date: Sat, 23 Jun 2001 16:26:43 +0900
Message-Id: <unique.message.id.string#mysite.com>
This is a test message.
END_OF_MESSAGE
Net::SMTP.start('localhost', 25) do |smtp|
smtp.send_message(msgstr, "exchangeerrors#5112.mysite.com", "emai#mysite.com")
end
end
However, the emails being sent have no subjects in them. The msgstr just becomes the body of the email. I don't see anywhere in the documentation on how to specify a mail subject. Does anyone know?

So I looked at the documentation and it looks like Net::SMTP doesn't support this. In the documentation it says this:
What is This Library NOT?¶ ↑
This library does NOT provide functions to compose internet mails. You must create them by yourself. If you want better mail support, try RubyMail or TMail. You can get both libraries from RAA. (www.ruby-lang.org/en/raa.html)
So I looked into the MailFactory gem (http://mailfactory.rubyforge.org/), which uses Net::SMTP actually:
require 'net/smtp'
require 'rubygems'
require 'mailfactory'
mail = MailFactory.new()
mail.to = "test#test.com"
mail.from = "sender#sender.com"
mail.subject = "Here are some files for you!"
mail.text = "This is what people with plain text mail readers will see"
mail.html = "A little something <b>special</b> for people with HTML readers"
mail.attach("/etc/fstab")
mail.attach("/some/other/file")
Net::SMTP.start('smtp1.testmailer.com', 25, 'mail.from.domain', fromaddress, password, :cram_md5) { |smtp|
mail.to = toaddress
smtp.send_message(mail.to_s(), fromaddress, toaddress)
}
and now it works!

I know this mostly useless now but, I tried sending email using Net::SMTP and was able to set the subject. maybe you could have tried
Subject: 'test message' instead of Subject: test message

I actually managed to send a mail with NET::SMTP with a subject, I think that the indentation is important (example taken from http://www.tutorialspoint.com/ruby/ruby_sending_email.htm) -
message = <<MESSAGE_END
From: Private Person <me#fromdomain.com>
To: A Test User <test#todomain.com>
Subject: SMTP e-mail test
This is a test e-mail message.
MESSAGE_END
Net::SMTP.start('localhost') do |smtp|
smtp.send_message message, 'me#fromdomain.com',
'test#todomain.com'
end
The Subject will be correct. If you print the variable "message" you'll get this output-
"From: Private Person <me#fromdomain.com>\nTo: A Test User <test#todomain.com>\nSubject: SMTP e-mail test\n\nThis is a test e-mail message.\n"
While this code, with spaces in front of "Subject" -
message = <<MESSAGE_END
From: Private Person <me#fromdomain.com>
To: A Test User <test#todomain.com>
Subject: SMTP e-mail test
This is a test e-mail message.
MESSAGE_END
Net::SMTP.start('localhost') do |smtp|
smtp.send_message message, 'me#fromdomain.com',
'test#todomain.com'
end
will print -
"From: Private Person <me#fromdomain.com>\nTo: A Test User <test#todomain.com>\n Subject: SMTP e-mail test\n\nThis is a test e-mail message.\n"
And then the subject won't work.

Related

Ruby: I am trying to send an array in the email {body} but it does not send the array rather just text 'body' is send

Here is the code I tried
a = [1,2,3,4,5]
require 'net/smtp'
message = <<MESSAGE_END
From: Private Person <me#fromdomain.com>
To: A Test User <test#todomain.com>
Subject: Something
"#{a}" This is a test e-mail message.
MESSAGE_END
Net::SMTP.start('localhost') do |smtp|
smtp.send_message message, 'me#fromdomain.com',
'test#todomain.com'
end
I have figured a way around this
message =%Q(
Subject: Test suite result
#{a}
)
smtp = Net::SMTP.new 'smtp.gmail.com', 587
smtp.enable_starttls
smtp.start('smtp.gmail.com', 'from#mail.com', 'password', :plain) do |smtp|
smtp.send_message message, 'from#mail.com',
'to#mail.com'
end
But if I use above approach I am only able to send the body and cannot send subject and the to address.
Heredocs can accept variables like a normal string, using the #{var} syntax:
message = <<MESSAGE_END
From: Private Person <me#fromdomain.com>
To: A Test User <test#todomain.com>
Subject: #{a}
This is a test e-mail message.
MESSAGE_END
Source: https://stackoverflow.com/a/3332901

how to read subject of gmail ruby using Ruby Gem -gmail

I am trying to figure out how to read body of gmail using Ruby Gem "gmail"
I need to read past 10 days mails and make the objects as Json format
here is the code
require "gmail"
date1 = Date.today - 10
gmail = Gmail.connect("mailid", "password")
emails = gmail.inbox.emails(:after => date1)
emails.each_with_index do |mail, i|
#code
#mail.raw_message.body.decoded
end
Any suggestions how to read subject and body of mail
Thank you.
Assuming mail is instance of email, for reading subject:
mail.subject
for reading body
mail.body.raw_source

How do I send two SMS messages in a chain to the same receiver with twilio?

I am working on a project that returns SMS messages to a user who has just sent a SMS message to the server.
The process is:
The user sends a SMS message to the server.
The server will send two SMS messages back to this user. Note that these are two separate short messages and will be sent pretty much at the same time.
I've got the sending part working, but just for sending one SMS message, not two. When I add more code to send another message only the second message part works, which means only the second message has been sent out, the first message has been ignored.
The code looks pretty much like:
else
sms = SMS.create(:body => params['Body'], :from => params['From'], :to => params['To'], :created_at => Time.now)
#return a random saved sms
return_secret = SMS.first(:offset => rand(SMS.count))
twiml = Twilio::TwiML::Response.new do |r|
r.Sms return_secret.body
#send another message to remind user for rating
ask_rating = remind_rating
if ask_rating
twiml = Twilio::TwiML::Response.new do |r|
r.Sms ask_rating
end
twiml.text
end
Does anyone know how to send two messages in Twilio?
You've got some variable shadowing going on with twiml. As you wrote it, the second message's code is inside of the first message's block. Yet, you refer to a variable with the same name as one outside of the block. I would try flattening your code so you aren't nesting like that.
I think the issue here is you're instantiating a second TwiML::Response object when you already have one, so you can just references the previous one which you assigned to r in the first block. You also called it r in the second block so you just remove the block that encloses it:
sms = SMS.create(:body => params['Body'], :from => params['From'], :to => params['To'], :created_at => Time.now)
#return a random saved sms
return_secret = SMS.first(:offset => rand(SMS.count))
twiml = Twilio::TwiML::Response.new do |r|
r.Sms return_secret.body
#send another message to remind user for rating
ask_rating = remind_rating
if ask_rating
r.Sms ask_rating
end
end
Also the blocks weren't balanced in the initial code snippet so I stripped out the else to make it syntactically accurate.
Thank you all, really appreciate your replies.
After consulting with twilio team, they gave me an example like this:
require 'rubygems'
require 'twilio-ruby'
require 'sinatra'
get '/sms-quickstart' do
twiml = Twilio::TwiML::Response.new do |r|
r.Message "Hey Monkey. Thanks for the message!"
r.Message "this is the 2nd message"
end
twiml.text
end
I just deleted
if ask_rating
twiml = Twilio::TwiML::Response.new do |r|
everything works...

How to pass variables into Mail body template?

I am trying to write simple mailer in Sinatra which sends email with params variables.
require 'sinatra'
require 'mail'
class App < Sinatra::Base
post '/test_mailer' do
company = params['Field6']
email = params['Field5']
puts "Company name: #{company}"
puts "Email: #{email}"
mail = Mail.new do
from 'me#mydomain.com'
to 'me#mydomain.com'
subject 'Here is the image you wanted'
text_part do
body "Company Name \n === \n #{company} \n \n Email \n === \n #{email}"
end
end
mail.deliver!
end
end
How to move email template to test_mailer.txt with company and email variables ?
I'm not sure I understand you - you want an separate email template file, right? I'm thinking you could use an erb, or haml template and then do something like the following:
text_part do
body erb(:test_mailer)
end
Your test_mailer.erb file would then contain your email template.
Here shows how something similar is done using pony.

send email to multiple recipients using net/smtp

I have this code:
require "net/smtp"
MAIL_SERVER = "xxx.ad.xxx.net"
def lib_sending_report(email_hash)
# define message body
message = <<"MESSAGE_END"
From: <#{email_hash[:sender]}>
To: <#{email_hash[:recipients]}>
MIME-Version: 1.0
Content-type: text/html
Subject: #{email_hash[:subject]}
<p>Hi All,</p>
<p>We've just executed a round of load test.</p>
<p>Regards</p>
MESSAGE_END
Net::SMTP.start(MAIL_SERVER) do |smtp|
smtp.send_message message, email_hash[:sender], email_hash[:recipients]
end
end
test = ""
lib_sending_report( {:sender => "abc#xxx.com",
:recipients => "abc#xxxx.com",
:subject => "Load.Test.Report.of.#{test}"} )
When I change :recipients => "abc#xxxx.com" to :recipients => "abc#xxxx.com;efg#xxx.com", it gives me this error:
501 5.5.4 Invalid Address (Net::SMTPSyntaxError)
I can successfully send email when :recipients => "abc#xxxx.com" just have one recipient.
Where am I wrong? It seems that the separator(semicolon) I used is wrong.
I tried to use comma instead of semicolon, but it didn't work
Net::SMTP's send_message takes an array of "To:" address strings.
Per the documentation:
to_addr is a String or Strings or Array of Strings, representing the destination mail address or addresses.
This example from the documentation shows how:
Net::SMTP.start('smtp.example.com') do |smtp|
smtp.send_message msgstr,
'from#example.com',
['dest#example.com', 'dest2#example.com']
end

Resources