1. 程式人生 > >python 發郵件本地傳送沒問題 阿里雲Linix上傳送不出去問題

python 發郵件本地傳送沒問題 阿里雲Linix上傳送不出去問題

 加上  smtplib.SMTP_SSL(mail_host, 465)  #阿里雲把25埠封掉了

附完整程式碼

import pandas as pd import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart import time

def get_current_time():     return str(time.strftime('%Y-%m-%d %H')) current_time = get_current_time() filename='/opt/user/jflm/liusonglei/stockEarlyWarning/data/stockEarlyWarning-20180525.xlsx'  #附件地址 areceiver = '

[email protected]' acc = '' def send_mail(to_list,sub,context,filename):  #to_list:收件人;sub:主題;content:郵件內容     mail_host="smtp.***.com"  #設定伺服器     mail_user="****.com"    #使用者名稱     mail_pass="*****"   #口令     mail_postfix="****.com"  #發件箱的字尾     me="大資料"+"<"+mail_user+"@"+mail_postfix+">"   #這裡的“伺服器”可以任意設定,收到信後,將按照設定顯示     msg = MIMEMultipart() #給定msg型別     msg['Subject'] = sub #郵件主題     msg['From'] = me     msg['To'] = areceiver     msg['Cc'] = acc     msg.attach(context)     #構造附件1     att1 = MIMEText(open(filename, 'rb').read(), 'xls', 'gb2312')     att1["Content-Type"] = 'application/octet-stream'     att1["Content-Disposition"] = 'attachment;filename='+filename[-13:-1]#這裡的filename可以任意寫,寫什麼名字,郵件中顯示什麼名字,filename[-6:]指的是之前附件地址的後6位     msg.attach(att1)     try:         # s = smtplib.SMTP()         s = smtplib.SMTP_SSL(mail_host, 465)  #阿里雲把25埠封掉了         s.connect(mail_host)  #連線smtp伺服器         s.login(mail_user,mail_pass)  #登陸伺服器         s.sendmail(me, areceiver.split(',')+ acc.split(','), msg.as_string())  #傳送郵件         s.close()         return True     except Exception:         return False

if __name__ == '__main__':     # mailto_list=['[email protected]','[email protected]']     data=pd.read_excel(filename, sheet_name=0)     a=pd.DataFrame(data)     b=a.iloc[:,0].size     sub="萬達影票庫存"     d='' #表格內容     for i in range(b):         d=d+"""         <tr>           <td>""" + str(a.index[i]) + """</td>           <td>""" + str(a.iloc[i][0]) + """</td>           <td width="60" align="center">""" + str(a.iloc[i][1]) + """</td>         </tr>"""     html = """\ <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<body> <div id="container"> <p><strong>這是萬達影票"""+current_time+"""時的庫存:</strong></p> <div id="content">  <table width="30%" border="2" bordercolor="black" cellspacing="0" cellpadding="0"> <tr>   <td width="40"><strong></strong></td>   <td width="50"><strong>商品名</strong></td>   <td width="60" align="center"><strong>庫存</strong></td> </tr>"""+d+""" </table> </div> </div> </div> </body> </html>

      """     context = MIMEText(html,_subtype='html',_charset='utf-8')  #解決亂碼     if send_mail(areceiver.split(',')+ acc.split(','),sub,context,filename):         print ("傳送成功")     else:         print( "傳送失敗")