0
我試圖生成一個輸出併發送相同的電子郵件,使用下面的代碼使用python smtplib模塊,但它沒有正常工作。郵件功能正在工作,但收到一個空文件。可以有人指導我這個代碼有什麼問題蟒蛇smtp附加功能
import boto.iam
import csv
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email import Encoders
# Opening CSV file with write permission
file = open ('report.csv', 'wb')
writer = csv.writer(file)
writer.writerow(['UserName', 'arn', 'password-last-used'])
conn = boto.iam.connection.IAMConnection()
users = conn.get_all_users()['list_users_response']['list_users_result']['users']
for i in users:
writer.writerow([i.user_name, i.arn, i.get("password_last_used", 'Never Used')])
def send_report():
server = smtplib.SMTP("localhost", 25)
SUBJECT = "AWS Report"
EMAIL_FROM = "localhost"
EMAIL_TO = ['[email protected]']
msg = MIMEMultipart()
msg['Subject'] = SUBJECT
msg['From'] = EMAIL_FROM
msg['To'] = ', '.join(EMAIL_TO)
part = MIMEBase('application', "octet-stream")
part.set_payload(open("report.csv", "rb").read())
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="report.csv"')
msg.attach(part)
server.sendmail(EMAIL_FROM, EMAIL_TO, msg.as_string())
send_report()