Multi-line 'Body' in mime email

5534
5
09-17-2018 09:53 AM
JoeBorgione
MVP Emeritus

New to using mime within a python email...

https://www.smallsurething.com/multi-line-strings-in-python/ :  this guy provides a couple of ways to create a multi-line string, but I can't seem to convince mime to use it in the body of an email.

body = """first line
        second line
        final line"""
print(body)

first line
        second line
        final line‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

# I use it in mime like this:

    msg = MIMEMultipart()
    msg["From"] = emailfrom
    msg["To"] = COMMASPACE.join(emailto)
    msg["Subject"] = "Please see attached xls for errors"
    body = """OriginObjectID & DestinationObjectID are the two lines in the error
            RuleDescription is what rule has been broken
            Name should be self-explanatory"""
    
    msg.attach(MIMEText(body,'plain'))

As shown in lines 6-8, this approach produces an indent that I'd like to avoid.

template = ("This is the first line.\n"
            "This is the second line.\n"
            "This is the third line.")


print(template)
This is the first line.
This is the second line.
This is the third line.‍‍‍‍‍‍‍‍‍

msg = MIMEMultipart()
msg["From"] = emailfrom
msg["To"] = COMMASPACE.join(emailto)
msg["Subject"] = "Please see attached xls for errors"
body = ("OriginObjectID & DestinationObjectID are the two lines in the error\n"
        "RuleDescription is what rule has been broken\n"
        "Name should be self-explanatory")
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Is what I'd like to use, and it works great from the command line  but when I use it mime, the newlines are ignored so the body of my email is one long run-on sentence. What's the secret to get a nice looking, multi line mime-email body?

That should just about do it....
Tags (3)
0 Kudos
5 Replies
MichaelVolz
Esteemed Contributor

Are you creating this in Pro's version of python?

If so, have you tried just using msg = EmailMessage() instead of msg = MIMEMultipart()?

 

0 Kudos
JoeBorgione
MVP Emeritus

No.... Just a stand alone script; the script itself runs some gp tools against an enterprise gdb to get topology errors.  After working that end, it creates an xls file of the errors that I attach to the email.  I just want the users to understand how to read the xls....

That should just about do it....
0 Kudos
JoeBorgione
MVP Emeritus

I imported the textwrap module as described here textwrap – Formatting text paragraphs - Python Module of the Week  and tweaked my 'body' variable a little bit:

import datetime,smtplib,textwrap,arcpy

body = """\nOriginObjectID & DestinationObjectID are the two lines in the error
\nRuleDescription is what rule has been broken
\nName should be self-explanatory"""
body = textwrap.dedent(body).strip()
    
msg.attach(MIMEText(body,'plain'))

and the email body looks like this:

and that works for me!

That should just about do it....
DanPatterson_Retired
MVP Emeritus

explore textwrap a bit more... dedent, indent, wrap are a few of the gems that it contains. there are others

JoeBorgione
MVP Emeritus

It seems to be a pretty cool find!

That should just about do it....
0 Kudos