2015-05-05 9 views
5

पर ऑब्जेक्ट की तरह फ़ाइल संलग्न करें मुझे स्थानीय फाइलों को ईमेल में संलग्न करने के तरीके के बारे में बहुत सारे उदाहरण मिल गए हैं। मैं जो करना चाहता हूं वह एक ईमेल जैसे वस्तु को एक ईमेल संलग्न करता है। तुम क्यों पूछते हो? तो मुझे फ़ाइलों को साफ करने से निपटने की ज़रूरत नहीं है। नीचे मेरा कोड और मेरी त्रुटि है। ज्यादा googling मैं अभी भी यह काम करने के लिए पाने में कामयाब रहे नहीं किया है के बाद, किसी भी मदद बहुत सराहना की जाएगी :)ईमेल को पाइथन 3

def email_sup_teams(team_name, contact_list, file_attachemnt): 
    message_list = [] 
    for jobs in file_attachemnt: 
     for k, v in jobs.items(): 
      message_list.append(v + ',') 
    attachment_text = "\n".join(message_list) 
    print(type(attachment_text)) 

    msg = MIMEText(' Failed jobs list. Please see attachment') 
    msg['Subject'] = 'Not run Jobs for ' + team_name 
    msg['From'] = '[email protected]' 
    msg['To'] = '[email protected]' 

    f = io.StringIO(attachment_text) 
    attachment = MIMEText(f.read()) 
    attachment.add_header('Content-Disposition', 'attachment', filename='test_attach')   
    msg.attach(attachment) 

    s = smtplib.SMTP('smlsmtp') 
    s.sendmail(msg['From'], msg['To'], msg.as_string()) 
    s.quit() 
    print('\n' + team_name + ' Email Sent') 

त्रुटि:

<class 'str'> 
Traceback (most recent call last): 
    File "queue_cleaner_main.py", line 85, in <module> 
    sys.exit(main()) 
    File "queue_cleaner_main.py", line 82, in main 
    queue_cleaner_functions.email_sup_teams(t, team_members_emails, attachment_file_of_jobs) 
    File "D:\oppssup\old_job\queue_cleaner_functions.py", line 179, in email_sup_teams 
    msg.attach(attachment) 
    File "C:\Python34\lib\email\mime\nonmultipart.py", line 22, in attach 
    'Cannot attach additional subparts to non-multipart/*') 
email.errors.MultipartConversionError: Cannot attach additional subparts to non-multipart/* 

उत्तर

5

बाहर कर देता है मैंने पढ़ा जाना चाहिए था

https://docs.python.org/3/library/email-examples.html

अधिक बारीकी से। मुझे यकीन है कि यह मेरा ईमेल बनाने के लिए केवल 1 एमआईएम प्रकार ऑब्जेक्ट का उपयोग कर रहा था लेकिन एकाधिक एमआईएमई ऑब्जेक्ट्स जोड़ने की कोशिश कर रहा था। मूल रूप से इसे काम करने के लिए मैंने नीचे दिए गए कोड का उपयोग किया। खुशी के दिन!

def email_sup_teams(team_name, contact_list, file_attachemnt): 
    message_list = [] 
    for jobs in file_attachemnt: 
     for k, v in jobs.items(): 
      message_list.append(v + ',') 
    attachment_text = "\n".join(message_list) 
    print(type(attachment_text)) 
    # Create the container (outer) email message. 
    msg = MIMEMultipart() 
    #msg = MIMEText(' Failed jobs list. Please see attachment') 
    msg['Subject'] = 'Not run Jobs for ' + team_name 
    msg['From'] = '[email protected]' 
    msg['To'] = '[email protected]' 
    msg.preamble = 'Failed jobs list. Please see attachment' 
    f = io.StringIO(attachment_text) 
    attachment = MIMEText(f.getvalue()) 
    attachment.add_header('Content-Disposition', 'attachment', filename='jobs_not_run.xls')   
    msg.attach(attachment) 

    s = smtplib.SMTP('smlsmtp') 
    s.sendmail(msg['From'], msg['To'], msg.as_string()) 
    s.quit() 
    print('\n' + team_name + ' Email Sent') 
संबंधित मुद्दे