Python 3.4 send mail, pythonsend
# Coding = UTF-8 # Python 3.4 https://docs.python.org/3.4/library/#IDE:Visual Studio 2015 Window10import atexitimport osimport unicodedataimport sysimport timeimport unicodedataimport extends codeimport codecsimport mathimport csvimport base64import reimport smtplib from email. mime. text import MIMEText from email. mime. multipart import MIMEMultipartfrom email. header import Header from email. mime. I Mage import MIMEImagefrom email. mime. base import MIMEBase from email. utils import COMMASPACE, formatdate import emailimport OS. path import sys import mimetypes import configparser import string # parser # config = ConfigParser. configParser () # config. read (inifile) # OS. remove (inifile) # Remove file subject = Header (" Sales Report", "UTF-8 ") # mail title ReplyToName = "geovindu@dusystem.com" ReplyToMail = "geovindu@dusystem.com" To = "463588883@qq.com" bcc = "geovindu@163.com"; cc = "geovindu@jw28.com"; COMMASPACE = ','; file_name = inifile # "file_name" From = "% s <geovindu@dusystem.com>" % Header ("", "UTF-8 ") # sender and name server = smtplib. SMTP ("mail.dusystem.com", 25) server. login ("geovindu@dusystem.com", "888") # only when the smtp server needs verification # construct the MIMEMultipart object as the root container main_msg = MIMEMultipart ("alternative"); # alternative related 2. email of Version x. MIMEMultipart. MIMEMultipart () # construct a MIMEText object as the mail display content and append it to the root container text_msg = MIMEText ("xxx emails you forwarded", _ subtype = 'html ', _ charset = "UTF-8") # email content 2. email of Version x. MIMEText. MIMETextmain_msg.attach (text_msg) # construct the MIMEBase object as the file attachment content and append it to the root container # The image is not displayed, the attachment is different # fp = open (file_name, 'rb') # msgImage = MIMEImage (Fp. read () # fp. close () # msgImage. add_header ('content-id', '<image1>') # main_msg.attach (msgImage) # Set the attachment header # basename = OS. path. basename (file_name) # file_msg.add_header ('content-disposition', 'attachment', filename = basename) # modify the mail header file_msg = MIMEText (open (file_name, 'rb '). read (), 'base64', 'utf-8 ') # file file_msg ["Content-Type"] = 'application/octet-stream' file_msg ["Content-Disposition"] = 'attachm Ent; filename = "055.JPG" 'main_msg.attach (file_msg) # Set the root container attribute main_msg ['from'] = From if ReplyToMail! = 'None': main_msg ['reply-to '] = "% s <% s>" % (Header (ReplyToName, "UTF-8"), ReplyToMail) main_msg ['to'] = To; main_msg ['subobject'] = Subject; main_msg ['cc'] = Cc; main_msg ['bcc '] = Bcc; # This is invalid. main_msg ['date'] = formatdate (localtime = True) # The formatted full text fullText = main_msg.as_string () # Use smtp to send a try: server. sendmail (From,. split (';'), fullText) finally: server. quit () # OS. remove (file_name) print ("sent successfully ");
Send Images
Is a file
# Coding = UTF-8 # Python 3.4 https://docs.python.org/3.4/library/#IDE:Visual Studio 2015 Window10import atexit # class library import osimport unicodedataimport sysimport timeimport unicodedataimport extends codeimport codecsimport mathimport csvimport base64import reimport smtplib from email. mime. text import MIMEText from email. mime. multipart import MIMEMultipartfrom email. header import Header from email. mi Me. image import MIMEImagefrom email. mime. base import MIMEBase from email. encoders import encode_base64from email. mime. application import MIMEApplication from email. utils import COMMASPACE, formatdate import emailimport OS. path import sys import mimetypes import configparser import string import mimetypesimport base64; # https://docs.python.org/3.4/library/email.html#https://docs.python.org/3/library /Email-examples.htmlinifile = u 'f:/Python/ 文..doc '# config = ConfigParser. configParser () # config. read (inifile) # OS. remove (inifile) # remove the file subject = Header (" Sales Report", "UTF-8 ") # mail title ReplyToName = "geovindu@dusystem.com" ReplyToMail = "geovindu@dusystem.com" To = "463588883@qq.com" bcc = "geovindu@163.com"; cc = "geovindu@jw28.com"; COMMASPACE = ','; file_name = inifile # "file_name" From = "% s <geovindu @ dusystem. Com> "% Header (" "," UTF-8 ") # sender and name server = smtplib. SMTP ("mail.dusystem.com", 25) server. login ("geovindu@dusystem.com", "888") # only when the smtp server needs verification # construct the MIMEMultipart object as the root container main_msg = MIMEMultipart ("alternative"); # alternative related 2. email of Version x. MIMEMultipart. MIMEMultipart () # construct the MIMEText object as the mail display content and append it to the root container text_msg = MIMEText ("please read the document and make suggestions", _ subtype = 'html ', _ charset = "UTF-8") # email content 2. email of Version x. MI METext. MIMETextmain_msg.attach (text_msg) # construct the MIMEBase object as the file attachment content and attach it to the root container # The image is not displayed, and the attachment is different # fp = open (file_name, 'rb ') # msgImage = MIMEImage (fp. read () # fp. close () # msgImage. add_header ('content-id', '<image1>') # main_msg.attach (msgImage) # Set the attachment header # basename = OS. path. basename (file_name) # file_msg.add_header ('content-disposition', 'attachment', filename = basename) # modify the email header # image # file_image = "F:/Python/055.jpg"; # fi Le_msg = MIMEText (open (file_image, 'rb '). read (), 'base64', 'utf-8 ') # The file is missing. # file_msg ["Content-Type"] = 'application/octet-stream' # The image (application/octet-stream) can be used, but the WORD does not work # file_msg ["Content-Disposition"] = 'attachment; filename = "055.jpg" '#1 the file name can be garbled # contentType, encoding = mimetypes. guess_type (file_name) # mainType, subType = contentType. split ('/', 1) # file = open (file_name, 'rb') # attachment = MIMEB Ase (mainType, subType) # attachment. set_payload (file. read () # encode_base64 (attachment) # file. close () # basename = OS. path. basename (file_name); # attachment. add_header ('content-disposition', 'attachment ', filename = basename) # attachment. add_header ('content-disposition', 'attachment', filename = OS. path. basename (file_name) # file_name #2 file name garbled # file_msg = MIMEApplication (open (file_name, 'rb '). read () # File Sending # fi Le_msg.add_header ('content-disposition', 'attachment', filename = "application writer .doc") # main_msg.attach (file_msg) # file_msg #3 Note: Specify the att encoding basename = OS. path. basename (file_name) att = MIMEText (open (file_name, 'rb '). read (), 'base64', 'utf-8 ') att ["Content-Type"] = 'application/octet-stream' att ["Content-Disposition"] = 'attachment; filename = % s' % basename. encode ("UTF-8") main_msg.attach (att) # Set the root container attribute main_msg ['From'] = From if ReplyToMail! = 'None': main_msg ['reply-to '] = "% s <% s>" % (Header (ReplyToName, "UTF-8"), ReplyToMail) main_msg ['to'] = To; main_msg ['subobject'] = Subject; main_msg ['cc'] = Cc; main_msg ['bcc '] = Bcc; # This is invalid. main_msg ['date'] = formatdate (localtime = True) # The formatted full text fullText = main_msg.as_string () # Use smtp to send a try: server. sendmail (From,. split (';'), fullText) finally: server. quit () # OS. remove (file_name) remove the file print ("sent successfully ");
#4. there is no Chinese garbled file_name = u'f:/Python/APP writer. Doc 'urlfilename = "app writer. Doc" att = MIMEBase ('application', 'octet-stream') att. set_payload (open (file_name, 'rb '). read () att. add_header ('content-disposition', 'attachment', filename = ('gbk', '', urlfilename) # The name is not garbled during gbk receiving. Encode_base64 (att) main_msg.attach (att)