# Mail issue

**URL:** <https://www.ruby-forum.com/t/mail-issue/191228>\
**Category:** Ruby\
**Created:** [July 7, 2010, 8:01pm UTC](https://www.ruby-forum.com/t/mail-issue/191228 "2010-07-07T20:01:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![di3go](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a9a28c/32.png) [@di3go](https://www.ruby-forum.com/u/di3go)\
**Post date:** [July 7, 2010, 8:01pm UTC](https://www.ruby-forum.com/t/mail-issue/191228/1 "2010-07-07T20:01:53Z")

</div>

Hello!

I need to get mails from a mail server using pop3, save to disk to later  
resend.

The get part:

pop = Net::POP3.new ‘mail server’  
pop.start ‘address’, ‘password’

if pop.mails.empty?  
puts “No mail”  
else  
puts “Have mails”

```
pop.mails.each_with_index do |m,i|
  File.open("inbox/#{i}", "w+") do |f|
    f.write m.pop
  end
end

```

end

With this code i can download and save the mails into files.

Now i need get this files and resend as mails, this is the part isnt  
working…

smtp = Net::SMTP.new ‘mail address’, port  
smtp.start ‘mail domain’, ‘username’, ‘password’, :login

puts smtp.sendmail File.read(file from get path), ‘from’, ‘to’

Well, the send part is broken, can anyone help me?

---

<div class="post-metadata">

**Author:** ![di3go](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a9a28c/32.png) [@di3go](https://www.ruby-forum.com/u/di3go)\
**Post date:** [July 7, 2010, 9:25pm UTC](https://www.ruby-forum.com/t/mail-issue/191228/2 "2010-07-07T21:25:45Z")

</div>

Here is a mass Emailer that uses a TK front end. If you don’t want the  
front end you can just remove the calls and such…no big. It uses 2  
files the \*\*.txt file holds the Emails and the \*\*.html holds the actual  
page.

############START SCRIPT###########  
require ‘fileutils’  
require ‘net/smtp’  
require ‘tk’

begin

root = TkRoot.new(‘background’=\>‘white’){title “My Frontend”}  
$text = TkText.new(root, ‘width’=\>30){ background ‘white’;  
pack(“padx”=\>1, “pady”=\>1, “side”=\>‘left’)}

$emailName = Array::new  
$emailAddress = Array::new

$eT = File.read(‘test\_par.html’)

IO.foreach(‘emails.txt’,’\n’) do |line|  
$text.insert(‘end’, “#{line}”)  
end

class SendEmail

def send\_mail

$emailName = $text.value

```
#text.value.each do |name|
# p name.scan(/.*?[^\[^\]^\n]$/).flatten
#end

$emailName.each do |name|

begin
  $address = name.scan(/.*?[^\n]$/).flatten #separate out the Email 

```

address

```
  Net::SMTP.start('mail.hrsmart.com', 25, 'localhost','EMAIL ACCOUNT 

```

NAME’,‘PASSWORD’, :plain) do |smtp|  
smtp.open\_message\_stream(‘FROM EMAIL ADDRESS’, $address) do |f|  
f.puts ‘From: removed\_email\_address@domain.invalid’  
f.puts “To: #{$address}”  
f.puts "MIME-Version: 1.0  
f.puts “Content-type: text/html”  
f.puts ‘Subject: January Tattoo Specials’  
f.puts  
f.puts “#{$eT}”  
end  
end

```
  #puts message
  #p $address

rescue Exception => e
  puts "Could not send to #{$address} - #{e.message}\n"
  #puts e.backtrace.inspect
end

```

end  
end  
end

TkButton.new(root, ‘background’=\>‘blue’, ‘foreground’=\>‘white’){text  
“Send Mail”; command{SendEmail.new.send\_mail}; pack(‘side’=\>‘left’,  
‘padx’=\>1, ‘pady’=\>1)}

rescue  
print “Exception occured\n”  
end

Tk.mainloop  
############END SCRIPT###########
