File Exchange

image thumbnail


version 1.1 (2.26 KB) by

a reasonably secure wrapper for sendmail that configures, and deletes, settings for gmail servers



View License

The MATLAB function sendmail will not work with a gmail account without some modification. The problem is that gmail server uses secure authentication and requires a user and password and some changes to the port and SSL settings. The doc page for sendmail (R2010b and later) explains how to do this. Two obvious problems are: (1) the preferences where username and password are saved are persistent between sessions and kept in an unsecure file on your hard drive - a bad idea. (2) the port and SSL setting are not persistent and need to be reset for each MATLAB session - annoying. GMAIL acts as a wrapper to sendmail that sets these values, sends an email, and then erases the user and password values. It is not ultra-secure but at least your password is not saved in a plain text file. Keep in mind though that your password string now exists in the command history! So an optional last argument allows you to delete the command history file after completion. (Note: I don't know how to delete just the last command without a long edit of the file.)

Call for comments: if you can think of another place where the password may be saved after running this function please let me know in the comments. Thanks.

Comments and Ratings (13)

Naor Movshovitz

This function became obsolete shortly after it was submitted. The ML function email works fine with gmail accounts now, taking only a couple of simple setpref commands which are well documented. I am going to leave this FEX submission here for another month and then remove it, because I think it is now doing more harm than good.

Arda Aksu

it doesn't work anymore

Maybe the error
Could not connect to SMTP host:, port: 25;
Connection timed out: connect
occurs because we are behind a proxy.
And see


I also get this error:

Could not connect to SMTP host:, port: 25;
Connection refused: connect

I can use the script to send email from my home computer but not from the office. Same Matlab and Windows version. Any idea?


Naor Movshovitz

Chandra, I was unable to reproduce that error. On all machines and configurations I have access to, the emails are sent without a problem. Perhaps there is some setting on your gmail account that prevents outgoing emails from other sources? On my gmail account settings page, under the Forwarding and POP/IMAP tab, I have IMAP enabled. See if that helps.

good luck.

??? Error using ==> sendmail at 164
Exception reading response;
Connection reset

Error in ==> gmail at 26

Can U Help me??

Naor Movshovitz

John, I am not getting an error message and my emails are going through same as always. Can you give me a Matlab release and OS that generate the error?

John Fuller

John Fuller (view profile)

Like other functions of similar purpose I've found, the following error message comes up:

Could not connect to SMTP host:, port: 25;
Connection refused: connect

I found this by removing the clc from this function. I'm assuming Gmail has put in some changes that make these no longer functional.

Stan Young

Thank you for the function. It is needed. Are there any functions to retrieve headers or text from IMAP or POP3 servers that anyone is aware of?

Naor Movshovitz

Good catch Gary. Thanks. Revised file uploaded.


Gary (view profile)

Your regexp to check whether the sender email address is valid does not allow for email addresses with numbers in them.

Naor Movshovitz

That's a good idea. Thank you Jan.

Jan Simon

Jan Simon (view profile)

You can use one of the numerous password input dialogs from the FEX to ask for the password once in a Matlab session. Or you can save the password encrypted with one of the AES function from the FEX. The key for encryption can be stored in a P-coded function, but then additional obfuscation is needed to impede reading the variables with the debugger... At least no clear text password and no history deleting.



Fixed reg exp check of valid email string to allow numbers and _ and .edu emails.

MATLAB Release
MATLAB 7.11 (R2010b)
Tags Add Tags

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Win prizes and improve your MATLAB skills

Play today