Python Forum
Adding a list to Python Emailing Script
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Adding a list to Python Emailing Script
#1
Hi,

I'm having a bit of trouble getting information into an automatic email that I'm having python send for me.

To start I have selenium webscrape this site for job posts then package that into a list format. From there I want to call a function that sends an email to my personal Gmail with the list of web scraped data as the input to that function.

Currently, the output of the list is like this: ['Software Engineer and Technical Writer – Winter/Spring/Summer Intern\nTethers Unlimited, Inc. Connect the Universe', 'Sr. Embedded Software Engineer III\nTethers Unlimited, Inc. Connect the Universe', 'Sr Product Manager – Technical – Software Defined Radios\nTethers Unlimited, Inc. Connect the Universe', 'Internships\nTethers Unlimited, Inc. Connect the Universe']

Ideally, I'd also love some help formatting the list so that when it went into the email function it was separated by a new row at each new title for easier differentiation.

I'll attach the main code and function code below so that people can take a look.

Thanks in advance for any help!

CK
Reply
#2
Main Code:

import time
from selenium import webdriver
from send_email import send_email

# Chromes web driver is installed on your PATH so it can be accessed like this without pathing.
url = "http://www.tethers.com/careers/"
driver = webdriver.Chrome()
driver.get(url)
time.sleep(5)

title_list = driver.find_elements_by_class_name('position')

job_list = []
for title in title_list:
    job_list.append(title.text)

send_email(job_list)
Function code:

def send_email(jobs):
    import smtplib, ssl

    port = 465
    password = input("Type your password and press enter: ")
    sender_email = "[email protected]"
    receiver_email = "[email protected]"


    message = """\
    Subject: Tethers Unlimited Jobs
    
    Here are the new jobs from Tethers Unlimited: {job}"""

    print(message)
    context = ssl.create_default_context()

    with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) as server:
        server.login("[email protected]", password)
        server.sendmail(sender_email, receiver_email, message.format(job=jobs))
buran write Feb-17-2021, 09:37 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#3
def send_email(jobs):
    import smtplib, ssl
 
    port = 465
    password = input("Type your password and press enter: ")
    sender_email = "[email protected]"
    receiver_email = "[email protected]"
 
 
    message = """\
    Subject: Tethers Unlimited Jobs
     
    Here are the new jobs from Tethers Unlimited: {job}"""
 
    print(message)
    context = ssl.create_default_context()
 
    with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) as server:
        server.login("[email protected]", password)
        server.sendmail(sender_email, receiver_email, message.format(job='\n'.join(jobs)))
or even better - use f-strings

def send_email(jobs):
    import smtplib, ssl
 
    port = 465
    password = input("Type your password and press enter: ")
    sender_email = "[email protected]"
    receiver_email = "[email protected]"
 
 
    message = f"""\
    Subject: Tethers Unlimited Jobs
     
    Here are the new jobs from Tethers Unlimited: {'\n'.join(jobs)}"""
 
    print(message)
    context = ssl.create_default_context()
 
    with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) as server:
        server.login("[email protected]", password)
        server.sendmail(sender_email, receiver_email, message)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
(Feb-17-2021, 09:39 AM)buran Wrote:
def send_email(jobs):
    import smtplib, ssl
 
    port = 465
    password = input("Type your password and press enter: ")
    sender_email = "[email protected]"
    receiver_email = "[email protected]"
 
 
    message = """\
    Subject: Tethers Unlimited Jobs
     
    Here are the new jobs from Tethers Unlimited: {job}"""
 
    print(message)
    context = ssl.create_default_context()
 
    with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) as server:
        server.login("[email protected]", password)
        server.sendmail(sender_email, receiver_email, message.format(job='\n'.join(jobs)))
or even better - use f-strings

def send_email(jobs):
    import smtplib, ssl
 
    port = 465
    password = input("Type your password and press enter: ")
    sender_email = "[email protected]"
    receiver_email = "[email protected]"
 
 
    message = f"""\
    Subject: Tethers Unlimited Jobs
     
    Here are the new jobs from Tethers Unlimited: {'\n'.join(jobs)}"""
 
    print(message)
    context = ssl.create_default_context()
 
    with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) as server:
        server.login("[email protected]", password)
        server.sendmail(sender_email, receiver_email, message)

