# Assigning directory path to downloaded email attachments

**URL:** <https://www.ruby-forum.com/t/assigning-directory-path-to-downloaded-email-attachments/167586>\
**Category:** Rails\
**Created:** [May 9, 2009, 5:30pm UTC](https://www.ruby-forum.com/t/assigning-directory-path-to-downloaded-email-attachments/167586 "2009-05-09T17:30:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jj84](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@jj84](https://www.ruby-forum.com/u/jj84)\
**Post date:** [May 9, 2009, 5:30pm UTC](https://www.ruby-forum.com/t/assigning-directory-path-to-downloaded-email-attachments/167586/1 "2009-05-09T17:30:32Z")

</div>

I am trying to build a script that will download emails + their  
attachments (from gmail server) and place them both into their  
respective folders (one folder containing one email + its attachments).

I have managed to build a script (by putting together pieces from two  
separate scripts) that gets the job 90% done, my problem is that I can’t  
get the attachments to save in the same folders as their respective  
emails.

The folder structure where the emails are saved now is perfect, it is  
just a question of getting the attachments to follow its respective  
email into the folder.

If someone would have any tips for me I would be extremely grateful!

Here’s some sample code of the attached script:

* * *

def save\_to\_disk(email) ## downloads emails  
dir = File.join(MAIL\_FOLDER,email.subject)  
FileUtils.mkdir\_p(dir)

filename = File.join(dir,email.subject)  
file = File.open(filename,“w”)  
file.puts email.body  
file.close

```
  if email.multipart? then ## downloads attachments
     email.parts.each do |m|
       if m.disposition
         filename = m.disposition_param('filename')
         if filename[0,2]=='=?'
           filename.gsub!(/=\?[^\?]+\?(.)\?([^\?]+)\?=$/){$1=='B' ?

```

$2.unpack(‘m\*’) : $2.unpack(‘M\*’)}  
end  
file = File.open(filename,‘wb’) {|f|f.write(m.body)}  
puts filename  
end  
end  
end  
end

* * *

---

<div class="post-metadata">

**Author:** ![jj84](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@jj84](https://www.ruby-forum.com/u/jj84)\
**Post date:** [May 9, 2009, 5:35pm UTC](https://www.ruby-forum.com/t/assigning-directory-path-to-downloaded-email-attachments/167586/2 "2009-05-09T17:35:08Z")

</div>

On May 9, 4:30 pm, “étienne Lacoste” \<rails-mailing-l…@andreas-  
[s.net](http://s.net)\> wrote:

> I am trying to build a script that will download emails + their  
> attachments (from gmail server) and place them both into their  
> respective folders (one folder containing one email + its attachments).
> 
> I have managed to build a script (by putting together pieces from two  
> separate scripts) that gets the job 90% done, my problem is that I can’t  
> get the attachments to save in the same folders as their respective  
> emails.

The filename in the mime headers is just a filename - you need to join  
that with a directory name same as you do when saving the email  
itself.

Fred

---

<div class="post-metadata">

**Author:** ![jj84](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@jj84](https://www.ruby-forum.com/u/jj84)\
**Post date:** [May 9, 2009, 7:21pm UTC](https://www.ruby-forum.com/t/assigning-directory-path-to-downloaded-email-attachments/167586/3 "2009-05-09T19:21:12Z")

</div>

On May 9, 5:11 pm, “Étienne Lacoste” \<rails-mailing-l…@andreas-  
[s.net](http://s.net)\> wrote:

> Frederick C. wrote:

> Thanks for the reply. I have already tried doing that, but since I’m a  
> beginner with programming, I can’t seem to get it done! I was hoping  
> maybe you could give me an example??

Well you’ve already got  
File.join(dir,email.subject)

for when you wanted a file in dir with the name email.subject, it’s  
exactly the same this time except that instead of email.subject it’s  
whatever you’ve extracted from the message part.

Fred

---

<div class="post-metadata">

**Author:** ![jj84](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@jj84](https://www.ruby-forum.com/u/jj84)\
**Post date:** [May 9, 2009, 6:11pm UTC](https://www.ruby-forum.com/t/assigning-directory-path-to-downloaded-email-attachments/167586/4 "2009-05-09T18:11:34Z")

</div>

Frederick C. wrote:

> On May 9, 4:30ï¿½pm, “ï¿½tienne Lacoste” \<rails-mailing-l…@andreas-  
> [s.net](http://s.net)\> wrote:
> 
> > I am trying to build a script that will download emails + their  
> > attachments (from gmail server) and place them both into their  
> > respective folders (one folder containing one email + its attachments).
> > 
> > I have managed to build a script (by putting together pieces from two  
> > separate scripts) that gets the job 90% done, my problem is that I can’t  
> > get the attachments to save in the same folders as their respective  
> > emails.
> 
> The filename in the mime headers is just a filename - you need to join  
> that with a directory name same as you do when saving the email  
> itself.
> 
> Fred

Thanks for the reply. I have already tried doing that, but since I’m a  
beginner with programming, I can’t seem to get it done! I was hoping  
maybe you could give me an example??

---

<div class="post-metadata">

**Author:** ![jj84](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@jj84](https://www.ruby-forum.com/u/jj84)\
**Post date:** [May 9, 2009, 11:42pm UTC](https://www.ruby-forum.com/t/assigning-directory-path-to-downloaded-email-attachments/167586/5 "2009-05-09T23:42:09Z")

</div>

I got it to work now! Thanks for the advice Fred.
