locked
Send Email With Attachment As Email Body RRS feed

  • Question

  • I am trying to send an email with an attachment as the body of the email. I want to replicate the attach-->insert as text option that you can use in the actual Outlook application.

    This is the code I have tried, but it does not work to insert my document as text in the body of the email.

    import win32com.client

    #some constants (from http://msdn.microsoft.com/en-us/library/office/aa219371%28v=office.11%29.aspx)
    olFormatHTML = 2
    olFormatPlain = 1
    olFormatRichText = 3
    olFormatUnspecified = 0
    olMailItem = 0x0

    obj = win32com.client.Dispatch("Outlook.Application")

    attachment1 = "test.docx"

    with open(attachment1 , 'r') as myfile:
        data=(myfile.read())

    newMail = obj.CreateItem(olMailItem)
    newMail.Subject = "I AM SUBJECT!!"
    newMail.BodyFormat = olFormatHTML    #or olFormatRichText or olFormatPlain
    newMail.HTMLBody = data
    newMail.HTMLBody = "<h1>I am a title</h1><p>I am a paragraph</p>"
    newMail.To = "help@example.com"


    newMail.Attachments.Add(Source=attachment1)

    # open up in a new window and allow review before send
    newMail.Display()

    Saturday, February 3, 2018 12:49 AM

All replies

  • Hi RocketGirl87654321,

    You had mentioned that,"I want to replicate the attach-->insert as text option that you can use in the actual Outlook application."

    You can try to refer example below may help you.

    Sub demo()
        Dim wd As Object, editor As Object
        Dim doc As Object
        Dim oMail As MailItem
    
        Set wd = CreateObject("Word.Application")
        Set doc = wd.Documents.Open("C:\Users\v-padee\Desktop\mail_body.docx")
        doc.Content.Copy
        doc.Close
        Set wd = Nothing
    
        Set oMail = Application.CreateItem(olMailItem)
        With oMail
            .BodyFormat = olFormatRichText
            Set editor = .GetInspector.WordEditor
            editor.Content.Paste
            .Display
        End With
    End Sub
    

    Further, You can modify the code as per your requirement.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, February 5, 2018 6:17 AM
  • Hi RocketGirl87654321,

    Is your issue solved?

    I find that you did not follow up this thread after posting the issue.

    If your issue is solved then I suggest you to post your solution and mark it as an answer.

    If your issue is still exist then try to refer the solution given by the community members.

    If then also you have any further questions then let us know about it.

    We will try to provide further suggestions to solve the issue.

    Thanks for your understanding.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, February 9, 2018 9:17 AM