Uncharted 2 Teaser MEL procs using -flag commands
Dec 03

I was thinking today it would be handy to send an e-mail to myself when a tool was done. Guess what its fairly easy to do with Python in Maya and here is an example:

import smtplib
import email.Message

# Create a function to send an e-mail
def pyMail(user='', password='', serverURL=None, sender='', to='', subject='', text=''):

	mailfailed = ''
	try:
		# The Message object helps with formating the e-mail for the server to read
		message = email.Message.Message()
		message['To'] = to
		message['From'] = sender
		message['Subject'] = subject
		message.set_payload(text)

		# Connect to the mailsever
		# Also you can specify a specific port like this:
		# mailServer = smtplib.SMTP(serverURL, 365)
		print 'connecting...',
		mailServer = smtplib.SMTP(serverURL)
		mailServer.set_debuglevel(1)

		# (Transport Layer Security) mode. All SMTP commands that follow will be encrypted.
		print 'starting tls...',
		try:
			mailServer.starttls()
			mailServer.ehlo(serverURL)
		except: pass

		# If you pass a user name and password it will be passed to the mail server
		# Note if your server requires auth it will fail without
		# If the server does not require auth it will fail if you pass it something
		print 'logging in...',
		if(user and password):
			mailServer.login(user, password)

		# It is a requirement to add '\n.\n' to the end of a message
		print 'sending...',
		mailfailed = mailServer.sendmail(sender, to, message.as_string())

		print 'disconnecting...',
		try:
			mailServer.quit()
		except: pass

		print 'mail sent.'
	except:
		print 'Error in sending mail.',

	if mailfailed:
		print 'failed:', mailfailed  # some recipients, but not all of them, failed

# Send an e-mail
pyMail( serverURL	= 'mail.server.com',
	sender = 'from@address.com',
	to = 'to@address.com',
	subject = 'hello',
	text = 'world',
	user = 'user',
	password = 'password')

Not to hard ehh? And there is nothing wrong with you creating a python function like this and calling it with MEL if this is all you want from Python.

Happy coding,
-RyanT

One Response to “Maya Python Email Module”

  1. JO Says:

    Gave this a shot with a gmail acct, as per your instructions, and it errors out, giving only:

    connecting… Error in sending mail.

    I think this may be a PYTHONPATH issue - since it works for some and not others…

Leave a Reply