我已尝试使用以下代码发送附件,但文件未发送,仅发送内容。请帮忙。
SERVER = "smtp.example.com"
FROM = "[email protected] /cdn-cgi/l/email-protection"
TO = ["listOfEmails"] # must be a list
enter code here
SUBJECT = "Subject"
TEXT = "Your Text"
# Prepare actual message
message = """From: %s\r\nTo: %s\r\nSubject: %s\r\n\
%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
DNSFile="abc.csv"
# Send the mail
import smtplib
msg = MIMEMultipart()
msg.attach(DNSFile)
server = smtplib.SMTP(SERVER)
server.sendmail(FROM, TO, message)
server.quit()## Heading ##
我使用Python发送附件的代码是:
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email import encoders
## FILE TO SEND AND ITS PATH
filename = 'some_file.csv'
SourcePathName = 'C:/reports/' + filename
msg = MIMEMultipart()
msg['From'] = '[email protected] /cdn-cgi/l/email-protection'
msg['To'] = '[email protected] /cdn-cgi/l/email-protection'
msg['Subject'] = 'Report Update'
body = 'Body of the message goes in here'
msg.attach(MIMEText(body, 'plain'))
## ATTACHMENT PART OF THE CODE IS HERE
attachment = open(SourcePathName, 'rb')
part = MIMEBase('application', "octet-stream")
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= %s" % filename)
msg.attach(part)
server = smtplib.SMTP('smtp.office365.com', 587) ### put your relevant SMTP here
server.ehlo()
server.starttls()
server.ehlo()
server.login('[email protected] /cdn-cgi/l/email-protection', 'password_here') ### if applicable
server.send_message(msg)
server.quit()
希望这对你有用。这对我来说就像一个魅力。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)