I was sending emails from flask-mail but since trying to use the mailservers at namecheap or bluehost, I'm getting the following error:
SSLError: [Errno 1] _ssl.c:510: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol
So now I'm trying to send the email without flask-mail but I'm still getting the same error. Any fix?
My code is as follows:
from smtplib import SMTP
smtp = SMTP()
smtp.set_debuglevel(debuglevel)
smtp.connect('xxxxxx', 26)
smtp.login('noreply#xxx.com', 'xxxxxxx')
from_addr = "xxx <noreply#xxx.com>"
to_addr = rec#xxx.com
subj = "hello"
date = datetime.datetime.now.strftime( "%d/%m/%Y %H:%M" )
message_text = "Hello\nThis is a mail from your server\n\nBye\n"
msg = "From: %s\nTo: %s\nSubject: %s\nDate: %s\n\n%s" % ( from_addr, to_addr, subj, date, message_text )
smtp.sendmail(from_addr, to_addr, msg)
smtp.quit()
My application is running on Ubuntu 14.04 on Amazon EC2.
Thanks.
The reason that this is giving you this error is because your mail server is not a SMTP server. Use Gmail or another smtp mail service to send the mail. Try sending it through a gmail account with the server being smtp.gmail.com and the port being 587. First though you will need to configure your account for it.
Related
I have searched many python email related post here but I seem not able to send a single email using python. I am working within corporate LAN. But If I use my company's smtp relay server I am able to send email to gmail, or my company ID or other emails.This is very strange or it is suppose to be this way? How can I use gmail smtp server to send email outside?
Here are few scripts I have tried.
====================================1==
import smtplib
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login("xyz#gmail.com", "myPasswd")
msg = "YOUR MESSAGE!"
server.sendmail("xyz#gmail.com", "xyz#gmail.com", msg)
server.quit()
===================================2==
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
msg = MIMEMultipart()
msg['From'] = 'sender#gmail.com'
msg['To'] = 'recvr#gmail.com'
msg['Subject'] = 'simple email in python'
message = 'here is the email'
msg.attach(MIMEText(message))
mailserver = smtplib.SMTP('smtp.gmail.com',587)
# identify ourselves to smtp gmail client
mailserver.ehlo()
# secure our email with tls encryption
mailserver.starttls()
# re-identify ourselves as an encrypted connection
mailserver.ehlo()
mailserver.login('sender#gmail.com', 'pswd')
mailserver.sendmail('sender#gmail.com','rcvr#gmail.com',msg.as_string())
mailserver.quit()
======================================
I am sure it is nothing to do with code but something else I am missing.
I had the same problem
It seems that google find your application is insecure. You can:
Allow less secure applications to connect to the server in google apps settings. see Allowing less secure apps to access your account
Secure your code [whatever it means by google ...]
I have a server on app engine from which I'm trying to send a mail with an SMTP socket. The code goes something like this:
session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
session.starttls()
session.ehlo()
session.login(sender, sender_password)
session.sendmail(sender, recipient, msg.as_string())
session.quit()
and I get the error in the title on the line for "session.starttls()".
Any ideas on how to solve this?
Thanks
AppEngine does not permit apps to send mail directly. Use the AppEngine Mail Python API instead.
I was trying to build a python script which could send emails via Gmail.
When I try to connect to the Gmail Server, I get an error Errno 10013.
This is what I'm trying to do:
gmail_message = smtplib.SMTP('smtp.gmail.com')
gmail_message.starttls()
gmail_message.login('xyz#gmail.com','xyzpassword')
gmail_message.sendmail('xyz#gmail.com',['xyz#gmail.com'], msg.as_string())
gmail_message.quit()
Error:
error: [Errno 10013] An attempt was made to access a socket in a way forbidden by its access permissions
What can I do to fix this?
You may need to specify the port by
gmail_message = smtplib.SMTP(host='smtp.gmail.com', port=587)
This is code that worked for me on Win7. It's pretty much identical to yours.
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username,password)
for comp in comps:
msg = "Subject:CommenceSweep:Comp%s-%s\n\n" % (comp,sweep)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
Note: Did you correctly configure Gmail to allow SMTP?
I'm building and testing a web service on my local machine before i put it in production. I want to test the mail service. I'm using the standard python email and smtplib libraries.
import smtplib
from email.mime.text import MIMEText
fp = open('textfile', 'rb')
msg = MIMEText(fp.read())
fp.close()
me = 'me_email#localhost'
you = 'me_again_email#localhost'
msg['Subject'] = 'The contents of %s' %fp
msg['From'] = me
msg['To'] = you
s = smtplib.SMTP('localhost')
s.sendmail(me, [you], msg.as_string())
s.quit()
I have not configured sendmail and hence it throws an error. But since I just want to test my web service I am not concerned if sendmail cant send an email right now. My service is designed to pulls some records off db and send them an email. So i want to know if this connection, python taking inputs from db and pushing an email is working. I want to receive the email on localhost sent via the script.
An SMTP server needs to be configured for sending emails. Sending emails is not possible unless you configure an SMTP server. More information on using python smtplib can be found in pymotw.com, tutorialspoint.com and Python docs.
I use lib secure_smtpd for create SMTP server. When I generate certificate and use it for SSL connection I catch exception (for test I use Opera mail client and The Bat!):
SSLError: _ssl.c:489: The handshake operation timed out
When I test use python script everything is ok:
smtpObj = smtplib.SMTP_SSL('localhost',2000)
smtpObj.set_debuglevel(1)
smtpObj.login('testuser', '111111')
msg = MIMEMultipart('alternative')
msg['Subject'] = "my subj SSL"
msg['From'] = sender
msg['To'] = "username#site.com"
msg.attach(MIMEText("Hello world!",'html'))
smtpObj.sendmail(sender, [toemail], msg.as_string())
Can somebody help fix problem with handshake?
I use python 2.7.3
How you configured SMTP in Opera and The Bat! ? There is SSL mode (which you seems to use), where all connection is wrapped in SSL/TLS, and STARTTLS mode, where connection is plain but SSL/TLS excahnge is started after client issues STARTTLS command.