Rails ActionMailer将电子邮件复制到已发送文件夹SMTP ruby

时间:2014-10-01 07:48:49

标签: ruby-on-rails email smtp actionmailer

您好我可以使用发送邮件功能发送我想要的电子邮件但是客户发送文件夹中没有发送邮件。

我是否必须以某种方式手动将电子邮件复制到已发送的文件夹?配置在下面并且可以发送电子邮件,仅供参考

    config.action_mailer.smtp_settings = {
      address:              'send.XXX.com',
      port:                 587,
      domain:               'XXX.com',
      user_name:            'X@XXX.com',
      password:             'XXX',
      authentication:       'plain',
      enable_starttls_auto: true  }

如何在Rails中执行此操作

更新

可以使用IMAP轻松完成,只需发新邮件,Mail.new等

   target_mailbox = 'Inbox.Sent'
   imap = Net::IMAP.new("imap.someserver.com")
   imap.authenticate(email_account.authentication, email_account.user_name, email_account.password)  
   imap.create(target_mailbox) unless imap.list('', target_mailbox)
   imap.append(target_mailbox, imap_sent_mail_copy.to_s)
   imap.logout
   imap.disconnect

1 个答案:

答案 0 :(得分:1)

          target_mailbox = 'Inbox.Sent'
          imap = Net::IMAP.new("imap.someserver.com")
          imap.authenticate(email_account.authentication, email_account.user_name, email_account.password)  
          imap.create(target_mailbox) unless imap.list('', target_mailbox)
          imap.append(target_mailbox, imap_sent_mail_copy.to_s)
          imap.logout
          imap.disconnect

其中imap_sent_mail_copy.to_s只是一个邮件实例

imap_sent_mail_copy = Mail.new do
  to to_email_address
  from from
  subject subject
  message_id message_id
  if attachment_ids!=""
    docs = Document.where(id: attachment_ids)
    docs.each do |a|
      doc = Document.find_by_id(a)
        attachments["#{doc.name}.#{doc.file_ext}"] = File.read("#{Rails.root}/public#{doc.attachment_url}") if doc
    end
  end
  html_part do
    content_type 'text/html; charset=UTF-8' 
    body "#{message} #{signature}"
  end
end
相关问题