Loren on the Art of MATLAB

September 16th, 2010

My First Use of sendmail

Today I was working on a demo that someone else started. And then, wouldn't you know, I hit a snag. I decided that now was the time to learn to use sendmail so I could send my version of code right from my MATLAB session. It was so easy, I thought I'd show you how to do it.

Contents

Set Preferences

I first tried using sendmail without reading all of the doc, just the input argument list. And I got an error telling me I had to use setpref to set MATLAB up to know about my internet presence. The first error message showed that I had to set up information about my email account.

       setpref('Internet','E_mail','xxx@company.com')

I tried sending the message again, and got prompted with a second error, so decided to read the reference page so I would bypass any other errors as well. Turned out, that wasn't necessary. I fixed my preferences again to set up information about the mail server.

       setpref('Internet','SMTP_Server','mail')

In Business!

Now I was in business and able to send the MATLAB code to my co-worker without leaving MATLAB.

       sendmail('yyy@company.com','demo files', ...
                'loren''s version','mydemo.m')

Do You Use sendmail?

I can imagine setting up a long simulation and sending my self email to let me know when it's done, especially if I am doing so during the evening or weekend. What tasks do you use sendmail for? Let me know here.


Get the MATLAB code

Published with MATLAB® 7.11

18 Responses to “My First Use of sendmail”

  1. naor replied on :

    Unfortunately it’s not so simple if you are using gmail:

    >> sendmail(’12345@friend.net’,'test’,'this is a test’)
    ??? Error using ==> sendmail at 168
    530 5.7.0 Must issue a STARTTLS command first. x9sm5155434waj.15

    there is an undocumented workaround here that i haven’t tried yet
    http://www.mathworks.com/support/solutions/en/data/1-CZXO98/index.html?solution=1-CZXO98

  2. Jiang Hengqiang replied on :

    When I try out these functions,’553 authentication is required’ was displayed. What should I do to let Matlab aware of my E-mail password?

  3. Paul replied on :

    I use sendmail to send text messages to my phone. It is exceptionally useful when it takes days to run my code. It can help signal directly to my cell phone in the case certain events occurred or computational stages have been passed.

  4. Luca Balbi replied on :

    I used sendmail in the past, just to let my simulation tell me it had finished. The company has an SMS-by-mail sending service, so I had notifications right on my cell phone!

  5. Aurelien Queffurust replied on :

    I am using sendmail to automatically send to my coworkers the executable files after a compilation .
    Since I am the only guy to have MATLAB Compîler in my company, I often receive requests to compile their MATLAB codes. I just wrote a small code which compile their code using mcc command and then sendmail is invoked to send the standalone and the output log (given by the verbose flag of mcc) .
    The benefit is that they directly know by email when the compilation has been performed, no need to come in my office !

    I also use sendmail in codes when an important information has been detected . For example , we receive in real-time seismic event files , we want to know when one of these files is not just a noise but a microseismic event.

  6. Loren replied on :

    Thanks for the feedback, everyone.

    Jiang: See this note in the documentation:
    “Note The sendmail function does not support e-mail servers that require authentication.”.

    –Loren

  7. BenB replied on :

    You can get gmail to work with sendmail with a bit of tweaking, it’ll need your password (so beware all the caveats about plain text passwords and ripping data from p codes).

    Here’s a function to send a short message to an email address of your choice. [Working in 2010a at least.]

    function []=gmail(address,subject,message)
    % Define these variables appropriately:
    mail = 'XXX@gmail.com'; %Your GMail email address
    password = 'PASSWORD'; %Your GMail password
    
    % Then this code will set up the preferences properly:
    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(address,subject,message)
    end
    
  8. Loren replied on :

    Thanks BenB!

    I think some folks will really benefit from your code here.

    –loren

  9. Aurelien Queffurust replied on :

    @BenB

    Your code can also be found in sendmail MATLAB documentation within R2010b only.
    >> doc sendmail
    or on the web:
    http://www.mathworks.com/access/helpdesk/help/techdoc/ref/sendmail.html

  10. Benjamin Kraus replied on :

    I wrote a function a couple years ago that does exactly what you proposed. It acts as a wrapper for another function (that in theory takes a long time to complete). It will notify you when the function finishes, or if it crashes. It should work with any preexisting function, without modification to your own code.

    I just uploaded it to the file exchange if people are interested:
    http://www.mathworks.com/matlabcentral/fileexchange/28733

    - Ben

  11. John M replied on :

    This is a cool function that I had no idea about. I will try to use it soon.

    Loren — FYI your helpdesk link to sendmail is broken.

  12. Loren replied on :

    Glad you like the function, John. Thanks, I fixed the link.

    –Loren

  13. Mohammad replied on :

    I think there is an easier way without having all these settings. You can easily create and executable file with Autohotkey software to send you and email, call/sms you with an online connection e.g. skype or anything. Then call the exe file at the end of your code with MATLAB. You can also set it to run other functions once the results are ready.

  14. Peter Larsen replied on :

    Can you set up sendmail to work with a microsoft exchange server?

    Peter

  15. Loren replied on :

    Mohammad-
    Ease is partly in the eyes of the beholder and what software they have access to.

    Peter-
    We use an exchange server at work, and I was able to use sendmail. It might depend on your server’s policies, I’m not sure about that.

    –Loren

  16. BenB replied on :

    @Aurelien Queffurust

    Thanks for pointing that out. I was trying to find out where this code was from originally and it was was taken from http://www.mathworks.com/support/solutions/en/data/1-3PRRDV/

    Ben

  17. Ahsan replied on :

    i want to know how can i send sms using sendmail? please let me know if anyone here knows the solution.my email id is sh_ahsan2003@hotmail.com

  18. Loren replied on :

    Ahsan-

    I found this entry on the file exchange: http://www.mathworks.com/matlabcentral/fileexchange/16649

    –Loren


MathWorks
Loren Shure works on design of the MATLAB language at MathWorks. She writes here about once a week on MATLAB programming and related topics.

These postings are the author's and don't necessarily represent the opinions of The MathWorks.