Hi Buran,

Thanks for the help. I tried the f-string method and that worked well. For some reason, the f-string wasn't liking the '\n' within the curly brackets of the message variable, it kept spitting out this error:
Error:
"SyntaxError: f-string expression part cannot include a backslash"
So I just created a linebreak variable and gave it a value of ='\n' that seemed to fix the error.

Everything works great until the function returns I believe. Then I get this error:
Error:
Traceback (most recent call last): File "C:/Users/Cknut/venv/PersonalCrawlers/TethersUnlimited.py", line 17, in <module> send_email(job_list) File "C:\Users\Cknut\venv\PersonalCrawlers\send_email.py", line 20, in send_email server.sendmail(sender_email, receiver_email, message) File "C:\Users\Cknut\AppData\Local\Programs\Python\Python38\lib\smtplib.py", line 859, in sendmail msg = _fix_eols(msg).encode('ascii') UnicodeEncodeError: 'ascii' codec can't encode character '\u2013' in position 134: ordinal not in range(128)
Can you tell me why the below code fixes this issue. I added .encode('utf-8').strip to the end of message and it seems to fix the issue. Is this just because I'm taking out the formatting that is built into this python version? Thanks!

Here is the function currently:
def send_email(jobs):
    import smtplib, ssl

    port = 465
    password = input("Type your password and press enter: ")
    sender_email = "[email protected]"
    receiver_email = "[email protected]"
    linebreak = '\n'

    message = f"""\
    Subject: Tethers Unlimited Jobs
      
    Here are the new jobs from Tethers Unlimited: {linebreak.join(jobs)}"""

    print(message)
    context = ssl.create_default_context()

    with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) as server:
        server.login("[email protected]", password)
        server.sendmail(sender_email, receiver_email, message.encode('utf-8').strip())
buran write Feb-18-2021, 08:58 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have fixed error tags for you.
See BBcode help for more info.
Reply
#5
Your msg contains non-ascii chars.
As stated in the docs for sendmail():
Quote:msg may be a string containing characters in the ASCII range, or a byte string. A string is encoded to bytes using the ascii codec, and lone \r and \n characters are converted to \r\n characters. A byte string is not modified.

When you pass str it try to encode it with ascii codec. If you encode it yourself (with utf-8) you pass byte string and thus no error.

Sorry, about the backslash in the f-string. It slipped from my mind. More clean code would be

jobs = '\n'.join(jobs)
 
message = f"""
Subject: Tethers Unlimited Jobs
       
Here are the new jobs from Tethers Unlimited: {jobs}"""
Also, note that no need of line continuation char. triple quotes are meant to be multi-line.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 3,196 Jun-29-2023, 11:57 AM
Last Post: gologica
Question Need help for a python script to extract information from a list of files lephunghien 6 1,076 Jun-12-2023, 05:40 PM
Last Post: snippsat
  Adding values with reduce() function from the list of tuples kinimod 10 2,637 Jan-24-2023, 08:22 AM
Last Post: perfringo
  Adding a Wake word to my Script Extra 9 3,089 Jan-16-2022, 10:46 AM
Last Post: SheeppOSU
  adding numbers in a list Nickd12 2 2,194 Jan-15-2021, 12:46 PM
Last Post: Serafim
  Adding List Element if Second part of the List Elements are the Same quest_ 3 2,438 Nov-25-2020, 04:33 PM
Last Post: bowlofred
  importing a list of numbers into python script barrypyth 8 4,543 Aug-22-2020, 09:10 PM
Last Post: barrypyth
  How to kill a bash script running as root from a python script? jc_lafleur 4 5,881 Jun-26-2020, 10:50 PM
Last Post: jc_lafleur
  Append only adding the same list again and again viraj1123 4 2,040 Jun-17-2020, 07:26 AM
Last Post: viraj1123
  crontab on RHEL7 not calling python script wrapped in shell script benthomson 1 2,291 May-28-2020, 05:27 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020