Sunday, 15 June 2008

Forwarding Email as Attachment Using ActionMailer

Using the default TMail::Mail.create_forward for forwarding emails will not help you using email templates for designing the message structure.

If you have an email message and you want to forward it using ActionMailer email templates all you need to do is attach the email content into your email template with content type 'message/rfc822' and file name with extension 'eml' (message.eml).

Lets see some code do that:

  1. Create your email template view file, lets called it "forward_email.rhtml" and fill it with your required content/design.
  2. In your ActionMailer model, create a method with the same name "forward_email" that accepts string argument contain the email content you need to forward:

    def forward_email(forward_email_content)
    recipients "test@espace.com.eg"
    from "info@espace.com.eg"
    subject "Testing forward email content as attachment"
    attachment "message/rfc822" do |a|
    a.filename = 'email.eml'
    a.body = forward_email_content
    a.transfer_encoding = '7bit'
    a.content_disposition = 'inline'
    end
    end

In this way, you can forward the email content as usual but inside your designed email template.

No comments: