In the most cases, you use ActionMailer to design and deliver your emails. But what if you already have an email and need to forward it as is?
In this case you can use the TMail::Mail object directly and with simple code you can forward the email content without doing any content manipulation. The following steps shows how you can forward an email content.
- If you have the email content as string, you can ask TMail to parse this string into TMail::Mail object:
@email = TMail::Mail.parse(email_content) - Using the parsed TMail::Mail object, you can create a forward copy of the email using one line of code:
@forward_email = @email.create_forward() - Now, you have a prepared forward email content, you can change any field you want according to your requirements:
@forward_email.to = 'my_friend@domain.com'
@forward_email.from = 'me@domain.com'
Note: don't change the forward email body, it contains the actual email content you need to forward. - Next, lets deliver this forwarded email using our ActionMailer model. You need to write two lines of code to do that:
@forward_email.write_back
YourMailerHanlder.deliver(@forward_email)
The first line flattens the TMail::Mail object into a MIME message ready for sending.
The second line asks the ActionMailer to deliver the given email.
This technique will not help you in adding any new content into the forwarded email, you just forward it into an empty email as is. I did that while working in RoR project in eSpace.
No comments:
Post a Comment