AnsweredAssumed Answered

Multi-line 'Body' in mime email

Question asked by jborgion Champion on Sep 17, 2018
Latest reply on Sep 17, 2018 by jborgion

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?

Outcomes