Sample code for 30+ languages & platforms
PowerBuilder

Attach an Email to Another Email

See more Email Object Examples

Demonstrates the Chilkat Email.AttachEmail method, which attaches a copy of another email to this email object. The attached email is encapsulated in a message/rfc822 subpart. Because a copy is attached, later changes to the source email do not affect the embedded message. This example attaches one email to another.

Background: "Forward as attachment" produces exactly this structure: the original message is embedded whole as a message/rfc822 part rather than quoted into the body. This preserves the original's headers and formatting intact, which matters for forwarding to a mailbox that will re-parse it, or for reporting spam/phishing with the original evidence attached. Such parts are counted by NumAttachedMessages.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_InnerEmail
oleobject loo_Email

li_Success = 0

//  Demonstrates the AttachEmail method, which attaches a copy of another email to this email
//  object.  The attached email is encapsulated in a message/rfc822 subpart.

loo_InnerEmail = create oleobject
li_rc = loo_InnerEmail.ConnectToNewObject("Chilkat.Email")
if li_rc < 0 then
    destroy loo_InnerEmail
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_InnerEmail.Subject = "Original message"
loo_InnerEmail.From = "alice@example.com"
loo_InnerEmail.Body = "This is the original message being forwarded."

loo_Email = create oleobject
li_rc = loo_Email.ConnectToNewObject("Chilkat.Email")

loo_Email.Subject = "FW: Original message"
loo_Email.From = "bob@example.com"
loo_Email.Body = "See the attached original email."

//  Attach a copy of the inner email as a message/rfc822 part.

li_Success = loo_Email.AttachEmail(loo_InnerEmail)
if li_Success = 0 then
    Write-Debug loo_Email.LastErrorText
    destroy loo_InnerEmail
    destroy loo_Email
    return
end if

Write-Debug "NumAttachedMessages = " + string(loo_Email.NumAttachedMessages)


destroy loo_InnerEmail
destroy loo_Email