Satchel
Satchel

Reputation: 16724

How to save copy of outgoing email in IMAP "Sent" box using ActionMailer?

When I use my regular client and send email through SMTP for an IMAP account, that outbound email gets saved in the IMAP "Sent" box.

How can I have the same behavior when I send email using Ruby on Rails ActionMailer?

Upvotes: 3

Views: 2778

Answers (2)

Spone
Spone

Reputation: 67

The Ruby IMAP library contains an append method that you can use to "save" these outbound emails to the folder of your choice:

# Let's assume the_mail is the Mail object you want to save
the_mail = Mail.new

# The name of the target mailbox
target_mailbox = 'Sent'

# Connect to the IMAP server
imap = Net::IMAP.new(YOUR_EMAIL_SERVER)
imap.authenticate('PLAIN', YOUR_LOGIN, YOUR_PASSWORD)  

# Create the target mailbox if it does not exist
imap.create(target_mailbox) unless imap.list('', target_mailbox)

# Save the message
imap.append(target_mailbox, the_mail.to_s)

# Close the connection
imap.logout
imap.disconnect

Hope this helps!

Upvotes: 6

Tim Snowhite
Tim Snowhite

Reputation: 3776

This tends to be a setting in your mail client program, from what I can tell; but I don't see much support for it in ActionMailer.

There is a ruby IMAP library, if you find that the messages are getting stored on the server, but just in the wrong place. http://ruby-doc.org/stdlib/libdoc/net/imap/rdoc/index.html

A workaround might be to send every message to your originating email address say [email protected], perhaps with a tag like [email protected], and then set up a rule in the client you'll be viewing this inbox with to route all emails with that TO: to the Sent Items box.

If you're using gmail as your mail server for your rails application, it saves a copy in the sent mail automatically.

Upvotes: 1

Related Questions