From jwhughes at hughesconcepts.com Wed Jul 7 22:53:54 2010 From: jwhughes at hughesconcepts.com (Joe Hughes) Date: Wed, 7 Jul 2010 16:53:54 -0400 Subject: [Email-SIG] Question about Multi-part message In-Reply-To: <1278521450.98.12715@mint-julep.mondoinfo.com> References: <1278521450.98.12715@mint-julep.mondoinfo.com> Message-ID: <6D9000D7-9065-4CF9-B555-B5F500345785@hughesconcepts.com> All, As you can see below I'm a bit baffled. I got the code working by commenting three lines, but I would like to have to, from, and date in the header. Any suggestions on how to make this work? Thanks, Joe #!/usr/local/bin/python3 ######################################################################## # Imports ######################################################################## from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText import email.utils import smtplib ######################################################################## # Constants ######################################################################## GMAIL_PORT = '587' GMAIL_SERVER = 'smtp.gmail.com' MAIL_PORT = 'XX' MAIL_SERVER = 'mailserver.company.com' PASSWORD = 'password' USERNAME = 'username at gmail.com' ######################################################################## # Main Routine ######################################################################## # # Try to connect to the mail server, handle exception # try: mail_server = smtplib.SMTP(MAIL_SERVER, MAIL_PORT) print("Valid response from", MAIL_SERVER) mail_server.quit() except: mail_from = USERNAME mail_to = ['user1 at company.com', 'user2 at company.com'] print("Invalid response from", MAIL_SERVER) try: gmail_server = smtplib.SMTP(GMAIL_SERVER, GMAIL_PORT) gmail_server.set_debuglevel(True) except: print("Invalid response to ssl connection.") try: gmail_server.ehlo() except: print("First ehlo failed") try: gmail_server.starttls() except smtplib.SMTPHeloError: print('This is an SMTP Helo Error when trying to start tls') except smtplib.SMTPException: print('This is an SMTP Exception') except RuntimeError: print('This is a Runtime Error') except: print("start tls failed") try: gmail_server.ehlo() except: print("second ehlo failed") try: gmail_server.login(USERNAME, PASSWORD) except: print("Login failed") try: message = "No response from " + MAIL_SERVER + " Possibly down." msg = MIMEMultipart() # msg['from'] = mail_from # msg['to'] = mail_to # msg['date'] = email.utils.formatdate(localtime=True) msg['subject'] = 'Company Mail Server Issue' msg.attach(MIMEText(message)) except: message = """Error creating MIME email message""" msg = MIMEMultipart() msg.attach(MIMEText(message)) try: gmail_server.sendmail(mail_from, mail_to, msg.as_string()) gmail_server.quit() except: print("Invalid response from", GMAIL_SERVER) -------------- next part -------------- An HTML attachment was scrubbed... URL: From mark at msapiro.net Thu Jul 8 02:33:25 2010 From: mark at msapiro.net (Mark Sapiro) Date: Wed, 07 Jul 2010 17:33:25 -0700 Subject: [Email-SIG] Question about Multi-part message In-Reply-To: <6D9000D7-9065-4CF9-B555-B5F500345785@hughesconcepts.com> References: <1278521450.98.12715@mint-julep.mondoinfo.com> <6D9000D7-9065-4CF9-B555-B5F500345785@hughesconcepts.com> Message-ID: <4C351CD5.1080203@msapiro.net> On 7/7/2010 1:53 PM, Joe Hughes wrote: > All, > > As you can see below I'm a bit baffled. I got the code working by > commenting three lines, but I would like to have to, from, and date in > the header. Any suggestions on how to make this work? > [...] > except: > mail_from = USERNAME > mail_to = ['user1 at company.com', > 'user2 at company.com'] This is cause the problem. [...] > try: > message = "No response from " + MAIL_SERVER + " Possibly down." > msg = MIMEMultipart() > # msg['from'] = mail_from > # msg['to'] = mail_to > # msg['date'] = email.utils.formatdate(localtime=True) You are setting msg['To'] to a list. It needs to be a string, e.g. if isinstance(mail_to, list): msg['To'] = ', '.join(mail_to) else: msg['To'] = mail_to Also, when you set headers, they will be capitalized or not as you set them so set msg['From'], msg['To'], msg['Date'] and msg['Subject']. -- Mark Sapiro The highway is for gamblers, San Francisco Bay Area, California better use your sense - B. Dylan From jwhughes at hughesconcepts.com Thu Jul 8 03:52:17 2010 From: jwhughes at hughesconcepts.com (Joe Hughes) Date: Wed, 7 Jul 2010 21:52:17 -0400 Subject: [Email-SIG] Question about Multi-part message In-Reply-To: <4C351CD5.1080203@msapiro.net> References: <1278521450.98.12715@mint-julep.mondoinfo.com> <6D9000D7-9065-4CF9-B555-B5F500345785@hughesconcepts.com> <4C351CD5.1080203@msapiro.net> Message-ID: <39DD5B42-9721-45AD-84D9-75B0DA6A85A9@hughesconcepts.com> Hi Mark, Thanks. With that information I was able to get the code working. Now I just have to find the right machine to run it on instead of my personal box. Joe On Jul 7, 2010, at 8:33 PM, Mark Sapiro wrote: > On 7/7/2010 1:53 PM, Joe Hughes wrote: >> All, >> >> As you can see below I'm a bit baffled. I got the code working by >> commenting three lines, but I would like to have to, from, and date in >> the header. Any suggestions on how to make this work? >> > [...] >> except: >> mail_from = USERNAME >> mail_to = ['user1 at company.com', >> 'user2 at company.com'] > > > This is cause the problem. > > [...] >> try: >> message = "No response from " + MAIL_SERVER + " Possibly down." >> msg = MIMEMultipart() >> # msg['from'] = mail_from >> # msg['to'] = mail_to >> # msg['date'] = email.utils.formatdate(localtime=True) > > > You are setting msg['To'] to a list. It needs to be a string, e.g. > > if isinstance(mail_to, list): > msg['To'] = ', '.join(mail_to) > else: > msg['To'] = mail_to > > > Also, when you set headers, they will be capitalized or not as you set > them so set msg['From'], msg['To'], msg['Date'] and msg['Subject']. > > -- > Mark Sapiro The highway is for gamblers, > San Francisco Bay Area, California better use your sense - B. Dylan >