发送电子邮件附件 python
Sending email attachment python
I am trying to send an email through python, but I am getting
an unexpected unindent error on msg.addpayload(part). I am getting
the error on the same msg.attach(part) when I copy + paste other
peoples code as well.
def sendemail(logfile, password='somepassword'):
# Initialize email sender/receiver/servers
email_subject = logfile
email_receiver = 'email@gmail.com'
email_sender = 'someemail@gmail.com'
gmail_smtp = 'smtp.gmail.com'
gmail_smtp_port = 587
text_subtype = 'plain'
filepath = os.path.abspath(logfile)
# Create the message
msg = MIMEMultipart()
msg['From'] = email_sender
msg['To'] = email_receiver
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = str(email_subject)
part = MIMEBase('application','octet-stream')
part.set_payload( open(logfile, 'rb').read() )
Encoders.encode_base64(part)
part.add_header('Content-Disposition','attachment',filename=filepath)
# Attach file to message
msg.add_payload(part)
# try:
server_gmail = smtplib.SMTP(gmail_smtp, gmail_smtp_port)
# Identify self to gmail server
server_gmail.ehlo()
# Put SMTP connection in TLS mode and call ehlo again
server_gmail.starttls()
#server_gmail.ehlo()
# Login to service
server_gmail.login(email_sender,password)
# Send email
server_gmail.sendmail(email_sender,email_receiver,msg.as_string())
# Close connection
server_gmail.close()
print("mail sent")
# except:
# print("failed to send mail")
sendemail('logtest.csv', 'somepassword')
您在缩进中混用了制表符和空格。这让 Python.
感到困惑
只能使用其中之一,不能同时使用。最好有空格。
I am trying to send an email through python, but I am getting an unexpected unindent error on msg.addpayload(part). I am getting the error on the same msg.attach(part) when I copy + paste other peoples code as well.
def sendemail(logfile, password='somepassword'):
# Initialize email sender/receiver/servers
email_subject = logfile
email_receiver = 'email@gmail.com'
email_sender = 'someemail@gmail.com'
gmail_smtp = 'smtp.gmail.com'
gmail_smtp_port = 587
text_subtype = 'plain'
filepath = os.path.abspath(logfile)
# Create the message
msg = MIMEMultipart()
msg['From'] = email_sender
msg['To'] = email_receiver
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = str(email_subject)
part = MIMEBase('application','octet-stream')
part.set_payload( open(logfile, 'rb').read() )
Encoders.encode_base64(part)
part.add_header('Content-Disposition','attachment',filename=filepath)
# Attach file to message
msg.add_payload(part)
# try:
server_gmail = smtplib.SMTP(gmail_smtp, gmail_smtp_port)
# Identify self to gmail server
server_gmail.ehlo()
# Put SMTP connection in TLS mode and call ehlo again
server_gmail.starttls()
#server_gmail.ehlo()
# Login to service
server_gmail.login(email_sender,password)
# Send email
server_gmail.sendmail(email_sender,email_receiver,msg.as_string())
# Close connection
server_gmail.close()
print("mail sent")
# except:
# print("failed to send mail")
sendemail('logtest.csv', 'somepassword')
您在缩进中混用了制表符和空格。这让 Python.
感到困惑只能使用其中之一,不能同时使用。最好有空格。