Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot get utf-8 text mail to show correctly #137

Open
Orionsg opened this issue Aug 5, 2020 · 0 comments
Open

Cannot get utf-8 text mail to show correctly #137

Orionsg opened this issue Aug 5, 2020 · 0 comments

Comments

@Orionsg
Copy link

Orionsg commented Aug 5, 2020

I am using Python 3 to send mail to the server:

import	traceback
import	smtplib
from	email.mime.text		import	MIMEText
from	email.mime.multipart 	import	MIMEMultipart

def sendmail(s_from : str, s_to : str, s_srv : str, s_txt='', s_html='', s_subj='', i_port=2525, s_login='', s_pw=''):

	if not (s_from and '@' in s_from):
		raise ValueError(f'No or invalid email receiver: {s_from}')
	if not (s_to and '@' in s_to):
		raise ValueError(f'No or invalid email sender: {s_to}')
	if not s_srv:
		raise ValueError('SMTP server not specified')
	if not (s_subj or s_txt or s_html):
		raise ValueError('Either subject, text or html must be specified')
	if s_login and not s_pw or s_pw and not s_login:
		raise ValueError('Login and password must either both be specified or not at all')

	msg 			=	MIMEMultipart('alternative')
	msg['Subject']		=	s_subj
	msg['From']		=	s_from
	msg['To']		=	s_to

	if s_txt:
		mime_txt	=	MIMEText(s_txt, 'plain', 'utf-8')
		msg.attach(mime_txt)
	if s_html:
		mime_html	=	MIMEText(s_html, 'html', 'utf-8')
		msg.attach(mime_html)

	smtp_srv		=	smtplib.SMTP(s_srv, i_port)
	if s_login:
		smtp_srv.login(s_login, s_pw)
	
	smtp_srv.sendmail(s_from, s_to, msg.as_string() )

def main():

	try:
		sendmail(
			s_from	=	'[email protected]',
			s_to	=	'[email protected]',
			s_srv	=	'localhost',
			s_txt	=	'Pure text body',
			s_html	=	'<h3>HTML Header body</h3><b>Normal body text</b>',
			s_subj	=	'This is subject text'
			)

	except:
		li_s_err = str(traceback.format_exc()).split('\n')
		for s_err in li_s_err:
			print(s_err)
			
if __name__ == '__main__':
	main()

If I remove the , 'utf-8 parameter in the two calls to MIMEText, the body of the email is displayed correctly.
Otherwise, it is displayed in mailslurper as PGgzPkhUTUwgSGVhZGVyIGJvZHk8L2gzPjxiPk5vcm1hbCBib2R5IHRleHQ8L2I+

Am I using the email.mime or smtplib modules wrongly, or is there a problem with mailslurper?

@Orionsg Orionsg changed the title Cannot get utf-8 text and html part of mail to show correctly in mail Cannot get utf-8 text and html part of mail to show correctly Aug 5, 2020
@Orionsg Orionsg changed the title Cannot get utf-8 text and html part of mail to show correctly Cannot get utf-8 text mail to show correctly Aug 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant