如何使用smtp模块将符号、数字、文本作为段落发送



正如您在下面看到的,我有一个脚本,它使用smtp模块将邮件发送回usr_mail,函数mail_man采用参数message并通过邮件发送,通过测试,我向脚本传递了一些简单的消息,但我似乎没有发送传递的消息,而是显示this message has no body text

一些消息示例:

"hello world !!! :) "

"you got mail from : " + str(usr_mail)

" @ somthing "

如何使用smtp模块发送带有符号、数字和字母的类似段落的消息

import smtp

def mail_man(message):
handle = smtplib.SMTP('smtp.gmail.com', 587)
handle.starttls()
handle.login(usr_mail , pass_wrd)
handle.sendmail(usr_mail , usr_mail , message)
handle.quit()

print ( " Successfully sent email to :: " +  usr_mail)
return 

if __name__ == "__main__":
print (usr_mail , pass_wrd )
mail_man(message="hello world !!! :) ")

我建议为段落响应创建一个单独的文件,然后从smtplib导入EmailMessage()类,这将允许您将该消息传递到电子邮件。我建议试试这个:

import smtplib
from email.message import EmailMessage
# Open the plain text file.
txtfile = 'name_of_your_file'
with open(txtfile) as f_obj:
# Create a blank message and then add the contents of the
# file to it
msg = EmailMessage()
msg.set_content(f_obj.read())
msg['Subject'] = 'your_subject'
msg['From'] = me
msg['To'] = you
# Send the message through your own SMTP server.
s = smtplib.SMTP('localhost')
s.send_message(msg)
s.quit()

这将允许您发送任何类型的长消息,因为它将文件的内容保存为字符串,然后将其添加到文件中,这样就不会出现转换错误。

最新更新