Search code examples
pythonstringmimesmtplib

How to combine two string variables into one multiline variable?


I want to use python to send an email with two rows(lines) of text like this

a    b    c    d
e    f    g    h

I tried to use xxx.attach() but I found that the second row is replaced the first one.

part1 = MIMEText(text1, 'plain')
part2 = MIMEText(text2, 'plain')
msg.attach(part1)
msg.attach(part2)

So, I tried to define each line as a string variable and then combine it together. Finally, I attach only one variable.

A="a   b   c   d"
B="e   f   g   h"

How to combine A and B to get the result as above?

PS. I am using Python3.6 on Windows10


Solution

  • Use the os module for this

    import os
    A = "a   b   c   d"
    B = "e   f   g   h"
    temp = os.linesep.join([A,B])