当我使用我的常规客户端并通过 SMTP 为 IMAP 帐户发送电子邮件时,该出站电子邮件将保存在 IMAP“已发送”框中。
当我使用 Ruby on Rails ActionMailer 发送电子邮件时,我怎样才能有相同的行为?
当我使用我的常规客户端并通过 SMTP 为 IMAP 帐户发送电子邮件时,该出站电子邮件将保存在 IMAP“已发送”框中。
当我使用 Ruby on Rails ActionMailer 发送电子邮件时,我怎样才能有相同的行为?
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
希望这可以帮助!
据我所知,这往往是您的邮件客户端程序中的设置;但我在 ActionMailer 中没有看到太多支持。
有一个 ruby IMAP 库,如果您发现消息存储在服务器上,但只是在错误的位置。http://ruby-doc.org/stdlib/libdoc/net/imap/rdoc/index.html
一种解决方法可能是将每封邮件发送到您的原始电子邮件地址,例如sender@yourdomain.com
,可能带有标签sender+sent@yourdomain.com
,然后在您将查看此收件箱的客户端中设置一个规则,以将所有带有该邮件的电子邮件路由TO:
到“已发送邮件”框。
如果您使用 gmail 作为您的 rails 应用程序的邮件服务器,它会自动在发送的邮件中保存一份副本。