Python Forum
SMTPLIB MIMEText HTML - Words being broken
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SMTPLIB MIMEText HTML - Words being broken
#1
I have a PYTHON script that sends emails in HTML format and I am seeing words being broken (not hyphenated) / with spaces in them.
The email text is very lengthy (> 2,000 characters) and this may be the root cause, in which case, how do I get around this?

Example Text 1: Once upon a time there was a developer
Email Text 1: Once upon a ti me there was a developer

Example Text 2: Who just couldn't figure out why
Email Text 2: Who ju st couldn't fi gure out why (notice that 2 words are broken)

Another example is where I have <a href="https://google.co.uk">Click Here</a> in the email text and it just so happens that the space is inserted between "<" and "a", to the resultant email has the following displayed:

< a href="https://google.co.uk">Click Here</a>

Instead of displaying Click Here

Any help would be very much appreciated!
Reply
#2
seeing your code and explaining where text comes from will help. HELP A LOT!
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
The main script calls SendEmail, with the EMAIL_BODY being one of the parameters that is passed.
EMAIL_BODY is a string that is created by a backend database table.

Immediately prior to calling SendEmail, the script writes the EMAIL_BODY string to a log file.
When reviewing the log file, all words are as they should be (no broken words), but the resultant email does have words that are broken.

EMAIL_BODY
<br>Before getting started, you may want to find out which IDEs and text editors are tailored to make Python editing easy, browse the list of introductory books, or look at code samples that you might find helpful.<br><br>There is a list of tutorials suitable for experienced programmers on the BeginnersGuide/Tutorials page. There is also a list of resources in other languages which might be useful if English is not your first language.<br><br>The online documentation is your first port of call for definitive information. There is a fairly brief tutorialthat gives you basic information about the language and gets you started. You can follow this by looking at the library reference for a full description of Python's many libraries and the language reference for a complete (though somewhat dry) explanation of Python's syntax. If you are looking for common Python recipes and patterns, you can browse the ActiveState Python Cookbook<br><br>If you want to know whether a particular application, or a library with particular functionality, is available in Python there are a number of possible sources of information. The Python web site provides a Python Package Index (also known as the Cheese Shop, a reference to the Monty Python script of that name). There is also a search page for a number of sources of Python-related information. Failing that, just Google for a phrase including the word ''python'' and you may well get the result you need. If all else fails, ask on the python newsgroup and there's a good chance someone will put you on the right track.<br><br>If you have a question, it's a good idea to try the FAQ, which answers the most commonly asked questions about Python.<br><br>If you want to help to develop Python, take a look at the developer area for further information. Please note that you don't have to be an expert programmer to help. The documentation is just as important as the compiler, and still needs plenty of work!

EMAIL CONTENT
Before getting started, you may want to find out which IDEs and text editors are tailored to make Python editing easy, browse the list of introductory books, or look at code samples that you might find helpful.
There is a list of tutorials suitable for experienced programmers on the Beginners Guide/Tutorials page. There is also a list of resources in other languages which might be useful if English is not your first language.
The online documentation is your first port of call for definitive information. There is a fairly brief tutorial that gives you basic information about the language and gets you started. You can follow this by lookin g at the library reference for a full description of Python's many libraries and the language reference for a complete (though somewhat dry) explanation of Python's syntax. If you are looking for common Python recipes and patterns, you can browse the Active State Python Cookbook
If you want to know whether a particular application, or a library with particular functionality, is available in Python there are a number of possible sources of information. The Python web site provides a Python Package Index (also known as the Cheese Shop, a reference to the Monty Python script of that name). There is also a search page for a number of sources of Python-related information. Failing that, just Google for a phrase including the word ''python'' and you may well get the result you need. If all else fails, ask on the python newsgroup and there's a good chance someone will put you on the right track.
If you have a question, it's a good idea to try the FAQ, which answers the mo st commo nly asked questions about Python.
If you want to help to develop Python, take a look at the developer area for further information. Please note that you don't have to be an expert programmer to help. The documentation is just as important as the compiler, and still needs plenty of work!



