# MailUtilities.py # Paul C. Pharr # August 7, 2001 # # Utility functions for sending mail import sys import string import socket import smtplib import base64 gMailDomain = 'nemetschek.net' gMailhost = 'mail.nemetschek.net' kErrorNoError = 0 kErrorNoServer = 1 kErrorBadList = 2 kErrorIndividualMailFailed = 3 kErrorMailMayHaveFailed = 4 def MakeLocalAddress(addr): return addr + '@' + gMailDomain ############### # SendMail ############### # # This function sends a mime formatted text email message # to one or more recipients and allows one or more binary # or text file attachments to be included at the end of # the message # # sender - A single string containing the sender's email address # recipients - A list of one or more tuples containing the full name # followed by the email address of the recipients # subject - A string contiaining the subject # message - A string contiaining the message # enclosures - A list of tuples with item[0] containing the file name and # item[1] containing the bytes of the file itself # def SendMail(sender, recipients, subject, message, enclosures = [], html = 0): result = kErrorNoError try: mailport=smtplib.SMTP(gMailhost) except: sys.stderr.write("Sending mail failed - could not find SMTP server\n") return kErrorNoServer if recipients == None: sys.stderr.write("Sending mail failed - Bad address list\n") return kErrorBadList try: #create address list for mail header fullAddresses = [] # list of strings of the form '"Full Name" ' justEmail = [] for i in range(len(recipients)): fullAddresses.append('"%s" <%s>' % (recipients[i][0],recipients[i][1])) justEmail.append(recipients[i][1]) except: sys.stderr.write("Sending mail failed - Bad address list\n") return kErrorBadList # need to pass the full name of the sender into the function senderFullAddress = '"%s" <%s>' % (sender, sender) #set content type if html: contentType = "text/html" else: contentType = "text/plain" #compose MIME message with enclosures fullMessage = 'From: ' + sender + '\n' +\ 'To: ' + string.join(fullAddresses,', ') + '\n' +\ 'Mime-Version: 1.0\n' +\ 'Subject: ' + subject + '\n' +\ 'Content-Type: multipart/mixed; boundary=content-bounds\n' +\ '\n' +\ 'This is a MIME message and should be read with a MIME capable mail reader.\n' +\ '--content-bounds\n' +\ 'Content-Type: ' + contentType + '; charset=US-ASCII\n' +\ 'Content-Transfer-Encoding: 7bit\n' +\ 'Content-Disposition: inline\n' +\ '\n' +\ message + '\n' for i in enclosures: fullMessage += '--content-bounds\n' +\ 'Content-Type: application/text; name="' + i[0] + '"\n' +\ 'Content-Transfer-Encoding: base64\n' +\ 'Content-Disposition: attachment; filename="' + i[0] + '"\n' +\ '\n' +\ base64.encodestring(i[1]) fullMessage += '--content-bounds--\n' badAddresses = 0 try: badAddresses = mailport.sendmail(senderFullAddress, justEmail, fullMessage) except: sys.stderr.write("Sending mail failed - Bad address list\n") result = kErrorBadList if badAddresses: sys.stderr.write('Some addresses failed to send:\n') sys.stderr.write(str(badAddresses)+'\n') result = kErrorIndividualMailFailed #Exception Type: smtplib.SMTPServerDisconnected #Exception Value: Connection unexpectedly closed #Exception Stack: # # File "/PerforceTools/Libraries/MailUtilities.py", line 118, in SendMail # mailport.quit() # File "/usr/local/lib/python2.1/smtplib.py", line 516, in quit # self.docmd("quit") # File "/usr/local/lib/python2.1/smtplib.py", line 294, in docmd # return self.getreply() # File "/usr/local/lib/python2.1/smtplib.py", line 271, in getreply # raise SMTPServerDisconnected("Connection unexpectedly closed") try: mailport.quit() except smtplib.SMTPException: sys.stderr.write("Got SMTP exception when quitting!\n") result = kErrorMailMayHaveFailed except socket.error: sys.stderr.write("Got socket exception when quitting!\n") result = kErrorMailMayHaveFailed if result == kErrorNoError: sys.stderr.write("Sent mail successfully: '%s'\n" % subject) else: sys.stderr.write("Sent mail attempt failed for: '%s' with error %s.\n" % (subject, str(result))) return result