![]() |
Adding a list to Python Emailing Script - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Adding a list to Python Emailing Script (/thread-32540.html) |
Adding a list to Python Emailing Script - Cknutson575 - Feb-17-2021 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 RE: Adding a list to Python Emailing Script - Cknutson575 - Feb-17-2021 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 = "ckdevelopment4469@gmail.com" receiver_email = "cknutson575@gmail.com" 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("ckdevelopment4469@gmail.com", password) server.sendmail(sender_email, receiver_email, message.format(job=jobs)) RE: Adding a list to Python Emailing Script - buran - Feb-17-2021 def send_email(jobs): import smtplib, ssl port = 465 password = input("Type your password and press enter: ") sender_email = "ckdevelopment4469@gmail.com" receiver_email = "cknutson575@gmail.com" 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("ckdevelopment4469@gmail.com", 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 = "ckdevelopment4469@gmail.com" receiver_email = "cknutson575@gmail.com" 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("ckdevelopment4469@gmail.com", password) server.sendmail(sender_email, receiver_email, message) RE: Adding a list to Python Emailing Script - Cknutson575 - Feb-18-2021 (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 = "ckdevelopment4469@gmail.com" receiver_email = "cknutson575@gmail.com" 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("ckdevelopment4469@gmail.com", password) server.sendmail(sender_email, receiver_email, message.format(job='\n'.join(jobs)))or even better - use f-strings 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: 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: 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 = "ckdevelopment4469@gmail.com" receiver_email = "cknutson575@gmail.com" 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("ckdevelopment4469@gmail.com", password) server.sendmail(sender_email, receiver_email, message.encode('utf-8').strip()) RE: Adding a list to Python Emailing Script - buran - Feb-18-2021 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. |