def SendEmail(logger, EMAIL_FROM, EMAIL_TO, EMAIL_SUBJECT, EMAIL_BODY, EMAIL_CC):
    try:
		config = configLib.getConfig(emailConfigPath)

		SMTP_HOST = str(config["SMTP_HOST"])
		SMTP_PORT = config["SMTP_PORT"]
		SMTP_AUTH = config["SMTP_AUTH"]
		SMTP_USER = config["SMTP_USER"]
		SMTP_PASSWORD = config["SMTP_PASSWORD"]
		SMTP_SSL = config["SMTP_SSL"]
		
		msgRoot = MIMEMultipart('related')
		msgRoot.preamble = 'This is a multi-part message in MIME format.'
		
		msgAlternative = MIMEMultipart('alternative')
		msgRoot.attach(msgAlternative)
		msgText_ALT = MIMEText('This is the alternative plain text message.')
		msgAlternative.attach(msgText_ALT)
	
		msgText = MIMEText('<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>PYTHON MAIL</title></head><body style="background-color: #eeeeee; margin: 0; padding: 0;"><TABLE ALIGN="center" style="background-color: #ffffff; padding: 20px; margin-bottom: 1em; width: 620px;"><TR><TD><img src="cid:image1"><br>' + EMAIL_BODY + '</TD></TR></TABLE>', 'html')
		
		msgAlternative.attach(msgText)
		
		fp = open('/root/Image/EmailBanner_Image.jpg', 'rb')
		msgImage = MIMEImage(fp.read())
		fp.close()
		
		msgImage.add_header('Content-ID', '<image1>')
		msgRoot.attach(msgImage)

		msgRoot['Subject'] = EMAIL_SUBJECT
		msgRoot['From'] = EMAIL_FROM
		msgRoot['To'] = EMAIL_TO
		toAddrs = [EMAIL_TO]
		if (len(EMAIL_CC) > 0) :
			msgRoot['Cc'] = EMAIL_CC
			toAddrs = [EMAIL_TO] + [EMAIL_CC]
			
		conn = SMTP(host=SMTP_HOST, port=587)
		logger.info( "Connected to SMTP Host ")
		conn.set_debuglevel(False)
		conn.ehlo()
		conn.starttls()
		logger.info( "started tls ")
		try:
			logger.info( "Sending email to " + EMAIL_TO)
			conn.sendmail(EMAIL_FROM, toAddrs, msgRoot.as_string())
			logger.info("******email.SendEmail() Success******")

		finally:
			conn.quit()

    except Exception, e:
		logger.error("******email.SendEmail() Failed******")
		logger.error(str(e))
		raise Exception (e)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Send email with smtp without using Mimetext mgallotti 0 677 Feb-01-2023, 04:43 AM
Last Post: mgallotti
  Tkinterweb (Browser Module) Appending/Adding Additional HTML to a HTML Table Row AaronCatolico1 0 876 Dec-25-2022, 06:28 PM
Last Post: AaronCatolico1
  Why is copying and pasting a block now broken? WagmoreBarkless 2 1,328 May-05-2022, 05:01 AM
Last Post: WagmoreBarkless
  Why is copying and pasting a block now broken? WagmoreBarkless 1 1,197 May-04-2022, 11:40 PM
Last Post: Larz60+
  Sending Attachments via smtplib [SOLVED] AlphaInc 3 2,107 Oct-28-2021, 12:39 PM
Last Post: AlphaInc
  Sending random images via smtplib [SOLVED] AlphaInc 0 1,661 Oct-19-2021, 10:10 AM
Last Post: AlphaInc
  [UnicodeEncodeError from smtplib] yoohooos 0 3,344 Sep-25-2021, 04:27 AM
Last Post: yoohooos
  Generate a string of words for multiple lists of words in txt files in order. AnicraftPlayz 2 2,756 Aug-11-2021, 03:45 PM
Last Post: jamesaarr
  reading html and edit chekcbox to html jacklee26 5 3,020 Jul-01-2021, 10:31 AM
Last Post: snippsat
  BrokenPipeError: [Errno 32] Broken pipe throwaway34 6 8,937 May-06-2021, 05:39 AM
Last Post: throwaway34

Forum Jump:

User Panel Messages

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