How do I create a smtplib code that sends an 'Item in stock' email to a specific email address?

Issue

Using selenium I have a code that determines if an item is ‘in stock’ or ‘out of stock’. The code can detect once an ‘out of stock’ item becomes ‘in stock’. Using smtplib, I’d like an email to be sent from the following email address:

example1@hotmail.com
password: 1234

to:

example2@hotmail.com

notifying the individual once an item is ‘in stock’.

What would a complete python code for this look like?.

Solution

You can try something like :

  1. For Sending Plain Text Emails

    import smtplib, ssl
    from email.mime.text import MIMEText
    
    port = 587  # For starttls
    smtp_server = "smtp.office365.com"
    
    sender_email = "the_sender_email_address"            # example1@hotmail.om
    password = "the_sender_email_address_password"       # 1234
    
    
    receiver_email = "the_receiver_email_address"        # example2@hotmail.com
    
    message = MIMEText("Hurry up.. Item in Stock Again") # Your message
    message['Subject'] = "Item in Stock Again"           # Email Subject 
    message['From'] = sender_email
    message['To'] = receiver_email
    
    
    context = ssl.create_default_context()
    with smtplib.SMTP(smtp_server, port) as server:
        server.ehlo()  # Can be omitted
        server.starttls(context=context)
        server.ehlo()  # Can be omitted
        server.login(sender_email, password)
        server.sendmail(sender_email, receiver_email, message.as_string())
    
  2. Including Some HTML Content

     import smtplib, ssl
     from email.mime.text import MIMEText
     from email.mime.multipart import MIMEMultipart
    
    
     port = 587  # For starttls
     smtp_server = "smtp.office365.com"
    
     sender_email = "sender_email"
     password = "sender_email_password"
    
     receiver_email = "receiver_email"
    
    
     message = MIMEMultipart("alternative")
     message["Subject"] = "multipart test"
     message["From"] = sender_email
     message["To"] = receiver_email
    
     # Create the plain-text and HTML version of your message
     text = """\
     Hurry Up"""
     html = """\
     <html>
         <body>
           <p>
              <a href="http://www.realpython.com">Item</a> 
                  is in stock again.
           </p>
          </body>
     </html>
     """
    
     # Turn these into plain/html MIMEText objects
     part1 = MIMEText(text, "plain")
     part2 = MIMEText(html, "html")
    
     # Add HTML/plain-text parts to MIMEMultipart message
     # The email client will try to render the last part first
     message.attach(part1)
     message.attach(part2)
    
     context = ssl.create_default_context()
     with smtplib.SMTP(smtp_server, port) as server:
        server.starttls(context=context)
        server.login(sender_email, password)
        server.sendmail(sender_email, receiver_email, message.as_string())
    
  3. Adding Attachments Using the email Module

    import email, smtplib, ssl
    
    from email import encoders
    from email.mime.base import MIMEBase
    from email.mime.multipart import MIMEMultipart
    from email.mime.text import MIMEText
    
    
    port = 587  # For starttls
    smtp_server = "smtp.office365.com"
    
    sender_email = "sender_email_address"
    password = "sender_email_password"
    
    receiver_email = "receiver_email_address"
    
    
    
    subject = "Item in Stock "
    body = "email with attachment"
    
    # Create a multipart message and set headers
    message = MIMEMultipart()
    message["From"] = sender_email
    message["To"] = receiver_email
    message["Subject"] = subject
    message["Bcc"] = receiver_email  # Recommended for bul mails
    
    # Add body to email
    message.attach(MIMEText(body, "plain"))
    
    filename = "some_document.pdf"        # In same directory as the script
    
    # Open PDF file in binary mode
    with open(filename, "rb") as attachment:
        # Add file as application/octet-stream
        # Email client can usually download this automatically as attachment
        part = MIMEBase("application", "octet-stream")
        part.set_payload(attachment.read())
    
    # Encode file in ASCII characters to send by email    
    encoders.encode_base64(part)
    
    # Add header as key/value pair to attachment part
    part.add_header(
        "Content-Disposition",
        f"attachment; filename= {filename}",)
    
    # Add attachment to message and convert message to string
    message.attach(part)
    text = message.as_string()
    
    # Log in to server using secure context and send email
    context = ssl.create_default_context()
    with smtplib.SMTP(smtp_server, port) as server:
        server.starttls(context=context)
        server.login(sender_email, password)
        server.sendmail(sender_email, receiver_email, text)
    

NOTE : While the code examples above will work, keep in mind it’s old code and isn’t the best code you can write to send emails using python smtplib, email.
You should be using the new EmailMessage/EmailPolicy API (introduced in python 3.6).
You can find more examples here on python docs.

Answered By – WaLid LamRaoui

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published