How to save a copy of outgoing email in the Sent field of IMAP using ActionMailer?

When I use my regular client and send email via SMTP to an IMAP account, this outgoing email is stored in the IMAP Sent field.

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

+2


a source to share


2 answers


This is usually a setting in your email client, from what I can tell; but I don't see any support for it in ActionMailer.

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



A workaround might be to send each message to your original email address, say sender@yourdomain.com

perhaps with a type tag sender+sent@yourdomain.com

, and then set up a rule on the client that you will be viewing in that mailbox to route all email with this TO:

to the Sent Items field.

If you are using gmail as the mail server for your rails app, it will automatically save a copy in the sent mail.

+1


a source


The Ruby IMAP library contains a method append

you can use to "save" these outgoing emails in a folder of your choice:

# Let 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!

+5


a source







All Articles