JuliusC
JuliusC

Reputation: 29

WEBScraping TypeError: sendmail() missing 1 required positional argument: 'msg'

I have an error in my code. Is there an issue with connection to gmail? Or there is some other issue with my code? Can you please show me how to fix this problem?

169.9
Garmin Forerunner 735XT GPS Multisport and Running Watch, Black/Grey Traceback (most recent call last):
File "C:\Users\User\source\repos\RCS_WEB_SCRAPER\RCS_WEB_SCRAPER\RCS_WEB_SCRAPER.py", line 52, in check_price()
File "C:\Users\User\source\repos\RCS_WEB_SCRAPER\RCS_WEB_SCRAPER\RCS_WEB_SCRAPER.py", line 29, in check_price send_mail()
File "C:\Users\User\source\repos\RCS_WEB_SCRAPER\RCS_WEB_SCRAPER\RCS_WEB_SCRAPER.py", line 46, in send_mail msg
TypeError: sendmail() missing 1 required positional argument: 'msg'

MY CODE

import requests
from bs4 import BeautifulSoup
import smtplib
import time


URL = 'https://www.amazon.co.uk/Garmin-Forerunner-735XT-Multisport-Running-Black-Grey/dp/B01DWIY39A/ref=sr_1_3?keywords=garmin&qid=1582615813&sr=8-3'

headers = {
    "User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:73.0) Gecko/20100101 Firefox/73.0'}


def check_price():
    page = requests.get(URL, headers=headers)

    soup = BeautifulSoup(page.content, 'html.parser')

    title = soup.find(id ="productTitle").get_text()
    price = soup.find(id="priceblock_dealprice").get_text()
    converted_price = float(price[1:6])

    if(converted_price < 160.00):
        send_mail()

    print(converted_price)
    print(title.strip())

    if(converted_price > 160.00):
        send_mail()

def send_mail():
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.ehlo()
    server.starttls()
    server.ehlo()

    server.login('address', 'mAJnkzjfTqw8xJe')

    subject = 'Price decreased!'
    body = 'Now it is time to buy: https://www.amazon.co.uk/Garmin-Forerunner-735XT-Multisport-Running-Black-Grey/dp/B01DWIY39A/ref=sr_1_3?keywords=garmin&qid=1582615813&sr=8-3'

    msg = f"Subject: {subject}\n\n{body}"

    server.sendmail(
        '[email protected]',
        msg 
    )
    print('E-mail has been sent!')
    server.quit()

while(True):
    check_price()
    time.sleep(28800)

Upvotes: 1

Views: 653

Answers (1)

Chris Doyle
Chris Doyle

Reputation: 12027

Sendmail requires 3 arguments to be passed to it. A from address, a list of to addresses, and a message thats to be sent.

from the documentation https://docs.python.org/3/library/smtplib.html#smtplib.SMTP.sendmail

The required arguments are an RFC 822 from-address string, a list of RFC 822 to-address strings (a bare string will be treated as a list with 1 address), and a message string.

You need to update your code where you call server.sendmail to include a from address and a to address and then your msg.

Upvotes: 2

Related Questions