[Python] Email sending, realize running script user input the file to be sent, the input is completed and the sending is successful

Tip: After the article is written, the table of contents can be automatically generated. How to generate it can refer to the help document on the right


foreword

提示:这里可以添加本文要记录的大概内容:

This article mainly introduces how to write a Python script to send emails with attachments. When running the script, the user will be asked to enter the file to be sent by email. After the user completes the input, click Enter, and the email will be sent successfully.


提示:以下是本篇文章正文内容,下面案例可供参考

1. Download smtplib

From the command line, use the command pip install smtplib to download the module.

pip install smtplib

2. Use steps

1. Import library

The code is as follows (example):

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication

2. Mail sending information, mail receiving information setting, and setting input at runtime

The code is as follows (example):

emailitem = input('Please input the message you want to send:')
mailsend_host = 'smtp.qq.com'
#邮件的使用者账号
mailsend_user = '********'
#密码(授权码),授权码登陆QQ邮箱获取
mailsend_passwd = '***********'
#邮件发送者的邮箱地址
mail_sender = '************@qq.com'
#邮件接受者的邮箱地址,可以使用列表,可以发送到多个邮箱地址
mail_receivers = ['**********@qq.com']

3. Set the content of the sent email

The code is as follows (example):

sendmessage = MIMEMultipart()

4. Set the email sender and receiver information

The code is as follows (example):

#邮件主题
message['Subject'] = 'Your email from Test'
#发送方信息
message['From'] = mail_sender
#接受方信息
message['To'] = mail_receivers[0]

5. Add attachment content

The code is as follows (example):

filesendname = emailitem
with open(filesendname, 'rb') as filesend:
    attachment = MIMEApplication(filesend.read(), _subtype='txt')
    attachment.add_header("Content-Disposition", "attachment", filename=filesendname)
    message.attach(attachment)

6. Login to send email

The code is as follows (example):

#登录并发送邮件
try:
    mailsend = smtplib.SMTP()
    #连接到邮箱服务器
    mailsend.connect(mailsend_host,25)
    #邮箱登录
    mailsend.login(mailsend_user,mailsend_passwd)
    #发送邮件
    mailsend.sendmail(mail_sender,mail_receivers,message.as_string())
    #退出
    mailsend.quit()
    print('mail send success')
exceptprint('mail send error')

7. Realize the effect

Script running effect

Summarize

This article mainly introduces the realization of sending emails with attachments through python. When running the script in use, the user will be asked to input the file to be sent by email. After the user completes the input, click Enter and the email will be sent successfully. This article is mainly used to record the specific implementation method of using python to realize the email sending function. It refers to some content on the Internet and is only used for learning records. If there is any infringement, please contact to delete it.

Guess you like

Origin blog.csdn.net/liaotianyin/article/details/130403464