0

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!

  • I don't think there's an easy/straight-forward way to do this in the terminal. It's time to start looking at a simple GUI library like [Tkinter](https://docs.python.org/3/library/tk.html) – danidee Jan 29 '17 at 14:04

2 Answers2

1

Exit when you enter empty line

print("Body? ", end="")

lines = []
while True:
    line = input("")
    if not len(line):
        break
    lines.append(line)

print("\n".join(lines))
zelenyjan
  • 663
  • 6
  • 7
1

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)
Orange
  • 126
  • 6