When running long code streams, waiting in front of the computer watching the program run can be excruciating. Sometimes I find myself doing this if I do not know how long the code will run, or if there will be errors returned. (I also sit in front of the computer because I enjoy watching cat gifs and the old school de-fragmentation screens, so who knows…) But you don’t have to! Go out and accomplish something else while your computer chugs away. With the useful command sendmail, we can send messages to our email or phone with some simple commands. Through this post, I’ll share some sample .m files that we commonly use to notify ourselves of code completion or errors.

First of all, ‘sendmail’ takes in four arguments, the recipient contact information, the subject of the email, the email message itself and then any attachments. For example if you want to send an email to us at ‘support@matlabgeeks.com’, here’s how you’d do so:

>> sendmail( 'support@matlabgeeks.com' , 'Hello' ,... 'Thanks for the awesome email information!' );

Unfortunately, sendmail isn’t so simple, and this will just return an error, unless you have setup your email address you are sending from and the SMTP server you are using.

>> setpref( 'Internet' , 'SMTP_Server' , 'mail.__.com' ); >> setpref( 'Internet' , 'E_mail' , 'myID@__.com' ); % and/or >> setpref( 'Internet' , 'SMTP_Username' , 'myID@__.com' );

Additionally, you will have to include a password for your account if your server requires it. Placing your password in cleartext can be a security vulnerability, so be cautious.

setpref( 'Internet' , 'SMTP_Password' ,password);

Now you can send out an email! Or if you want to send an sms message, this can be done by modifying the recipient field of sendmail. For example if you want to send a text message to your verizon phone use the following:

>> sendmail( '55555555555@vtext.com' , 'Hello' ,... 'What's going on tonight?' );

A list of phone numbers that messages can be sent to is found on Mr. Tweney’s page.

One great usage of sendmail is also to message yourself when your code completes. We have attached to this post some files that describes how to do so, but here it is as well. As you will notice, I’ve encapsulated the code portion within a try catch loop, so any errors are emailed to me as well. For example try to run both function with a = 5;, but b = [2 3] or b = 2;

function sample_script(a, b) % sample error and completion file % what time is it? date_time = datestr(now); % try to do some type of command try C = [a; b]; % if it works, return the time it finished disp(C); e_mail( 'support@matlabgeeks.com' , 'matlabgeeks' , 'vlugade' ,PASSWD,... 'Completed' ,[ 'At : ' ,date_time]); catch err % find the error type errStr = err.identifier; e_mail( 'support@matlabgeeks.com' , 'gmail' , 'vlugade' ,PASSWD,... 'Completed' ,{[ 'At : ' ,date_time], [ 'With error : ' , errStr]}); end

You’ll also notice that I utilized the function ‘e_mail’ to send out messages. This e_mail.m file has been attached here as well. Overall it is similar to what you will find on Mathworks, but feel free to modify for your own uses as well. The arguments are slightly different than sendmail, in that it requires the recipient email address, the smtp server, username, and password before the email contents. We’ve included settings for gmail, yahoo, and matlabgeeks, as well as a catch all, but your settings might differ as well. You’ll also notice that you do not need to save your password in the clear within the function for it to work, but rather pass it in as an argument.

Once again the attached code.