Reputation: 946
I am trying to write a Python class that will send emails using Python's smtplib
libraries. I got it to work with standard Gmail, but am having trouble getting the config to work with other accounts.
Emailer Class config and send method:
def configure(self, serverLogin, serverPassword, fromAddr, toAddr, serverHost='mail.myserver.edu', serverPort=465):
self.server=smtplib.SMTP(serverHost,serverPort) #set the server host/port - currently configured for gmail
self.server.ehlo()
self.server.starttls()
self.server.ehlo()
self.server.login(serverLogin, serverPassword) #login to senders email
self.fromAddr = fromAddr
self.toAddr = toAddr
def send(self):
msgText = email.MIMEText.MIMEText("\n".join(self.message))
self.msg.attach(msgText)
print "Sending email to %s " % self.toAddr
text = self.msg.as_string() #conver the message contents to string format
self.server.sendmail(self.fromAddr, self.toAddr, text) #send the email
Emailer Test method:
def test(self):
message = "Like a boss" #body message of the email
attachment = ["/Desktop/testImage2"] #list of attachments
image = ["/Desktop/testImage2"]
emailer = Emailer()
emailer.addAttachment(attachment)
emailer.addToMessage(message,image)
emailer.setSubject("Python Test")
emailer.configure(serverLogin="loginname", serverPassword="password", fromAddr="[email protected]", toAddr=["[email protected]"])
emailer.send()
I set up my email account successfully in other mail clients (like Outlook) using this information, and the classes work just fine when I use serverHost = 'smtp.gmail.com'
and serverPort = 587
. However, when I run the test()
class in the terminal with my non-Gmail server information, the code appears to hang on this bit:
/Users/anaconda/lib/python2.7/smtplib.pyc in __init__(self, host, port, local_hostname, timeout)
254 self.esmtp_features = {}
255 if host:
--> 256 (code, msg) = self.connect(host, port)
257 if code != 220:
258 raise SMTPConnectError(code, msg)
/Users/anaconda/lib/python2.7/smtplib.pyc in connect(self, host, port)
315 print>>stderr, 'connect:', (host, port)
316 self.sock = self._get_socket(host, port, self.timeout)
--> 317 (code, msg) = self.getreply()
318 if self.debuglevel > 0:
319 print>>stderr, "connect:", msg
/Users/anaconda/lib/python2.7/smtplib.pyc in getreply(self)
359 while 1:
360 try:
--> 361 line = self.file.readline(_MAXLINE + 1)
362 except socket.error as e:
363 self.close()
/Users/anaconda/lib/python2.7/socket.pyc in readline(self, size)
474 while True:
475 try:
--> 476 data = self._sock.recv(self._rbufsize)
477 except error, e:
478 if e.args[0] == EINTR:
KeyboardInterrupt:
Can anyone tell me why it's getting stuck here?
Upvotes: 2
Views: 6972
Reputation: 946
So it turns out the problem was that I needed to be using SMTP_SSL
instead of just SMTP
because of the security settings on my server.
Upvotes: 5
Reputation: 125
Try the telnet test for you server, the python error described is connection problem. Try telnet mail.myserver.edu 465
for example.
Upvotes: 0
Reputation: 125
It's maybe is connection problem. Check the access using telnet and check the server answer.
telnet smtp.gmail.com 587
Trying 64.233.186.109...
Connected to smtp.gmail.com.
Escape character is '^]'.
220 smtp.gmail.com ESMTP f103sm809119qkh.38 - gsmtp
Upvotes: 0