0

Sorry for the poor title, I'm currently experiencing a weird problem with my code. When I send an email using my app, some words has weird spacing in between them.

For example: "Feel free to use our stor age for xx days"

The spacing usually happens on different words depending on the recipients name. I'm guessing the length of the name of the recipient has a factor in this as the email begins with a greeting (e.g Hi John Doe,)

The one I just built is using smptlib library from Python and I'm currently storing the templates in MySQL.

Python code basically looks like this:

server = smtplib.SMTP(mailServer)
msgContent = rawHTMLFromDb.replace('{fullname}', name)

message = MIMEText(msgContent, 'html')
message['From'] = sendFrom
message['To'] = sendTo
message['Subject'] = subject

msg_full = message.as_string()

server.sendmail(sendFrom, sendTo, msg_full)

I'm not sure if there's any bearing but I'm pulling the templates from MySQL that's stored in a LONGTEXT datatype column.

I also already did a print right after the substitution and confirmed that substitution and weird spacing is not present. It's just viewable on the mail client.

Edit: Some more details on the msgContent

It looks like this:

<!DOCTYPE html><html><head><style>..longCSS</style></head><body><p>Hi {fullname}</p><table>Several other content here.</table></body></html>
Dominic
  • 928
  • 3
  • 9
  • 19
  • I think that for us to help you you should get one of the problematic `msgContent ` and just do `msgContent = 'problematic template'` at the beginning and specify the weird result and what you expected. –  Aug 30 '17 at 02:02
  • Hi @gsi-frank, the only problem is that it's too long if I were to put it in as is. It's just an html template with – Dominic Aug 30 '17 at 02:10
  • 1
    I understand, so my recommendation is to start stripping the content of one of the problematic template to the minimum without making the problem disappear and put that no so long content here. Without the contest is very hard that someone will be able to help you. –  Aug 30 '17 at 02:15
  • Thanks for the suggestion gsi_frank, I added a stripped down version and replaced the .format code into .replace as that's what I'm doing in my code. – Dominic Aug 30 '17 at 02:22

2 Answers2

0

The CSS is justifying the text - forcing the text to align with the edges of the container - and this is causing the unusual internal spacing. See the snippet below for a demonstration.

p {
  text-align: justify;
  width: 275px
}
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse porttitor, metus nec tempus lobortis, magna ipsum viverra odio, nec aliquet velit eros sed erat. Etiam non gravida lacus. Morbi a sem convallis lacus elementum ultricies.</p>
snakecharmerb
  • 47,570
  • 11
  • 100
  • 153
0

I still don't know what's happening but I was initially just copy and pasting my html codes directly to MySQL Workbench when this was happening.

When I made a script that inserts the file's content directly, the problem doesn't show up. So I'm guessing that copy pasting my content directly inserts some weird formatting to the code.

Here's the script I made:

import pymysql
from database import Database

db = Database()
conn = db.get_conn()

templatePath = r'<projectPath>\index.html'

tmp = (open(templatePath, mode='r'))

with conn.cursor() as cur:
    updateScript = "UPDATE email_templates SET html = %s WHERE id = 7"

    cur.execute(updateScript, (tmp.read()))
    cur.close()
    conn.commit()
    print('Template update success')
Dominic
  • 928
  • 3
  • 9
  • 19