Methods Page

Coder’s Little Time Saver

We all know the problem; it’s getting late in the office, all your colleagues left hours ago, and your eyes are watering from staring at the analysis output of a script that should have finished running ages ago. Yet for some inexplicable reason, it’s still not done. Wouldn’t it be great if you could just nip out to get some fresh air and be informed when the script is finally done? Well actually, you can! Here’s a handy little tip explaining how to embed email alerts in MATLAB and Python scripts:

MATLAB comes with a handy function that supports sending emails within scripts. But before we can get to the actual email sending, we need to configure some server information. Here is an example for a gmail account:

mail = ''; %Your GMail email address
password = 'secret';  %Your GMail password

props = java.lang.System.getProperties;
props.setProperty('mail.smtp.socketFactory.class', '');

Now, we are ready to send an email:

sendmail('','Hello there!');

The second argument in the sendmail function corresponds to the subject line of the email. If you are keen to let MATLAB send a more elaborate email, you can also include a text body:

sendmail('','Hello there!','Have you seen that great post on the Forging Connections Blog?');

It is even possible to send attachments:

sendmail('','Hello there!','Have you seen that great post on the Forging Connections Blog?',{'/Users/Fred/image.jpeg’});avou

By including these few lines of code in your time consuming MATLAB script, you can now get notified when it is time to go back to the office for the results.

Python offers a simple solution to send emails from within scripts via the smtplib module. Here is a function that provides the configuration for gmail:

def send_email():
            import smtplib

            gmail_user = ""
            gmail_pwd = "Password"
            FROM = ''
            TO = [''] 
            SUBJECT = "Meeting at 3pm"
            TEXT = "Are you coming to the meeting?"

            # Prepare actual message
            message = """\From: %s\nTo: %s\nSubject: %s\n\n%s
            """ % (FROM, ", ".join(TO), SUBJECT, TEXT)
                #server = smtplib.SMTP(SERVER) 
                server = smtplib.SMTP("", 587) 
                server.login(gmail_user, gmail_pwd)
                server.sendmail(FROM, TO, message)
                print 'successfully sent the mail'
                print "failed to send mail"

For more information see and

Leave a Response

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: