Home > Blockchain >  Error SMTP can't send an email from gmail
Error SMTP can't send an email from gmail

Time:12-23

I am currently trying to use smtp to send an email from my gmail to another yahoo email address. I've enabled less secure apps from my Gmail account. I've also went through Gmail Captcha. I also tried to add a port number (587, 2525), but it didn't work. I can't get the code to work. I posted my code here, and after it the error message.

The weird thing is that, this used to work for me few months ago!!

import requests
from bs4 import BeautifulSoup
# import lxml
import smtplib

my_email = "[email protected]"
password = "MY_PASSWORD"

URL = "https://www.amazon.com/Instant-Pot-Plus-60-Programmable/dp/B01NBKTPTS/ref=sr_1_1_sspa?crid=34PO9PKK0CUSX" \
      "&keywords=instant+pot&qid=1639842722&sprefix=instant,aps,76&sr=8-1-spons&spLa=ZW5jcnlwdGVkUXVhbGlmaWV" \
      "yPUEyUlM1TDQ5N0RQS1VXJmVuY3J5cHRlZElkPUEwNzc4NTk5M0c2VVhTVlE1Q1lBTyZlbmNyeXB0ZWRBZElkPUEwMjE0NzMyNUlTVUg0V1" \
      "BYQTZWJndpZGdldE5hbWU9c3BfYXRmJmFjdGlvbj1jbGlja1JlZGlyZWN0JmRvTm90TG9nQ2xpY2s9dHJ1ZQ&th=1 "

TARGET_PRICE = 200

headers = {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36",
    "Accept-Language": "en-GB,en-US;q=0.9,en;q=0.8"
}

response = requests.get(URL, headers=headers)
website_html = response.text

print(response.status_code)

soup = BeautifulSoup(website_html, "lxml")

price = float(soup.find(id="attach-base-product-price").attrs["value"])
title = soup.find(id="productTitle").get_text()


# sending a notification email
if price <= TARGET_PRICE:
    message = f"{title} is now {price}"

    with smtplib.SMTP("smtp.gmail.com") as connection:
        connection.starttls()
        result = connection.login(my_email, password)
        connection.sendmail(
            from_addr=my_email,
            to_addrs="[email protected]",
            msg=f"Subject:Amazon Price Alert!\n\n{message}\n{URL}"
        )

print("done")

ERROR MESSAGE

Traceback (most recent call last):
  File "/Users/Charaf/PycharmProjects/Intermediate /AmazonPriceTracker/main.py", line 45, in <module>
    with smtplib.SMTP("smtp.gmail.com") as connection:
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 255, in __init__
    (code, msg) = self.connect(host, port)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 341, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 312, in _get_socket
    return socket.create_connection((host, port), timeout,
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/socket.py", line 843, in create_connection
    raise err
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/socket.py", line 831, in create_connection
    sock.connect(sa)
TimeoutError: [Errno 60] Operation timed out

CodePudding user response:

Have you tried it with a Google App Password (https://support.google.com/accounts/answer/185833?hl=en)? I just tested it with my amazon price checker which is pretty similar and it works on mine, with a google app password.

This is what I normally use to send a mail though-

    def Send_Mail(email,password,remail): #Function for sending a mail to the user 
    server = smtplib.SMTP('smtp.gmail.com',587)
    server.ehlo()
    server.starttls()
    server.ehlo()
    
    server.login(email,password)
    
    body = f"Price Fell Down\n\nCheck -{url}"
    
    server.sendmail(email,remail,body)

CodePudding user response:

Gmail uses an IMPLICIT TLS connection (SSL) by default.

This means you don't upgrade a Gmail connection with starttls(). Instead you connect to a different port; the ssl port 465.

Remove:

server.starttls()
server.ehlo()

..and keep only one server.ehlo() while of course connecting on port 465.

In the list below, your code should work as is for any service other than Gmail & Rackspace.

  • GMAIL.COM - IMPLICIT TLS connection (SSL)
  • RACKSPACE.COM - IMPLICIT TLS connection (SSL)
  • MAILTRAP.COM - EXPLICIT TLS connection (STARTTLS)
  • iCLOUD.COM - EXPLICIT TLS connection (STARTTLS)
  • OUTLOOK - EXPLICIT TLS connection (STARTTLS)
  • OUTLOOK.COM - EXPLICIT TLS connection (STARTTLS)
  • YAHOO.COM - EXPLICIT TLS connection (STARTTLS)
  • MAIL.COM - EXPLICIT TLS connection (STARTTLS)
  • AOL.COM - EXPLICIT TLS connection (STARTTLS)
  • COMCAST.NET - EXPLICIT TLS connection (STARTTLS)
  • Related