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 = "MY_EMAIL@gmail.com"
password = "MY_PASSWORD"
URL = "https://www.amazon.com/Instant-Pot-Plus-60-Programmable/dp/B01NBKTPTS/ref=sr_1_1_sspa?crid=34PO9PKK0CUSX" \
"&keywords=instant%2Bpot&qid=1639842722&sprefix=instant%2Caps%2C76&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="SOME_EMAIL@yahoo.com",
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