1

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.

The Rookie
  • 595
  • 9
  • 26

1 Answers1

1

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

Community
  • 1
  • 1
user000001
  • 32,226
  • 12
  • 81
  • 108