Question on HTML formatting with set string in message - 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: Question on HTML formatting with set string in message (/thread-32813.html) |
Question on HTML formatting with set string in message - Cknutson575 - Mar-08-2021 Hi guys, I am wondering if anyone can help me format this email so that I just get a bolded: Here are the new jobs from Tethers Unlimited: and then the remaining jobs that populate the email are plain text. I am having a difficult time with the HTML and variable calling because I'm not sure how to manipulate the variable to pull it out of a Set[str] when I can only concatenate strings. Here is the code: def send_email(jobs): import smtplib, ssl from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart port = 465 password = "XXXX" sender_email = "XXXX" receiver_email = "XXXX" linebreak = '\n' message = MIMEMultipart("alternative") message["Subject"] = "Tethers Unlimited Jobs" message["From"] = sender_email message["To"] = receiver_email text = f"""\ Here are the new jobs from Tethers Unlimited:\n{linebreak.join(jobs)}""" html =""" <html> <body> <p><b>Here are the new jobs from Tethers Unlimited:</b><br> """ + {linebreak.join(jobs)} + """ </p> </body> </html> """ part1 = MIMEText(text,"plain") part2 = MIMEText(html,"html") message.attach(part1) message.attach(part2) 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.as_string())Here is the error I get when I run it:
RE: Question on HTML formatting with set string in message - buran - Mar-08-2021 It looks like you are confused between string formatting/f-strings and concatenating strings. You don't need curly braces in {linebreak.join(jobs)} when concatenating
RE: Question on HTML formatting with set string in message - snippsat - Mar-08-2021 So to fix what buran talk about,then it look like this. job = 'Python job' text = f"""\ <p>Here are the new jobs from Tethers Unlimited:<br>{job} <html> <body> <p><b>Here are the new jobs from Tethers Unlimited:</b><br> {job} </p> </body> </html>"""Don't need two part,just use keep all html MIMEText(text, "html") .As see i have add <p> in you first text line.This is what receiver will see. RE: Question on HTML formatting with set string in message - Cknutson575 - Mar-09-2021 Thanks guys! |