2

I am new to using smtplib and wanted to used it to automate the mass-mail sending process in my company. I tried this basic script -

#!/usr/bin/env python
import smtplib

server = smtplib.SMTP('smtp.gmail.com:587')
user_mail = '[user_email]@gmail.com'
user_pwd = '[password]'
rcv_mail = '[reciever_email]@yahoo.com'
msg = 'Test'

server.login(user_mail, user_pwd)

server.ehlo()
server.starttls()
server.sendmail(user_mail, rcv_mail, msg)
server.quit()

I am not using this script as is in my program, this is just to get a hang of how smtplib works. I'm getting this error

Traceback (most recent call last):
  File "sampleSMTP.py", line 4, in <module>
    server = smtplib.SMTP('smtp.gmail.com. 587')
  File "/usr/local/lib/python2.7/smtplib.py", line 256, in __init__
    (code, msg) = self.connect(host, port)
  File "/usr/local/lib/python2.7/smtplib.py", line 316, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "/usr/local/lib/python2.7/smtplib.py", line 291, in _get_socket
    return socket.create_connection((host, port), timeout)
  File "/usr/local/lib/python2.7/socket.py", line 557, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno -2] Name or service not known

Previously in other iterations I have tried using MIMEMultipart and MIMEText from the email module, and I have got as output other Exceptions too, like ServerAuthenticationError (<- I did some print debugging in this one and found that the exception was raised from the server.login() statement).

My request is, can someone explain to me these exceptions and tell me what I need to do to get rid of them? I dont need a working code, just a good explanation.

Edit: There had been a syntax error previously, which I corrected. The Exception is now this:

Traceback (most recent call last):
File "sampleSMTP.py", line 10, in <module>
    server.login(user_mail, user_pwd)
File "/usr/local/lib/python2.7/smtplib.py", line 585, in login
    raise SMTPException("SMTP AUTH extension not supported by server.")
smtplib.SMTPException: SMTP AUTH extension not supported by server.

My original question stands - how and why are these exceptions caused in smtplib methods, and how can I resolve them?

Bumble Bee
  • 133
  • 6
  • `'smtp.gmail.com. 587'` is not a valid hostname. Also be aware that Google does not like mass mails and will block you send too many. – Klaus D. Dec 23 '17 at 05:21
  • Thanks for pointing that out - it was a typo on my part. And yes, I am aware of google's spam and mass mail policy, but I thought it kicks up only for adding too many recipients for a single sent mail? I'm planning to send personalised mail for every recipient – Bumble Bee Dec 24 '17 at 09:43
  • You show a code, say that you did not use it *as is* and show errors without the code that caused them. I'm sorry but as my crystal ball is currently out of order I cannot guess what you real code is... Please show the code and the errors associated if you want help in fixing errors. – Serge Ballesta Dec 24 '17 at 10:05
  • @BumbleBee Why don't you try to use SMTP SSL wrapper (port 465) and don't forget to [let less secure apps to use your account](https://support.google.com/accounts/answer/6010255?hl=en). – sstevan Dec 24 '17 at 11:33

2 Answers2

0

While creating SMTP object, you should provide hostname as name:port. So, server = smtplib.SMTP('smtp.gmail.com. 587') is incorrect. It should be server = smtplib.SMTP('smtp.gmail.com:587').

If you get errors after this, refer to this thread: How to send an email with Gmail as provider using Python?

-1

server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()

This is a same question.

I_love_python
  • 67
  • 1
  • 2
  • 7