Plain text email has new line characters instead of formatting

packs
python
unicode

(Anand Sengar) #1

Greetings,

I am encountering one problem with one of my action in stackstorm where email formatting still has newline characters. It is python runner where I send email in plain-text or html format, depending on input:

def run(self, toEmail, fromEmail, body, subject, isHTML=False, ccEmail=None):

'''

Run method to send email.

'''

smtpObj = smtplib.SMTP('localhost')

if isHTML:

  message = MIMEText(str(body), 'html')

else:

  message = MIMEText(body, "plain")

message['To'] = toEmail

message['From'] = fromEmail

message['Subject'] = subject

message['cC'] = ccEmail

smtpObj.sendmail(fromEmail, [toEmail, ccEmail], message.as_string())

Has anyone seen this issue before in Stackstorm? Any help is appreciated.

Thanks

Anand S.


(Lindsay Hill) #2

How are you calling this method? From somewhere else in the Python action, or is this a standalone action that you’re calling as part of a workflow?

What content are you calling this action with?

If it is a standalone action, what happens if you just call the action directly, bypassing any workflows?

My guess is that you’ve probably got double-escaping going on somewhere. Would need to see the wider context of how you’re using this action.


(Anand Sengar) #3

Hi,

I have tested this action standalone as well to isolate the issue and this is how I test it:

st2 run sre_common.email_notification body=‘This \n is my \n sample \n email test.’ fromEmail=‘ansengar@xxx.com’ subject=‘Provisioning Completed for Order ID : 1521030191420’ toEmail=‘ansengar@xxx.com’ isHTML=‘false’

This is what I get in my inbox:
‘’’
This \n is my \n sample \n email test.
‘’’

Thanks
Anand Sengar


(Anand Sengar) #4

Escaping the unicode encoding and encoding back in utf-8 solved my problem.

Basically adding one line like this:

message._payload= message._payload.decode('unicode_escape').encode('utf-8')

Thanks for all the help and suggestions.
Anand S.


(Lindsay Hill) #5

Great to hear you got it working