Sending Emails with Matlab

This method is (as far as I know) not found in the official Matlab’s documentation.  I found it a long time ago somewhere in the internet (don’t remember where, sorry). It’s intended for sending Emails using Matlab.

In order to use it, you should have an Email account which supports SMTP (eg. Gmail). This account will be the account we’re sending the messages through.

The script assumes you’re using Gmail, but it can be easily modified to use other Email suppliers (are there any?)

Here is the script, it’s a matlab function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
function send_mail(recipients, subject, mail_message, attachments)
% send_mail sends a mail message through Gmail
% function send_mail(subject, recipients, mail_message, attachments)
% input arguments:
% subject: A string - subject of the mail
% recipents: A cell array of strings - Email addresses of recipients
%                 eg. {'mail1@mail.com', 'mail2@mail.com'}
% mail_message: A cell array of strings - Actual body of the message A cell per line.
% attachments: A cell array of string - A cell per attachment: path to a file to attach Email.
%                      Use [] for none.
% Define these:
mail = 'myemail@gmail.com'; %Your GMail email address
password = 'mypassword'; %Your GMail password
% Don't touch unless you need to change the Email supplier (currently Gmail)
setpref('Internet','E_mail',mail);
setpref('Internet','SMTP_Server','smtp.gmail.com');
setpref('Internet','SMTP_Username',mail);
setpref('Internet','SMTP_Password',password);
props = java.lang.System.getProperties;
props.setProperty('mail.smtp.auth','true');
props.setProperty('mail.smtp.socketFactory.class', 'javax.net.ssl.SSLSocketFactory');
props.setProperty('mail.smtp.socketFactory.port','465');
% Send the email
sendmail(recipients, subject, mail_message, attachments);


And it can be called this way:

1
2
3
4
5
6
7
8
9
10
11
12
send_to = {'friend@somesupplier.com'};
subject = 'hello';
message = {...
                   'hi dear!',...
                   'Just wanted to tell you what I think about you',...
                   'I HATE YOU!',...
                   'Not really :)',...
                   'Ah, see the attached pictures'...
          };
mypic1 = '../pictures/blabla/picture1.png';
mypic2 = '../pictures/blabla/picture2.png';
send_mail(send_to, subject, message, {mypic1, mypic2});


Notice that the username/password are hardcoded. If you’re concerned about this, you can pass them as arguments to the function. However, this way you’ll hide them from inside the function, but they’ll appear somewhere in the caller code.

Another method is generating the “pcode” (See Matlab’s help about pcode). In general, it’s an encrypted version of the M-file, which can be interpreted by Matlab, but cannot be read by others. I believe that this is the appropriate method in our case. Once you have the pcode, you don’t need the M-code anymore, and nobody can see your password.  (you can save a m-code version without the password/username inside it for later use if you want).

When I first tested this script, I called it from a 50-iterations for loop, sending the same Email to the same person 50 times (it included a figure from Matlab). Please don’t be like me since it’s an abuse of the script/Gmail, and I believe it’s a violation of Gmails TOS. You might end with your account disabled or blacklisted. So please use it wisely.

Thats it ;)

If you enjoyed this post, make sure you subscribe to my RSS feed!

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit Post to StumbleUpon

No related posts.

Tags: ,

Saturday, January 31st, 2009 Programming

5 Comments to Sending Emails with Matlab

  • ana says:

    شو يعني ماطلب؟؟ يعني مستحي أسأل بس أنا حمار. :)

  • Jay says:

    Great post, just one thing, code snippit : send_mail(subject, send_to, message, {mypic1, mypic2}); should be send_mail(send_to,subject message, {mypic1, mypic2}); where send_to and subject should switch places.

  • Amir Watad says:

    Fixed. Thank you Jay for the correction. :)

  • oz zoaretz says:

    Hay

    i get an erroe when i try to run the script

    the error:

    send_mail(‘gershia@gmail.com’, ‘Test from MATLAB’,'Hello! This is a test from MATLAB!’,[])
    ??? Error using ==> sendmail>sendSMTP
    SMTP error: 530 5.7.0 Must issue a STARTTLS command first. t10sm14157058muh.30

    Error in ==> sendmail at 136
    sendSMTP(out, in, ['MAIL FROM: ' from], 1);

    Error in ==> send_mail at 26
    sendmail(recipients, subject, mail_message, attachments);

    need your help with this problam…

    Thanks
    Oz

  • Ben Schwehn says:

    Works great, thanks!

  • Leave a Reply

    my email
    my photo
    Hi,
    My name is Amir Watad. I have a BSc. in biomedical engineering from The Biomedical Engineering school , Technion , Israel, and a BSc. in electrical engineering from The Electrical Engineering school , Technion , Israel.
    I'm a Verification Engineer in Mellanox Technologies Ltd.
    I love Linux, the Command Line and the OpenSource Community.
    I used to write Poems (Arabic) when I was able to find time for this.
    January 2009
    S M T W T F S
    « Dec   Feb »
     123
    45678910
    11121314151617
    18192021222324
    25262728293031
    SEO Powered by Platinum SEO from Techblissonline

    Twitter links powered by Tweet This v1.6.1, a WordPress plugin for Twitter.