- Undocumented Matlab - https://undocumentedmatlab.com -

Sending email/text messages from Matlab

Posted By Yair Altman On December 7, 2016 | 6 Comments

In this day and age, applications are expected to communicate with users by sending email/text messages to alert them about applicative events (“IBM stock purchased @$99.99” or “House is on fire!”). Matlab has included the sendmail function [1] to handle this for many years. Unfortunately, sendmail requires some tweaking to be useful on all but the most basic/insecure mail servers. Today’s post will hopefully fill the missing gaps.
None of the information I’ll present today is really new – it was all there already if you just knew what to search for online. But hopefully today’s post will concentrate all these loose ends in a single place, so it may have some value:

Using a secure mail server

All modern mail servers use end-to-end TLS/SSL encryption. The sendmail function needs extra configuration to handle such connections, since it is configured for a non-encrypted connection by default. Here’s the code that does this for gmail, using SMTP server smtp.gmail.com and default port #465 (for other SMTP servers, see here [6]):

setpref('Internet', 'E_mail', from_address);  % sender "from" address, typically same as username, e.g. 'xyz@gmail.com'
setpref('Internet', 'SMTP_Username', username);
setpref('Internet', 'SMTP_Password', password);
setpref('Internet', 'SMTP_Server',   'smtp.gmail.com');
props = java.lang.System.getProperties;
props.setProperty('mail.smtp.auth',                'true');  % Note: 'true' as a string, not a logical value!
props.setProperty('mail.smtp.starttls.enable',     'true');  % Note: 'true' as a string, not a logical value!
props.setProperty('mail.smtp.socketFactory.port',  '465');   % Note: '465'  as a string, not a numeric value!
props.setProperty('mail.smtp.socketFactory.class', 'javax.net.ssl.SSLSocketFactory');
sendmail(recipient, title, body, attachments);  % e.g., sendmail('recipient@gmail.com', 'Hello world', 'What a nice day!', 'C:\images\sun.jpg')

All this is not enough to enable Matlab to connect to gmail’s SMTP servers. In addition, we need to set the Google account to allow access from “less secure apps” (details [7], direct link [8]). Without this, Google will not allow Matlab to relay emails. Other mail servers may require similar server-side account configurations to enable Matlab’s access.
Note: This code snippet uses a bit of Java as you can see. Under the hood, all networking code in Matlab relies on Java, and sendmail is no exception. For some reason that I don’t fully understand, MathWorks chose to label the feature of using sendmail with secure mail servers as a feature that relies on “undocumented commands” and is therefore not listed in sendmail‘s documentation. Considering the fact that all modern mail servers are secure, this seems to make sendmail rather useless without the undocumented extension. I assume that TMW are well aware of this, which is the reason they posted a partial documentation in the form of an official tech-support answer [9]. I hope that one day MathWorks will incorporate it into sendmail as optional input args, so that using sendmail with secure servers would become fully documented and officially supported.

Emailing multiple recipients

To specify multiple email recipients, it is not enough to set sendmail‘s recipient input arg to a string with , or ; delimiters. Instead, we need to provide a cell array of individual recipient strings. For example:

sendmail({'recipient1@gmail.com','recipient2@gmail.com'}, 'Hello world', 'What a nice day!')

Note: this feature is actually fully documented in sendmail‘s doc-page, but for some reason I see that some users are not aware of it (to which it might be said: RTFM!).

Sending phone text (SMS) messages

With modern smartphones, text (SMS) messages have become rather outdated, as most users get push notifications of incoming emails. Still, for some users text messages may still be a useful. To send such messages, all we need is to determine our mobile carrier’s email gateway for SMS messages, and send a simple text message to that email address. For example, to send a text message to T-Mobile number 123-456-7890 in the US, simply email the message to 1234567890@tmomail.net (details [10]).
Ke Feng posted a nice Matlab File Exchange utility [11] that wraps this messaging for a wide variety of US carriers.

User configuration panel

