मिश्रित सामग्री वाला ई-मेल भेजने के लिए सामग्री-प्रकार set सेट करना होगा मल्टीपार्ट/मिश्रित . के शीर्ष लेख . फिर, टेक्स्ट और अटैचमेंट अनुभागों को सीमाओं . के भीतर निर्दिष्ट किया जा सकता है ।
एक सीमा दो हाइफ़न के साथ शुरू होती है जिसके बाद एक अद्वितीय संख्या होती है, जो ई-मेल के संदेश भाग में प्रकट नहीं हो सकती है। ई-मेल के अंतिम खंड को दर्शाने वाली एक अंतिम सीमा भी दो हाइफ़न के साथ समाप्त होनी चाहिए।
संलग्न फ़ाइलें pack("m") . के साथ एन्कोड की जानी चाहिए ट्रांसमिशन से पहले बेस 64 एन्कोडिंग के लिए कार्य करता है।
उदाहरण
निम्नलिखित उदाहरण है, जो एक फ़ाइल भेजता है /tmp/test.txt एक अनुलग्नक के रूप में। एक बार कोशिश करें -
#!/usr/bin/python
import smtplib
import base64
filename = "/tmp/test.txt"
# Read a file and encode it into base64 format
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.
"""
# Define the main headers.
part1 = """From: From Person <me@fromdomain.net>
To: To Person <amrood.admin@gmail.com>
Subject: Sending Attachement
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=%s
--%s
""" % (marker, marker)
# Define the message action
part2 = """Content-Type: text/plain
Content-Transfer-Encoding:8bit
%s
--%s
""" % (body,marker)
# Define the attachment section
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"