In one of my projects I have to create python application to send email reports from local machine, using external smtp infrastructure.
I am using python 2.7.13 (latest at the moment) on local windows computer for testing and aws EC2 windows instance for production (same python version).
The code is: (simplified version)
s = smtplib.SMTP('smtp.host.com', 587)
s.ehlo()
s.starttls()
s.ehlo()
s.login('email#domain.com', 'password');
s.sendmail(from_to, from_to, m.as_string())
s.quit()
Everything works great on local computer, but while running on amazon ec2, I'm getting:
smtplib.SMTPRecipientsRefused: {'email#domain.com': (450, '4.7.1 <MY-INSATNCE-ID.aws-region.compute.internal>: Helo command rejected: Host not found')}
The problem is (was) that EC2 instance has configured local FQDN. It was bound to internal domain name system, therfore remote smtp server was not able to resolve and confirm it.
On the other hand, local computer used unknown FQDN passing only its local ip, which was ok for smtp server. I'm not sure if it is not a security issue.
The problem may be solved by adding local_hostname parameter to SMTP constructor.
Square brackets are obligatory.
s = smtplib.SMTP('smtp.host.com', 587)
to
s = smtplib.SMTP('smtp.host.com', 587, '[127.0.0.1]')
Related
I am trying to send emails with gmail using the smtp module for an amazon price tracker. A few days ago, it was working perfectly but today it came up with this error:
File "C:\Users\61409\AppData\Local\Programs\Python\Python39\lib\smtplib.py", line 755, in starttls
raise SMTPNotSupportedError(
smtplib.SMTPNotSupportedError: STARTTLS extension not supported by server.
This is the code that originally worked but gives the error:
import smtplib
def send_email():
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.ehlo()
server.login('xxxxxxxx#gmail.com', '*password*')
subject = f'The price of your product has fallen'
body = f'Check the amazon link - {URL}'
msg = f"Subject: {subject}\n\n{body}"
server.sendmail(
'xxxxxxx#gmail.com',
'xxxxxxx#gmail.com',
msg
)
print('The email has been sent')
server.quit()
I have tried making another gmail account and using SSL (importing ssl too) but the other gmail account comes up with the same problem and when using SSL, it comes up with a SMTP AUTH problem (unable to solve this too). I have allowed less secure apps but the same problem still occurs. I have also tried removing the first server.ehlo() and I have also tried using the server.set_debuglevel(1) but both do not work. I have also tried using with stmplib.SMTP(*Host*, *Port*) as server: and I have tried just using SMTP without using SSl and TLS at all but both answers do not work. Most of the answers removed the smtplib.SMTPNotSupportedError: STARTTLS extension not supported by server. but other problems occured such as smtplib.SMTPServerDisconnected: Connection unexpectedly closed which happened numerous times with other answers. I tried researching on solutions to the connection unexpectedly closing but none of those answers worked too.
These are the answers I have tried:
SMTPException: STARTTLS extension not supported by server
smtplib.SMTPNotSupportedError: STARTTLS extension not supported by server
Python 2: SMTPServerDisconnected: Connection unexpectedly closed
Python3 SMTP 'Connection unexpectedly closed'
https://www.reddit.com/r/Python/comments/5grxy8/python_smtplib_connection_unexpectedly_closed/daumi2m/
How to send an email with Python?
How can I fix this problem?, am I missing something?? Will I need to use another module or library to send emails??? I apologise if this is a repeat question with a viable answer. I also apologise if one of the answers I mentioned as not working does actual work except I screwed up the code.
smtplib.SMTPNotSupportedError: STARTTLS extension not supported by server.
The SMTP connection to the server does not seem to offer the STARTTLS extension, at least as seen from the perspective of the client. This extension is announced inside the response to the initial EHLO command of the client:
$ telnet smtp.gmail.com 587
...
Connected to smtp.gmail.com.
Escape character is '^]'.
220 smtp.gmail.com ESMTP h9sm19557267wre.24 - gsmtp
EHLO mail.example.com
250-smtp.gmail.com at your service, ...
250-SIZE 35882577
250-8BITMIME
250-STARTTLS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
...
Given that smtp.gmail.com definitely supports this extension it is likely that there is some man in the middle somewhere in path of the connection, which rewrites the traffic to something like this:
220 smtp.gmail.com ESMTP h9sm19557267wre.24 - gsmtp
EHLO mail.example.com
250-smtp.gmail.com at your service, ...
250-SIZE 35882577
250-8BITMIME
250-XXXXXXXA <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
...
Here the response of the server is changed to no longer show support for STARTTLS, in the hope that the client continues without encryption and thus the mail can be analyzed be the man in the middle. Such man in the middle are typically corporate firewalls like Cisco ASA but can also be ISP or local antivirus products.
A few days ago, it was working perfectly but today it came up with this error:
Check if there were any changes regarding local security products (antivirus) or its settings or if there are changes to your environment (corporate firewall, different network you use ...).
I had the same problem: smtplib working properly and then returning this same error, without any change in the server (outlook).
It just so happens that I had refactored my code, and the function that sent the email was deeper in the modules.
If I used the old way, it worked!
When I turned off the anti-virus (AVG in my case), all worked just fine!
So, for some reason, the code refactoring made the sending procedure to be caught by anti-virus.
I have a django application using this function, i'm trying to get the computer name of the ip address accessing my application. I'm doing this by using django-ipware to get the client's ip address, this part is working fine. Then i'm using socket.gethostbyaddr() to get the client's computer name, this works fine on my windows development machine.
def get_comp_name(request):
client_ip = get_client_ip(request)
try:
comp_name = socket.gethostbyaddr(client_ip[0])[0]
except socket.herror:
comp_name = ''
When i tried to deploy to a centOS 7 machine, I get the following error when executing socket.gethostbyaddr() on local network ip addresses.
socket.herror: [Errno 1] Unknown host
I can ping local ip addresses without issue. Am I missing a configuration on my centOS 7 machine?
Your DNS server needs to have an entry for this to work. Check /etc/resolv.conf if the DNS server IP is correct, check if DNS server is reachable from the CentOS node and finally, check if the entry in the DNS server is correct.
I've created a Flask API connecting to an RDS Aurora database using the Postgres engine. The endpoints work fine, and I can connect to them using Postman and cURL. When I try to connect via a unittest script, however, I receive the following error:
fe_sendauth: no password supplied
I'm not sure why it would only occur when tests are being run, does anyone have any insight?
try:
export POSTGRES_USER=" your pc username"
export POSTGRES_PW="your pc password"
It worked in my case.
I had a similar issue,to solve it i had to edit
sudo vi /var/lib/pgsql/data/pg_hba.conf
in METHOD column from MD5 to trust
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
I am trying to send email from a DigitalOcean droplet (Ubuntu 16.04 32 bit) in python3.5 using the local mail/smtp server, primarily to a gmail account. However, currently no errors are displaying from my python script or the server command, but the emails are not showing up in my gmail inbox or spam folder.
I have also followed similar steps from my laptop where possible, with no results.
I want to use the local mail/smtp server to send emails to avoid storing authentication details in the server, and since it will not be receiving emails then I am not as worried about security as if receiving (e.g. not worried about injection attack).
To start off, I run the command;
python3 -m smtpd -n -c PureProxy localhost:1025 &
I run the following script. My understanding is that this should send the email to the local mail/smtp server, which should then handle ensuring it's delivery.
import smtplib
from email.mime.text import MIMEText
msg = MIMEText('test message from droplet body')
msg['Subject'] = 'test message from droplet subject'
msg['From'] = 'droplet#mydomain.com.au'
msg['To'] = 'mygmailusername#gmail.com'
s = smtplib.SMTP('localhost', 1025)
s.set_debuglevel(True)
s.send_message(msg)
s.quit()
With the above method, no error messages occur. However the emails are never received. All methods I have tried so far have had this result.
Using the DebuggingServer instead of PureProxy and the same steps as before provides output similar to below;
---------- MESSAGE FOLLOWS ----------
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Subject: test message from droplet subject
From: droplet#mydomain.com.au
To: mygmailusername#gmail.com
X-Peer: 127.0.0.1
test message from droplet body
------------ END MESSAGE ------------
I have tried the first example on the following page
https://docs.python.org/3/library/email-examples.html
However this fails due to a refused connection error.
I have also tried to check the spamminess of my domain's emails using the following site, however it times out - this indicates to me that it may be a problem with DigitalOcean allowing emails through it's firewall.
https://www.mail-tester.com/
I have checked reputation of my domain using this site - it says it is neutral (using talosintelligence link in comments)
I have tried sending email using the prior instructions to my namecheap private email, but without success.
One possibility I read about is that a PTR DNS record needs to be set, to allow reverse DNS lookup. I am not sure what to do with this though, and I would have to override the default droplet name.
Extra details
I am using a .com.au domain registered with namecheap. I am also using a private email service with namecheap, which required setting multiple DNS records including MX - I added all on the following link, including the optional ones, on my digitalocean account (link in comments)
I also set up the digitalocean nameservers on the namecheap account (link in comments)
I can send and receive email on the private email service.
I have a very basic piece of Python code:
import smtplib
server = smtplib.SMTP(host, port)
problems = server.sendmail(from_addr, to_addr, message)
Is there solution to run it behind an HTTP proxy? I am using Python 3.4.1 on Linux with the http_proxy variable set.
Now I am getting a timeout from SMTP, but if I run this code from a proxy-free network, it works OK.
Is there solution to run it behind an HTTP proxy?
No, HTTP is a different protocol than SMTP and the proxy is for HTTP only. If you are very lucky you might be able to create a tunnel using the CONNECT command to the outside SMTP server, but usually the ports used for CONNECT are restricted so that you will not be able to create a tunnel to an outside host port 25 (i.e. SMTP).