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
No related posts.
5 Comments to Sending Emails with Matlab
Leave a Reply
About Me
Tags
Categories
- Algorithms
- Bash
- BlackBerry
- Collaboration
- Command Line
- Cool Tricks
- Easter Eggs
- Ebooks
- Firefox
- Hardware
- Humor
- iPhone
- Linux
- Linux Development
- Linux Kernel
- Networks
- Open Knowledge
- Other
- Productivity
- Programming
- Regular Expressions
- Science
- Security
- Shell Scripts
- Short Posts
- Social Networks
- Thoughts
- Tools
- Vim
- Web Development
- Websites
Popular Posts
Calendar
Archives
- July 2010 (5)
- June 2010 (1)
- May 2010 (1)
- April 2010 (3)
- March 2010 (1)
- January 2010 (1)
- December 2009 (2)
- September 2009 (13)
- July 2009 (1)
- June 2009 (6)
- May 2009 (4)
- March 2009 (18)
- February 2009 (10)
- January 2009 (10)
- December 2008 (7)
- November 2008 (8)
- October 2008 (1)
- August 2008 (1)
- July 2008 (1)
- June 2008 (2)

شو يعني ماطلب؟؟ يعني مستحي أسأل بس أنا حمار.
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.
Fixed. Thank you Jay for the correction.
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
Works great, thanks!