import smtplib
from smtplib import SMTP_SSL as SMTP
import datetime
HOST = "xxxxxxxx.enterprise.xxxx.com"
PORT = 25
FROM = "xxxx.xxxxxxxx@xxxx.com"
TO = ["xxxx.xxxxxxxxx@xxxx.com"]
SUBJECT = "Test"
BODY = "Success"
BODY = BODY + "!"
smtp = smtplib.SMTP(HOST,PORT)
smtp.connect(HOST, PORT)
smtp.login("xxxxxx\xxxxx", "xxxxx")
smtp.ehlo()
smtp.sendmail(FROM,TO,body)
smtp.quit()
server.set_debuglevel(0)Solved! Go to Solution.
import smtplib import datetime import logging HOST = "mailhost.xxxx.com" PORT = 25 from_addr = r"xxxx.xxxxxxx@xxxx.com" to_addr = r"xxxx.xxxxxxx@xxxx.com" subj = "ArcServer Update Script" date = datetime.datetime.now().strftime( "%d/%m/%Y %H:%M" ) message_text = "Hello\nThis is a mail from your server\n\nBye\n" msg = "From: %s\nTo: %s\nSubject: %s\nDate: %s\n\n%s" % ( from_addr, to_addr, subj, date, message_text ) try: test = "test" smtp = smtplib.SMTP(HOST, PORT) smtp.sendmail(from_addr,to_addr,msg + "%s" %(test)) smtp.quit() except Exception as e: print e logging.error(": %s" %(e))r"xxxxxx\xxxxx"
import os, time, sys
import smtplib
from socket import gaierror
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.utils import COMMASPACE, formatdate
from email import Encoders
from mah.wxDialogs import ibox, getfiles, ibox
import utils
def sendMail(host, user, pwd, port, subject, tls,
sender, to, cc, files, text, pid='0'):
'''main email sending function'''
## print host, user, pwd, port, tls
msg = MIMEMultipart()
msg['From'] = strOrBust(sender)
msg['To'] = strOrBust(COMMASPACE.join(to))
if cc:
msg['Cc'] = strOrBust(COMMASPACE.join(cc))
msg['Date'] = strOrBust(formatdate(localtime=True))
msg['Subject'] = strOrBust(subject)
#encode body unicode to str before sending
msg.attach(MIMEText(strOrBust(text)))
for file in files:
#run with basic bag of bits, using mimetypes to try to figure out
#file type is not worth the effort, and is not as reliable
part = MIMEBase('application', "octet-stream")
part.set_payload( open(file,"rb").read() )
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"'
% os.path.basename(file))
msg.attach(part)
try:
server = smtplib.SMTP(host, port)
except (smtplib.SMTPException, gaierror), error:
mailer.sb.SetStatusText('Failed to send email')
ibox(str(error) + ' Cannot connect to Server!','Failed!')
return
# Optional, but it gives you a nice view of the conversation
## server.set_debuglevel(1)
server.ehlo()
if tls:
server.starttls()
server.ehlo()
if user:
server.login(user, pwd)
#sendmail doesn't care about to and cc, so add them together
#then uniqify
toall = to + cc
toall = uniqify(toall)
try:
server.sendmail(sender, toall, msg.as_string())
if mailer:
mailer.sb.SetStatusText('Message ' + pid +
' was successfully sent')
except smtplib.SMTPException, error:
if mailer:
mailer.sb.SetStatusText('Failed to send email')
ibox(str(error),'Failed!')
server.close()
return
def strOrBust(obj, encoding='utf-8'):
'''converts unicode objects or other objects to str or returns original
encoded to enncoding'''
if isinstance(obj, basestring):
if not isinstance(obj, str):
obj = obj.encode(encoding, 'ignore')
else:
obj = obj.encode(encoding)
else:
obj = str(obj).encode(encoding)
return obj
def uniqify(seq):
''' Order preserving uniqifier for list, tuple or string'''
seen = set()
return [x for x in seq if x not in seen and not seen.add(x)]
I'm running Python 2.6.5. Yes this will be part of a script that updates a File Geodatabase automatically used by ArcServer as a feature landbase. I want the email to send to myself and another individual when the process succeeds or fails.
manhunter trying to run that throws ImportError: No module named mah.wxDialogs
Thanks I appreciate the help.
import os, time, sys
import smtplib
from socket import gaierror
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.utils import COMMASPACE, formatdate
from email import Encoders
def sendMail(host, user, pwd, port, subject, tls,
sender, to, cc, files, text, pid='0'):
'''main email sending function'''
## print host, user, pwd, port, tls
msg = MIMEMultipart()
msg['From'] = strOrBust(sender)
msg['To'] = strOrBust(COMMASPACE.join(to))
if cc:
msg['Cc'] = strOrBust(COMMASPACE.join(cc))
msg['Date'] = strOrBust(formatdate(localtime=True))
msg['Subject'] = strOrBust(subject)
#encode body unicode to str before sending
msg.attach(MIMEText(strOrBust(text)))
for file in files:
#run with basic bag of bits, using mimetypes to try to figure out
#file type is not worth the effort, and is not as reliable
part = MIMEBase('application', "octet-stream")
part.set_payload( open(file,"rb").read() )
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"'
% os.path.basename(file))
msg.attach(part)
try:
server = smtplib.SMTP(host, port)
except (smtplib.SMTPException, gaierror), error:
print str(error) + ' Cannot connect to Server!','Failed!'
return
# Optional, but it gives you a nice view of the conversation
## server.set_debuglevel(1)
server.ehlo()
if tls:
server.starttls()
server.ehlo()
if user:
server.login(user, pwd)
#sendmail doesn't care about to and cc, so add them together
#then uniqify
toall = to + cc
toall = uniqify(toall)
try:
server.sendmail(sender, toall, msg.as_string())
except smtplib.SMTPException, error:
print str(error),'Failed!'
server.close()
return
def strOrBust(obj, encoding='utf-8'):
'''converts unicode objects or other objects to str or returns original
encoded to enncoding'''
if isinstance(obj, basestring):
if not isinstance(obj, str):
obj = obj.encode(encoding, 'ignore')
else:
obj = obj.encode(encoding)
else:
obj = str(obj).encode(encoding)
return obj
def uniqify(seq):
''' Order preserving uniqifier for list, tuple or string'''
seen = set()
return [x for x in seq if x not in seen and not seen.add(x)]
import smtplib import datetime import logging HOST = "mailhost.xxxx.com" PORT = 25 from_addr = r"xxxx.xxxxxxx@xxxx.com" to_addr = r"xxxx.xxxxxxx@xxxx.com" subj = "ArcServer Update Script" date = datetime.datetime.now().strftime( "%d/%m/%Y %H:%M" ) message_text = "Hello\nThis is a mail from your server\n\nBye\n" msg = "From: %s\nTo: %s\nSubject: %s\nDate: %s\n\n%s" % ( from_addr, to_addr, subj, date, message_text ) try: test = "test" smtp = smtplib.SMTP(HOST, PORT) smtp.sendmail(from_addr,to_addr,msg + "%s" %(test)) smtp.quit() except Exception as e: print e logging.error(": %s" %(e))