Could you please post a program to send mail without using Action Mailer

require ‘net/smtp’

def send_email(to,opts={})
opts[:server] ||= ‘localhost’
opts[:from] ||= ‘[email protected]
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
I tried this but it doesn"t work

Define “doesn’t work”. Do you get an error message?

There are these alternatives that I know of:
https://rubygems.org/gems/pony
https://rubygems.org/gems/mail

Here is an example that works in Ruby 1.8.6.
Comments are in english but the receivers are french speaking people…
Mind the blank lines in the mailtext variable.

def send_general
#===============#

send the file “fichier_html” via SMTP to the adresses contained in the

file “Destinataires.txt” with a text

read the file and base64 encoding

filecontent = File.read(@fichier_html)
encodedcontent = [filecontent].pack(“m”) # base64

read the “to” adresses

dest = Array.new
IO.foreach(“Destinataires.txt”) { |ligne|
ligne.chomp!
if not ligne.empty? then dest.push(ligne) end }
marker = “==batilus123456==”

message text

body =<<EOF
Veuillez trouver ci-joint les résultats de : #{@laRegate.nomReg} du
#{@laRegate.date}

Commentaire : #{@commentEntry.text}

Le B.V.B.M.G.H.

N.B.: Veuillez ne pas répondre à ce courriel qui vous a été envoyé
automatiquement
EOF

header text

part1 =<<EOF
Subject: CVBM : resultats de regate
From: CVBM [email protected]
To: Destinataires [email protected]
Content-Type: multipart/mixed; boundary="#{marker}"
MIME-Version: 1.0

–#{marker}
EOF

message definition text

part2 =<<EOF
Content-Type: text/plain; charset=“UTF-8”
Content-Transfer-Encoding:8bit

#{body}

–#{marker}
EOF

attachment definition

part3 =<<EOF
Content-Type: application/htm; name="#{@fichier_html}"
Content-Transfer-Encoding:base64
Content-Disposition: attachment; filename="#{@fichier_html}"

#{encodedcontent}

–#{marker}–
EOF

mailtext = part1 + part2 + part3
mes = “Résultats envoyé à :”
dest.each { |d| mes = mes + “\n” + d }

Send the message

begin
Net::SMTP.start(‘smtp.orange.fr’) { |smtp| smtp.sendmail(mailtext,
[email protected]’, *dest) }
self.infoBox(@geneDialog, mes) # display information
rescue Exception => e
self.erreurBox(@geneDialog, e) # display error
end
end

I ran your example and it works fine. Have you looked in you email
logs?

Here’s the sort of thing I do using the mail gem. You can use gmail’s
smtp server if you have a gmail account set up:

require ‘mail’

options = {
:address => ‘smtp.gmail.com’,
:port => 587,
:user_name => ‘[email protected]’,
:password => ‘password’,
:authentication => ‘plain’,
:enable_starttls_auto => true
}

Mail.defaults do
delivery_method :smtp, options
end

Mail.deliver do
from ‘[email protected]
to %w([email protected] [email protected])
subject ‘Test’
body ‘Test email’
if attachments.size != 0 && attachments[0]
attachments.each {|filename|
add_file(filename)
}
end
end

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs