如何使用ActionMailer在IMAP"已发送"框中保存外发电子邮件的副本?

Tim*_* T. 3 imap ruby-on-rails actionmailer

当我使用常规客户端并通过SMTP为IMAP帐户发送电子邮件时,该出站电子邮件将保存在IMAP"已发送"框中.

当我使用Ruby on Rails ActionMailer发送电子邮件时,我怎么能有相同的行为?

小智 6

Ruby IMAP库包含一种append方法,您可以使用该方法将这些出站电子邮件"保存"到您选择的文件夹中:

# 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
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!