Python Forum
Question on HTML formatting with set string in message
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Question on HTML formatting with set string in message
#1
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:

Error:
C:\Python34\venv\Scripts\python.exe C:/Users/Cknut/venv/PersonalCrawlers/TethersUnlimited.py Traceback (most recent call last): File "C:/Users/Cknut/venv/PersonalCrawlers/TethersUnlimited.py", line 22, in <module> send_email(job_list_split) File "C:\Users\Cknut\venv\PersonalCrawlers\send_email.py", line 19, in send_email html =""" TypeError: can only concatenate str (not "set") to str Process finished with exit code 1
buran write Mar-08-2021, 08:59 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
#2
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
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
#3
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.
Reply
#4
Thanks guys!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  reading html and edit chekcbox to html jacklee26 5 713 Jul-01-2021, 10:31 AM
Last Post: snippsat
  Question about change hex string to integer sting in the list (python 2.7) lzfneu 1 644 May-24-2021, 08:48 AM
Last Post: bowlofred
  f-string error message not understood Skaperen 4 824 Mar-16-2021, 07:59 PM
Last Post: Skaperen
  Outlook Emails & HTML Table in Message Body JoeDainton123 1 4,213 Sep-02-2020, 05:15 AM
Last Post: buran
  question: finding multiple strings within string djf123 4 1,120 May-16-2020, 01:00 PM
Last Post: snippsat
  smtplib: string formatting not carrying over to email ClassicalSoul 1 917 Apr-22-2020, 09:58 PM
Last Post: bowlofred
  Simple newb string question Involute 2 1,072 Sep-08-2019, 12:50 AM
Last Post: Involute
  HTML to Python to Windows .bat and back to HTML perfectservice33 0 1,025 Aug-22-2019, 06:31 AM
Last Post: perfectservice33
  How to Find & Count String Patterns Between two Markers in a HTML file ahmedwaqas92 3 1,257 Aug-19-2019, 10:12 AM
Last Post: ahmedwaqas92
  String formatting difficulties mmk1995 3 1,257 Aug-09-2019, 11:18 AM
Last Post: wavic

Forum Jump:

User Panel Messages

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