5

So I'm trying to send an email through SMTPlib with Python, but I can't get it to work. I read up on the Microsoft SMTP specs, and put them in accordingly, but I can't get it to work. Here is my code:

    # Send an email
    SERVER  = "smtp-mail.outlook.com"
    PORT    = 587
    USER    = "******@outlook.com"
    PASS    = "myPassWouldBeHere"
    FROM    = USER
    TO      = ["******@gmail.com"]
    SUBJECT = "Test"
    MESSAGE = "Test"
    message = """\
From: %s
To: %s
Subject: %s
%s
""" % (FROM, ", ".join(TO), SUBJECT, MESSAGE)
    try:
        server = smtplib.SMTP()
        server.connect(SERVER, PORT)
        server.starttls()
        server.login(USER,PASS)
        server.sendmail(FROM, TO, message)
        server.quit()
    except Exception as e:
        print e
        print "\nCouldn't connect."

I got the code from a keylogger, but I cleaned it up a bit. I read up here on how basic SMTP works, but there are few things like starttls (Methods) I don't quite understand.

I really appreciate any help with this.

null
  • 548
  • 2
  • 6
  • 17
  • What does "*I can't get it to work*" mean? Do you get an error message? Does it crash you PC? – Robᵩ May 19 '15 at 01:20
  • The immediate problem here is that you are not creating a valid SMTP message. At a minimum, you need an empty line between the headers and the message body; but the proper solution is to use Python's `email` library to create an actually valid SMTP message; pasting together strings only works when your message is trivial, and/or you know exactly what you are doing. – tripleee Nov 23 '21 at 10:54

1 Answers1

11

Try this. This works in Python 2.7.

def send_mail(recipient, subject, message):

    import smtplib
    from email.MIMEMultipart import MIMEMultipart
    from email.MIMEText import MIMEText

    username = "sender@outlook.com"
    password = "sender's password"

    msg = MIMEMultipart()
    msg['From'] = username
    msg['To'] = recipient
    msg['Subject'] = subject
    msg.attach(MIMEText(message))

    try:
        print('sending mail to ' + recipient + ' on ' + subject)

        mailServer = smtplib.SMTP('smtp-mail.outlook.com', 587)
        mailServer.ehlo()
        mailServer.starttls()
        mailServer.ehlo()
        mailServer.login(username, password)
        mailServer.sendmail(username, recipient, msg.as_string())
        mailServer.close()

    except error as e:
        print(str(e))


send_mail('recipient@example.com', 'Sent using Python', 'May the force be with you.')
Kish
  • 132
  • 1
  • 4
  • If the message only has one part, wrapping it in a multipart is obviously redundant. – tripleee Jun 09 '15 at 05:18
  • It is now 2021, and you should probably not use code which targets the Python `email` library as it was before version 3.6 (2016). – tripleee Nov 23 '21 at 10:55
  • If you have smtplib.SMTPNotSupportedError: STARTTLS extension not supported by server. errors, in the ehlo commands specify any lowercase string as the parameter. For more detail: https://stackoverflow.com/questions/69541840/ssl-error-while-sending-email-using-smtp-with-python – healthybodhi Jan 23 '23 at 00:02