Many GUI programs contain configuration panels/tabs/windows. Enabling the user to set up their own email provider is a typical use-case for such a configuration. Naturally, you’d want your config panel not to display plain-text password, nor non-integer port numbers. You’d also want the user to be able to test the email connection.
Here’s a sample implementation for such a panel that I implemented for a recent project – I plan to discuss the implementation details of the password and port (spinner) controls in my next post, so stay tuned:

User configuration of emails in Matlab GUI (click to zoom-in) [12]
User configuration of emails in Matlab GUI (click to zoom-in)

Categories: GUI, Java, Medium risk of breaking in future versions, Semi-documented feature, Stock Matlab function

6 Comments (Open | Close)

6 Comments To "Sending email/text messages from Matlab"

#1 Comment By Andreea On March 8, 2017 @ 11:58

I did not know it was possible to use Matlab to send emails. The nicest part is it can even send sms though it is as Basic as it can be.
I have a coouple of questions, though. Do we have to use gmail? what if we want to use another Server?
Thanks for sharing!

#2 Comment By Yair Altman On March 24, 2017 @ 13:46

@Andreea – yes it is possible, all you need to do is to provide the correct SMTP information (IP address, port number, username, password)

#3 Comment By پروژه متلب On May 4, 2017 @ 17:26

This is a great teaching post

#4 Comment By Muhammad Abdurrehman On August 13, 2017 @ 12:58

Could this function (Sending Text Messages) useful outside USA?

Secondly Is there any possibility available to communicate with WhatsApp via Matlab


#5 Comment By Yair Altman On August 13, 2017 @ 13:08

@Muhammad – you can use this feature to send text messages as long as your phone operator supports an email-to-text feature. You need to contact your operator to find out the specific details.

As far as I know WhatsApp does not enable outside programs to communicate with it, and [19].

#6 Comment By Dima On August 2, 2019 @ 11:51

Hey Yair,

It looks like in MATLAB 2019 you need to set ‘E_mail’ property, in addition to what you mentioned above:

setpref('Internet', 'E_mail' , username);

Otherwise MATLAB gives the next error:

Error using sendmail (line 65)
Could not determine FROM address.  You can set one like this:

Article printed from Undocumented Matlab: https://undocumentedmatlab.com

URL to article: https://undocumentedmatlab.com/articles/sending-email-text-messages-from-matlab

URLs in this post:

[1] sendmail function: http://www.mathworks.com/help/matlab/ref/sendmail.html

[2] Using a secure mail server: #TLS

[3] Emailing multiple recipients: #multiple

[4] Sending text messages: #SMS

[5] User configuration panel: #panel

[6] see here: https://www.arclab.com/en/kb/email/list-of-smtp-and-pop3-servers-mailserver-list.html

[7] details: https://support.google.com/accounts/answer/6010255

[8] direct link: https://www.google.com/settings/security/lesssecureapps?rfn=27&rfnc=1&asae=2&anexp=lbe-R1_C

[9] official tech-support answer: https://www.mathworks.com/matlabcentral/answers/93383-how-do-i-use-sendmail-to-send-email-from-matlab-7-2-r2006a-via-the-gmail-server-or-yahoo-server

[10] details: https://en.wikipedia.org/wiki/SMS_gateway#Email_clients

[11] Matlab File Exchange utility: http://www.mathworks.com/matlabcentral/fileexchange/16649-send-text-message-to-cell-phone

[12] Image: http://undocumentedmatlab.com/images/email_config_panel.png

[13] Sending HTML emails from Matlab : https://undocumentedmatlab.com/articles/sending-html-emails-from-matlab

[14] Setting system tray popup messages : https://undocumentedmatlab.com/articles/setting-system-tray-popup-messages

[15] Icon images & text in Matlab uicontrols : https://undocumentedmatlab.com/articles/icon-images-in-matlab-uicontrols

[16] Checking status of warning messages in MEX : https://undocumentedmatlab.com/articles/checking-status-of-warning-messages-in-mex

[17] Better MEX error messages : https://undocumentedmatlab.com/articles/better-mex-error-messages

[18] Setting status-bar text : https://undocumentedmatlab.com/articles/setting-status-bar-text

[19] : http://mashable.com/2015/03/25/whatsapp-developers-api

Copyright © Yair Altman - Undocumented Matlab. All rights reserved.