代码一,代码二都可以顺利运行,任选其一即可
代码一:
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
from email.mime.text import MIMEText
from email.header import Header
def send_email_with_attachment(smtp_server, port, sender_email, password, receiver_email, subject, body, file_path):
# setup the email
msg = MIMEMultipart()
msg['From'] = Header(sender_email) # 请注意,如果你的头部不包含非ASCII字符,你可能并不需要使用Header。在大多数情况下,你可以直接将字符串赋值给头部,就像你在你的原始代码中所做的那样。但是,如果你的邮件头部包含非ASCII字符,或者你的邮件服务器要求你使用特定的字符编码,那么使用Header是有帮助的。
msg['To'] = Header(receiver_email,"utf-8")
msg['Subject'] = Header(subject,"utf-8")
msg.attach(MIMEText(body, 'plain'))
# setup the attachment
attachment = open(file_path, 'rb') # open the file in bynary mode
part = MIMEBase('application', 'octet-stream') # create a MIMEBase object
part.set_payload(attachment.read()) # set the payload
encoders.encode_base64(part) # encode the payload in base64
part.add_header('Content-Disposition', 'attachment; filename= {}'.format(file_path)) # add the content disposition
msg.attach(part)
# send the email
server = smtplib.SMTP(smtp_server, port)
server.starttls() # start the server
server.login(sender_email, password) # login
text = msg.as_string() # convert the message to a string
server.sendmail(sender_email, receiver_email, text) # send the email
server.quit() # quit the server
# fill these in with your information
smtp_server = 'smtp.qq.com'
port = 587 # common ports are 587, 465, or 25
sender_email = '[email protected]'
password = 'XXXX'
receiver_email = '[email protected]'
subject = '测试是否能发送邮件'
body = '详见附件'
file_path = './data/XXX.xlsx'
send_email_with_attachment(smtp_server, port, sender_email, password, receiver_email, subject, body, file_path)
代码二:已将附件的文件名设置为文件路径的基础名(不包括目录)。这通常更符合用户的预期。同时,我增加了一个try-except块来捕获可能出现的异常,并输出一个相应的错误消息。还增加了一个文件存在性检查。请根据您的实际需求进行调整。
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
from email.mime.text import MIMEText
from email.header import Header
import os
def send_email_with_attachment(smtp_server, port, sender_email, password, receiver_email, subject, body, file_path):
try:
# setup the email
msg = MIMEMultipart()
msg['From'] = sender_email
msg['To'] = receiver_email
msg['Subject'] = subject
msg.attach(MIMEText(body, 'plain'))
# Check if file exists
if not os.path.exists(file_path):
print(f"File {file_path} not found.")
return
# setup the attachment
with open(file_path, 'rb') as attachment_file:
part = MIMEBase('application', 'octet-stream') # create a MIMEBase object
part.set_payload(attachment_file.read()) # set the payload
encoders.encode_base64(part) # encode the payload in base64
part.add_header('Content-Disposition', f'attachment; filename= {os.path.basename(file_path)}') # add the content disposition
msg.attach(part)
# send the email
server = smtplib.SMTP(smtp_server, port)
server.starttls() # start the server
server.login(sender_email, password) # login
text = msg.as_string() # convert the message to a string
server.sendmail(sender_email, receiver_email, text) # send the email
server.quit() # quit the server
print("Email sent successfully.")
except Exception as e:
print(f"An error occurred: {e}")
# fill these in with your information
smtp_server = 'smtp.qq.com'
port = 587 # common ports are 587, 465, or 25
sender_email = '[email protected]'
password = 'xxxxx' # In a real-world application, never hard-code passwords
receiver_email = '[email protected]'
subject = '测试是否能发送邮件'
body = '详见附件'
file_path = './data/xxxxx.xlsx'
send_email_with_attachment(smtp_server, port, sender_email, password, receiver_email, subject, body, file_path)
邮件中的附件为.bin格式,可以直接转化为excel文件,继续接下来的处理就可以了
import shutil
# Copy and rename the file
shutil.copy('./data/tcmime.1788.2011.2231.bin', './data/tcmime.1788.2011.2231.xlsx')