Sending gmail from web server - python

In my python file I am sending mail from gmail:
I used
server = smtplib.SMTP("smtp.gmail.com", 587)
I am successfully sending mails from my local system,
I executed same code in the web server I got this error:
File "/usr/lib/python2.4/smtplib.py", line 244, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python2.4/smtplib.py", line 306, in connect
raise socket.error, msg
socket.error: (97, 'Address family not supported by protocol')
How to send mails using gmail in web server using python?

Try SMTP_SSL, based on the below it'll probably help:
https://docs.python.org/2/library/smtplib.html

Related

Repl.it SMTPLIB OSError: [Errno 99] Cannot assign requested address

I am trying to run simple emailsender.py script on repl.it to send an email.
It works without any problem when I try to run it on desktop PC, but on repl.it I am having an error message OSError: [Errno 99] Cannot assign requested address as detailed below.
The emailsender.py program looks like this:
import smtplib
def send_email(username: str, key: str):
reciever = username+"#theirmail.cz"
sender = "my_email#email.cz"
topic = "Autothorization bot"
# header
msg = "From: %s\r\nSubject: %s\r\nTo: %s\r\n\r\n" % (sender, topic, reciever)
# add message content
content = "Your key is: " + key
msg += content
server = smtplib.SMTP('smtp.seznam.cz')
server.login('my_email#email.cz', "my_email_password")
server.sendmail(sender, reciever, msg)
server.quit()
When I try to run the script via python on repl.it, I get this error message after some time:
>>> import emailsender
>>> emailsender.send_email("username", "test_message")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/runner/botpy/emailsender.py", line 24, in send_email
server = smtplib.SMTP('smtp.seznam.cz')
File "/usr/lib/python3.8/smtplib.py", line 253, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python3.8/smtplib.py", line 337, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib/python3.8/smtplib.py", line 308, in _get_socket
return socket.create_connection((host, port), timeout,
File "/usr/lib/python3.8/socket.py", line 808, in create_connection
raise err
File "/usr/lib/python3.8/socket.py", line 796, in create_connection
sock.connect(sa)
OSError: [Errno 99] Cannot assign requested address
This .py script is a piece of larger discord.py bot that I am trying to host on repl.it, but this should not have any effect, because this script alone is running well individually on desktop PC.
I've tried my best to figure out the cause of this error, but with no success so far.
For reasons unknown to me, a small configuration of commands fixed it.
New part of the script for sending the email looks like this:
server = smtplib.SMTP('smtp.seznam.cz', 587)
server.starttls()
server.ehlo()
server.login('my_email#email.cz', "my_email_password")
server.sendmail(sender, reciever, msg)
server.quit()
I had the same error and
port=587 fixed the problem

Python Mailing timed out error

I cannot send an email from python. Probably there is a security issue. Can somebody give me a hint?
This is the code:
import smtplib
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
#Next, log in to the server
server.login("***#gmail.com", "PASSWORD")
#Send the mail
msg = "Hello!"
# The /n separates the message from the headers
server.sendmail("***#gmail.com", "***#gmail.com", msg)
server.quit()
This is the error I get:
server = smtplib.SMTP('smtp.gmail.com', 587)
File "D:\Tools\Lib\smtplib.py", line 244, in __init__
(code, msg) = self.connect(host, port)
File "D:\Tools\Lib\smtplib.py", line 310, in connect
raise socket.error, msg
error: (10060, 'Operation timed out')
you should use smtplib.SMTP_SSL() because port 587 expects ssl(tls) from the get go.
https://docs.python.org/2/library/smtplib.html#smtplib.SMTP_SSL
check with gmail sometimes gmail blocks the signing from apps

Unable to send email using gmail server using python

I am trying to send email using python. I tried many solution on Stackoverflow but none worked.
my code is -
import smtplib
fromaddr = 'user_me#gmail.com'
toaddrs = 'user_you#gmail.com'
msg = 'msg text here'
username = 'user_me#gmail.com'
password = 'pwd'
server = smtplib.SMTP('smtp.gmail.com',587)
server.ehlo()
server.starttls()
server.ehlo()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
Code remain stuck at line server = smtplib.SMTP('smtp.gmail.com',587)
Tried port 465 and get this error -
Traceback (most recent call last):
File "gmail-python.py", line 130, in <module>
mainFunction()
File "gmail-python.py", line 101, in mainFunction
mail = smtplib.SMTP('smtp.gmail.com', 465)
File "/usr/lib/python2.7/smtplib.py", line 256, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python2.7/smtplib.py", line 317, in connect
(code, msg) = self.getreply()
File "/usr/lib/python2.7/smtplib.py", line 368, in getreply
raise SMTPServerDisconnected("Connection unexpectedly closed")
smtplib.SMTPServerDisconnected: Connection unexpectedly closed
tried this - smtplib.SMTP_SSL('smtp.gmail.com',465) and got this error
raise SMTPException("STARTTLS extension not supported by server.")
smtplib.SMTPException: STARTTLS extension not supported by server.
removing server.ehlo() had no effect.
removing server.starttls() caused below error -
raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (534, '5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbsV\n5.7.14 3GHIcJvaksu56NrYm76aC0MqGI_sFI2cNaKSoFX6CV7TlyW593zn4dnJN7mG4TGI7edfcj\n5.7.14 l-MOV1Sb0Joyewnax8dxiL5BQ28Gg3UsWDfibI5TZ-tEaqqpL4WEzX5hsqYg6ZIQlczjyq\n5.7.14 UbElUx1C2E0QBt-X3uCuueb06uPBqehnwNV-hOlYFCgZpxRR9fdi-rO65oD5c1asYyGaTh\n5.7.14 1VRKgMYJp-o_uMhMjGEyJWKLLYX3A> Please log in via your web browser and\n5.7.14 then try again.\n5.7.14 Learn more at\n5.7.14 https://support.google.com/mail/answer/78754 r85sm13251579pfr.48 - gsmtp')
I have enabled less secure app access in my account. Python version 2.7.6 on ubuntu.
Try after turning of Gmail's Security Feature Here.
This may be preventing 3rd party application from accessing Gmail for security purpose
https://www.google.com/settings/security/lesssecureapps

Socket Error - Sending email using gmail

I am using the code from this article to send an email using gmail.
The code is as follows
import smtplib
def sendemail(from_addr, to_addr_list, cc_addr_list,
subject, message,
login, password,
smtpserver='smtp.gmail.com:587'):
header = 'From: %s\n' % from_addr
header += 'To: %s\n' % ','.join(to_addr_list)
header += 'Cc: %s\n' % ','.join(cc_addr_list)
header += 'Subject: %s\n\n' % subject
message = header + message
server = smtplib.SMTP(smtpserver)
server.starttls()
server.login(login,password)
problems = server.sendmail(from_addr, to_addr_list, message)
server.quit()
return problems
When i run it, I however get the following socket error.
socket.error: [Errno 10013] An attempt was made to access a socket in a way forbidden by its access permissions
EDIT: Traceback
O:\Send Email With Attachment>SendEmail_Ver2.py
Traceback (most recent call last):
File "O:\Send Email With Attachment\SendEmail_Ver2.py", line 26, in <module>
password = 'XXXXXXXXXXX')
File "O:\SendEmail_Ver2.py", line 13, in sendemail
server = smtplib.SMTP(smtpserver)
File "C:\Program Files (x86)\Python26\lib\smtplib.py", line 239, in __init__
(code, msg) = self.connect(host, port)
File "C:\Program Files (x86)\Python26\lib\smtplib.py", line 295, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Program Files (x86)\Python26\lib\smtplib.py", line 273, in _get_socket
return socket.create_connection((port, host), timeout)
File "C:\Program Files (x86)\Python26\lib\socket.py", line 561, in create_connection
raise error, msg socket.error: [Errno 10013] An attempt was made to access a socket in
a way forbidden by its access permissions`
How do I get privileges for the script to use port 587? Or is there a higher port number that gmail can use?
Thanks
The problem was McAfee Antivirus. It was blocking the socket connection.
From McAfee Access Protection Log
23/08/2013 10:23:54 a.m. Blocked by port blocking rule C:\Program Files (x86)\Python26\python.exe Anti-virus Standard Protection:Prevent mass mailing worms from sending mail 74.125.25.108:25
I disabled McAfee completely and it worked fine after that.
The problem for me was also McAfee. But instead of disabling McAfee completely as user1494941 did, I would recommend adding python.exe to the list of excepted programs:
In the VirusScan Console,
Right-click on 'Access Protection', select 'Properties'
In Categories, Select 'Anti-virus Standard Protection'
In the Rules panel, select 'Prevent mass mailing worms from sending email'
Select 'Edit', to edit the rules for this protection.
Add python.exe to the list of processes to exclude and then OK.

Can not start smtp in Python

I tried to start a smtp server with python with the following code:
import smtplib
smtp_server = smtplib.SMTP('localhost')
And I get the following error:
File "test.py", line 2, in <module>
smtp_server = smtplib.SMTP('localhost')
File "/usr/local/lib/python2.7/smtplib.py", line 242, in __init__
(code, msg) = self.connect(host, port)
File "/usr/local/lib/python2.7/smtplib.py", line 302, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/local/lib/python2.7/smtplib.py", line 277, in _get_socket
return socket.create_connection((port, host), timeout)
File "/usr/local/lib/python2.7/socket.py", line 571, in create_connection
raise err
I tried to telnet localhost 25 and telnet localhost but they both give the following error:
Trying 127.0.0.1...
telnet: connect to address 127.0.0.1: Connection refused
telnet: Unable to connect to remote host: Connection refused
The same python code runs perfectly well on one of my other machines.
Any advice ?
Thanks.
UPDATE:
The machine I'm having problem with is running Python 2.7.2, while the machine that the code works well is running Python 2.6.2. I don't know if this is one of the reasons.
smtplib is SMTP protocol client. What you are looking for is smtpd.
The problem is that smtplib does not provide an SMTP server implementation, it implements SMTP client. Most likely, the code tries to connect to localhost and fails. See http://docs.python.org/2/library/smtplib.html
Is the other machine you write about already running an SMTP server?
And what are you trying to do?

Categories