0

My email program is emailing each line of the message as a separate email, i would like to know how to send all of the message in one email, when the email is sent the program will return to the beginning.

import smtplib
from email.mime.text import MIMEText

a = 1
while a == 1:
    print " "
    To = raw_input("To: ")
    subject = raw_input("subject: ")
    input_list = []
    print "message: "
    while True:
        input_str = raw_input(">")
        if input_str == "." and input_list[-1] == "":
            break
        else:
            input_list.append(input_str)

    for line in input_list:

        # Create a MIME text message and populate its values
        msg = MIMEText(line)
        msg['Subject'] = subject
        msg['From'] = '123@example.com'
        msg['To'] = To

        server = smtplib.SMTP_SSL('server', 465)
        server.ehlo()
        server.set_debuglevel(1)
        server.ehlo()
        server.login('username', 'password')

        # Send a properly formatted MIME message, rather than a raw string
        server.sendmail('user@example.net', To, msg.as_string())
        server.close()

(the part that takes multiple lines was made with the help Paul Griffiths, Multiline user input python)

Community
  • 1
  • 1
kyle k
  • 5,134
  • 10
  • 31
  • 45

1 Answers1

3
for line in input_list:

        # Create a MIME text message and populate its values
        msg = MIMEText(line)

You are calling server.sendmail in this loop.

Build your entire msg first (in a loop), THEN add all of your headers and send your message.

Joe
  • 41,484
  • 20
  • 104
  • 125