# How do you add attachments in tmail

**URL:** <https://www.ruby-forum.com/t/how-do-you-add-attachments-in-tmail/160949>\
**Category:** Ruby\
**Created:** [February 21, 2009, 10:05am UTC](https://www.ruby-forum.com/t/how-do-you-add-attachments-in-tmail/160949 "2009-02-21T10:05:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [February 21, 2009, 10:05am UTC](https://www.ruby-forum.com/t/how-do-you-add-attachments-in-tmail/160949/1 "2009-02-21T10:05:14Z")

</div>

hi im trying to create an email in tmail which i will then send off via  
smtp but how do i go around specifying files to be attached in it. i  
looked on tmail but couldnt find anything of the sort

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [February 22, 2009, 5:40am UTC](https://www.ruby-forum.com/t/how-do-you-add-attachments-in-tmail/160949/2 "2009-02-22T05:40:02Z")

</div>

anyone? Ive been googling like mad but cant find anything. It must be  
able to support this.

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [February 22, 2009, 5:51pm UTC](https://www.ruby-forum.com/t/how-do-you-add-attachments-in-tmail/160949/3 "2009-02-22T17:51:08Z")

</div>

Adam A. wrote:

> anyone? Ive been googling like mad but cant find anything. It must be  
> able to support this.

may be of use

[http://maanpaa.blogspot.com/2006/08/ruby-email-with-authentication-and.html](http://maanpaa.blogspot.com/2006/08/ruby-email-with-authentication-and.html)

Ville Maanpaa on weekdays

thoughts on coding, Ruby etc.  
Tuesday, August 22, 2006  
Ruby email with authentication and attachments

#!/usr/bin/ruby

# 

# smtp\_auth.rb

# Ville Maanpaa - email with authentication including attachments

# This is the Linux version - on windows add gems and use tmail in Rails

directory

# 

require ‘base64’  
require ‘tmail’  
require ‘net/smtp’  
require ‘getopts’

def usage( status, msg = nil )  
output = (status == 0 ? $stdout : $stderr)  
output.puts msg if msg  
output.print(\<  
Usage: #{File.basename $0} [-s] [-f]  
–subject=“release notes” --tolist=“toListProd.txt” --data2=“errors”  
–attach1=“ReleaseNotes\_Servers.doc”  
–attach2=“ReleaseNotes\_Clients.doc”  
–attach3="ReleaseNotes\_Web.

-h,–host=addr SMTP server address. ([default=imap.nexatech.com](http://default=imap.nexatech.com))  
-s,–subject=sbj subject of the message. (default=(none))  
-f,–from=from from address. (default=usernmae)

EOS  
exit status  
end

def main  
attachments=[]  
to=[]  
ARGV.each do |a|  
if a.index(“attach”)  
attachments \<\< a.split(“=”)[1]  
elsif a.index(“@”)  
to \<\< a  
end  
end  
ARGV.delete\_if{|x|x.index(“attach”)}  
getopts(nil, ‘from:’, ‘f:’, ‘tolist:’, ‘subject:’, ‘data2:’)  
smtphost = $OPT\_host || $OPT\_h || ‘[smtp.server.com](http://smtp.server.com)’  
subject = $OPT\_subject || $OPT\_s  
data2=“”  
if $OPT\_data2  
if FileTest.exists?($OPT\_data2)  
File.open($OPT\_data2, “r”) do |f|  
f.each\_line { |line| data2 \<\< line }  
end  
end  
end

tolist = $OPT\_tolist || ‘tolist.txt’  
from = $OPT\_from || $OPT\_f || ‘removed\_email\_address@domain.invalid’  
usage(1, ‘Sender address not given’) unless from  
if FileTest.exists?(tolist)  
File.open(tolist, “r”) do |f|  
f.each\_line { |line| to \<\< line }  
end  
end  
usage(1, ‘Receipt address(es) not given’) if to.empty?  
send\_mail smtphost, setup\_mail(from, to, subject, data2, attachments)  
end

def setup\_mail( from, to, subject, body, attachments)  
mail = TMail::Mail.new  
mail.date = Time.now  
mail.from = from  
if to.class.to\_s.index(“Array”) #this is where we read toList.txt file  
toarr=[]  
bcc=[]  
cc=[]  
to.each do |addr|  
if addr.index(“#”)  
#this is a comment  
elsif addr.index(“bcc:”)  
bcc \<\< addr[4…-1]  
mail.bcc=bcc  
elsif addr.index(“cc:”)  
cc \<\< addr[3…-1]  
mail.cc=cc  
else  
toarr\<  
mail.to=toarr  
end  
end  
else  
mail.to = to  
end  
mail.subject = subject if subject  
mail.mime\_version = ‘1.0’  
mail.set\_content\_type ‘multipart’, ‘mixed’  
mailpart1=TMail::Mail.new  
mailpart1.body = body  
mailpart1.set\_content\_type ‘text’, ‘plain’  
mail.parts \<\< mailpart1  
attachments.each do |att|  
if FileTest.exists?(att)  
content=IO::readlines(att, nil)  
mailpart=TMail::Mail.new  
mailpart.body = Base64.encode64(content.to\_s)  
mailpart.transfer\_encoding=“Base64”  
mailpart[‘Content-Disposition’] = “attachment; filename=#{att}”  
mail.parts \<\< mailpart  
end  
end  
mail  
end

def send\_mail( host, mail )  
msg = mail.encoded  
$stderr.print msg if $DEBUG

smtp = Net::SMTP.new(host, 25)  
smtp.set\_debug\_output $stderr if $DEBUG  
smtp.start(‘[your.serve.com](http://your.serve.com)’, ‘username’, ‘password’, :login) { #CHANGE  
THESE TO  
FIT YOUR ENV  
smtp.send\_mail msg, mail.from\_address, mail.destinations  
}  
end

main

posted by Ville at 11:21 AM  
0 Comments:

Post a Comment

\<\< Home  
About Me

```
 Name: Ville
 Location: United States Minor Outlying Islands

```

Blogging on Programming and Dana Point for now

View my complete profile  
Previous Posts

```
 * Locate Technology on the Map
 * Ruby script to send email with authentication and ...
 * Ajax
 * programming - GUI toolkits

```

Powered by Blogger
