如何使用python将大文件发送到电子邮件?

问题描述

我必须使用python发送大小为75 Mb的文件

  from datetime import datetime
    import smtplib
    from email.mime.multipart import MIMEMultipart
    from email.mime.text import MIMEText
    from email.mime.base import MIMEBase
    from email import encoders
    import os
    class outputdata():
        def SendEmail(self):
            fromaddr = "from mail"
            toaddr = ["to mail 1","to mail 2"]
        # instance of MIMEMultipart
            msg = MIMEMultipart()
    
        # storing the senders email address
            msg['From'] = fromaddr
    
        # storing the receivers email address
            msg['To'] = ','.join(toaddr)
    
        # storing the subject
            msg['Subject'] = "Sys/Org Admin Test Automation Report -  " +datetime.Now().strftime('%d/%m/%Y')
    
        # string to store the body of the mail
            body = "Please find the Sys/Org Admin test automation report attached with this email."
    
        # attach the body with the msg instance
            msg.attach(MIMEText(body,'plain'))
    
        # open the file to be sent log file has 75 Mb

            filename = os.path.abspath('log.html')
            filename1 = os.path.abspath('report.html')
            files = [filename,filename1]
            for file in files:
                    attachment = open(file,"rb")
                    # instance of MIMEBase and named as p
                    p = MIMEBase('application','octet-stream')
    
                    # To change the payload into encoded form
                    p.set_payload((attachment).read())
    
                    # encode into base64
                    encoders.encode_base64(p)
    
                    p.add_header('Content-disposition',"attachment; filename= %s" % ntpath.basename(file))
    
                    # attach the instance 'p' to instance 'msg'
                    msg.attach(p)
            s = smtplib.SMTP('smtp.gmail.com',587)
            s.starttls()
    
                # Authentication
            s.login(fromaddr,"password")
    
                # Converts the Multipart msg into a string
            text = msg.as_string()
    
                # sending the mail
            s.sendmail(fromaddr,toaddr,text)
    
                # terminating the session
            s.quit()
    
    readxmlfile = outputdata()
    readxmlfile.SendEmail()

这是我的错误,因为您的消息超出了Google的消息大小限制。

回溯(最近通话最近): 文件“ C:/Users/Sur​​esh-PC/Desktop/Sys-Admin-Automation/reports/gmailtest.py”,第89行,在 readxmlfile.SendEmail() SendEmail中的文件“ C:/Users/Sur​​esh-PC/Desktop/Sys-Admin-Automation/reports/gmailtest.py”,第83行 s.sendmail(fromaddr,toaddr,文本) sendmail中的文件“ C:\ Users \ Suresh-PC \ AppData \ Local \ Programs \ Python \ python37 \ lib \ smtplib.py”,行867 引发SMTPSenderRefused(代码,resp,from_addr) smtplib.SMTPSenderRefused:(552,b“ 5.2.3您的邮件超出了Google的邮件大小限制。请访问\ n5.2.3

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)