Python Forum
SMTPLIB MIMEText HTML - Words being broken - 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: SMTPLIB MIMEText HTML - Words being broken (/thread-9497.html)



SMTPLIB MIMEText HTML - Words being broken - GaryK - Apr-12-2018

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!


RE: SMTPLIB MIMEText HTML - Words being broken - buran - Apr-12-2018

seeing your code and explaining where text comes from will help. HELP A LOT!


RE: SMTPLIB MIMEText HTML - Words being broken - GaryK - Apr-16-2018

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)