1

Hi guys I'm writing a program to send and email from one email to another from the command line. Below is my program so far but I can't seem to get session.login(sender, password) to work. When I get rid of the "Try, except" function I get an error saying "SMTP AUTH extension not available on this server". I'm stuck and need help.

import smtplib

sender = raw_input("Please put in your e-mail")
password = raw_input("Email password please")
recipient = raw_input("Please put person e-mail")
message = raw_input("Insert message")

try:
    session = smtplib.SMTP("smtp.gmail.com:587")
    session.ehlo()
    session.starttls
    session.ehlo()
    #having issues with this specific command
    session.login(sender, password)
    session.sendmail(sender, recipient, message)
    session.quit
    print("message sent :)")
except smtplib.SMTPException:
    print("message not sent :(")
johnharris85
  • 17,264
  • 5
  • 48
  • 52
mqasim13
  • 11
  • 1

3 Answers3

1

You never actually call the function starttls:

 session.starttls  -> session.starttls() # add parens to call

Less relevant but you also don't call quit session.quit -> session.quit()

Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
0

Please, try yagmail. Disclaimer: I'm the maintainer, but I feel like it can help everyone out!

It really provides a lot of defaults: I'm quite sure you'll be able to send an email directly with:

import yagmail
yag = yagmail.SMTP(username, password)
yag.send(to_addrs, contents = msg)

You'll have to install yagmail first with either:

pip install yagmail  # python 2
pip3 install yagmail # python 3

Once you will want to also embed html/images or add attachments, you'll really love the package!

It will also make it a lot safer by preventing you from having to have your password in the code.

PascalVKooten
  • 20,643
  • 17
  • 103
  • 160
  • I tried installing yagmail and got this error message "Command "/usr/bin/python -c "import setuptools, tokenize;__file__='/private/var/folders/6m/qyyw1qz94qdc_h0z05szylnw0000gp/T/pip-build-CkeJNF/keyring/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /var/folders/6m/qyyw1qz94qdc_h0z05szylnw0000gp/T/pip-3EQldH-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /private/var/folders/6m/qyyw1qz94qdc_h0z05szylnw0000gp/T/pip-build-CkeJNF/keyring " – mqasim13 Jul 27 '15 at 22:36
0

'recipient' must be in list form for this to work.

Try changing

session.sendmail(sender, recipient, message)

to

session.sendmail(sender, [recepient], message)
myd
  • 1
  • 1