मिश्रित सामग्री के साथ एक ईमेल भेजने के लिए Content-type हेडर को multipart/mixed पर सेट करना आवश्यक है। फिर, टेक्स्ट और अटैचमेंट अनुभागों को boundaries के भीतर निर्दिष्ट किया जा सकता है।
एक बाउंड्री दो हाइफ़न के साथ शुरू होती है जिसके बाद एक अद्वितीय संख्या होती है, जो ईमेल के संदेश भाग में प्रकट नहीं हो सकती। ईमेल के अंतिम खंड को दर्शाने वाली एक अंतिम बाउंड्री भी दो हाइफ़न के साथ समाप्त होनी चाहिए।
ट्रांसमिशन से पहले अटैच की गई फ़ाइलों को pack("m") फ़ंक्शन के साथ एन्कोड किया जाना चाहिए ताकि बेस64 एन्कोडिंग हो।
उदाहरण
निम्नलिखित उदाहरण /tmp/test.txt फ़ाइल को अटैचमेंट के रूप में भेजता है। इसे एक बार आज़माएं -
#!/usr/bin/python import smtplib import base64 filename = "/tmp/test.txt" # फ़ाइल पढ़ें और इसे बेस64 फॉर्मेट में एन्कोड करें fo = open(filename, "rb") filecontent = fo.read() encodedcontent = base64.b64encode(filecontent) # base64 sender = 'webmaster@tutorialpoint.com' reciever = 'amrood.admin@gmail.com' marker = "AUNIQUEMARKER" body =""" This is a test email to send an attachement. """ # मुख्य हेडर परिभाषित करें part1 = """From: From PersonTo: To Person Subject: Sending Attachement MIME-Version: 1.0 Content-Type: multipart/mixed; boundary=%s --%s """ % (marker, marker) # संदेश क्रिया भाग परिभाषित करें part2 = """Content-Type: text/plain Content-Transfer-Encoding:8bit %s --%s """ % (body,marker) # अटैचमेंट अनुभाग परिभाषित करें part3 = """Content-Type: multipart/mixed; name=\"%s\" Content-Transfer-Encoding:base64 Content-Disposition: attachment; filename=%s %s --%s-- """ %(filename, filename, encodedcontent, marker) message = part1 + part2 + part3 try: smtpObj = smtplib.SMTP('localhost') smtpObj.sendmail(sender, reciever, message) print "Successfully sent email" except Exception: print "Error: unable to send email"