You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 = 'sender@testfrom.com',
s_to = 'receiver@testto.com',
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?
The text was updated successfully, but these errors were encountered:
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
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
I am using Python 3 to send mail to the server:
If I remove the
, 'utf-8
parameter in the two calls toMIMEText
, 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?
The text was updated successfully, but these errors were encountered: