Search code examples
pythonpython-3.xemailsmtplib

Line breaks in SMTPLIB Python 3


I am trying to make a script that sends a email. But how to do line breaks? I tried the following:

Change msg = MIMEText(msginp) to msg=MIMEText(msginp,_subtype='plain',_charset='windows-1255')

NOTE: msginp is a input (msginp = input('Body? '))

Does anybody know how to make line breaks? Like the enter key on your keyboard?

My code is:

import smtplib
from email.mime.text import MIMEText
import getpass
smtpserverinp = input('What SMTP server are you using? (Look here for more information. https://sites.google.com/view/smtpserver) ')
usern = input('What is your email adderess? ')
p = getpass.getpass(prompt='What is your password? (You will not see that you are typing because it is a password) ')
subjectss = input('Subject? ')
msginp = input('Body? ')
toaddr = input('To who do you want to send it to? ')
msg = MIMEText(msginp)

msg['Subject'] = subjectss
msg['From'] = usern
msg['To'] = toaddr

s = smtplib.SMTP(smtpserverinp, 587)
s.starttls()
s.login(usern, p)
s.sendmail(usern, toaddr, msg.as_string())
s.quit()

Thanks!


Solution

  • I can't add a comment to see if this was totally what you want but i'll give it ago.

    Your msginp = input('Body? ') ends once you press the enter key, for a new line you'd need to enter \n. Rather than typing \n each time you can make a loop. Once all the data has been collected you can use the MIMEText as you would before.

    Replace your msginp and MIMEText with this (total is the msginp)

    total = ""
    temp = input("Data: ")
    total = temp+"\n"
    while(temp!=""):
        temp = input("next line: ")
        total = total+temp+"\n"
    msg = MIMEText(total)