未加星标

Add the sender's name to the email field in python

字体大小 | |
[开发(python) 所属分类 开发(python) | 发布者 店小二03 | 时间 2019 | 作者 红领巾 ] 0人收藏点击收藏

I am trying to send email with below code.

import smtplib from email.mime.text import MIMEText sender = '<a href="/cdn-cgi/l/email-protection" data-cfemail="bdced8d3d9d8cffdced8d3d9d8cf93ded2d0">[email protected]</a>' def mail_me(cont, receiver): msg = MIMEText(cont, 'html') recipients = ",".join(receiver) msg['Subject'] = 'Test-email' msg['From'] = "XYZ ABC" msg['To'] = recipients # Send the message via our own SMTP server. try: s = smtplib.SMTP('localhost') s.sendmail(sender, receiver, msg.as_string()) print "Successfully sent email" except SMTPException: print "Error: unable to send email" finally: s.quit() cont = """\ <html> <head></head> <body> <p>Hi!<br> How are you?<br> Here is the <a href="http://www.google.com">link</a> you wanted. </p> </body> </html> """ mail_me(cont,['<a href="/cdn-cgi/l/email-protection" data-cfemail="09717073497170736a6664">[email protected]</a>']) I want "XYZ ABC" to appear as the sender's name when the email is received and its email address as '[emailprotected]'. but when i receive email i am receiving weird details in "from" fields of the email message. [![from: <a href="/cdn-cgi/l/email-protection" data-cfemail="144c4d4e54">[email protected]</a><machine-hostname-appearing-here> reply-to: <a href="/cdn-cgi/l/email-protection" data-cfemail="da8283809a">[email protected]</a><machine-hostname-appearing-here>, <a href="/cdn-cgi/l/email-protection" data-cfemail="b8f9fafbf8">[email protected]</a><machine-hostname-appearing-here>][1]][1]

I have attached a screenshot of the email that i receive.

how can i fix this according to my need.

import smtplib from email.mime.text import MIMEText def send_email(to=['<a href="/cdn-cgi/l/email-protection" data-cfemail="63061b020e130f0623061b020e130f064d000c0e">[email protected]</a>'], f_host='example.example.com', f_port=587, f_user='<a href="/cdn-cgi/l/email-protection" data-cfemail="6c09140d011c00092c09140d011c0009420f0301">[email protected]</a>', f_passwd='example-pass', subject='default subject', message='content message'): smtpserver = smtplib.SMTP(f_host, f_port) smtpserver.ehlo() smtpserver.starttls() smtpserver.ehlo smtpserver.login(f_user, f_passwd) # from email credential msg = MIMEText(message, 'html') msg['Subject'] = 'My custom Subject' msg['From'] = "Admin" msg['To'] = ','.join(to) for t in to: smtpserver.sendmail(f_user, t, msg.as_string()) # you just need to add this in for loop in your code. smtpserver.close() print('Mail is sent successfully!!') cont = """\ <html> <head></head> <body> <p>Hi!<br> How are you?<br> Here is the <a href="http://www.google.com">link</a> you wanted. </p> </body> </html> """ try: send_email(message=cont) except: print('Mail could not be sent')

above method I have tried to send mail which is worked for me even I am able to send mail to my gmail account(in spam folder). Let me know if you face any other related problem.

本文开发(python)相关术语:python基础教程 python多线程 web开发工程师 软件开发工程师 软件开发流程

代码区博客精选文章
分页:12
转载请注明
本文标题:Add the sender's name to the email field in python
本站链接:https://www.codesec.net/view/627729.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(python) | 评论(0) | 阅读(202)