Programming |
Sending E-Mail
To send an e-mail from within MATLAB, use the sendmail
function. You can also attach files to an e-mail, which lets you mail files directly from MATLAB. To use sendmail
, you must first set up your e-mail address and your SMTP server information with the setpref
function.
The setpref
function defines two mail-related preferences:
You should be able to find your outgoing SMTP server address in your e-mail account settings in your e-mail client application. You can also contact your system administrator for the information.
Once you have properly configured MATLAB, you can use the sendmail
function. The sendmail
function requires at least two arguments: the recipient's e-mail address and the e-mail subject:
You can supply multiple e-mail addresses using a cell array of strings, such as:
You can also specify a message body with the sendmail
function, such as:
In addition, you can also attach files to an e-mail using the sendmail
function, such as:
sendmail('recepient@somesever.com', 'Hello from MATLAB!',
'Thanks for using sendmail.', 'C:\yourFileSystem\message.txt
');
You cannot attach a file without including a message. However, the message can be empty.You can also attach multiple files to an e-mail with the sendmail
function, such as:
sendmail('recepient@somesever.com', 'Hello from MATLAB!', 'Thanks for using sendmail.', 'C:\yourFileSystem\message.txt
', 'C:\yourFileSystem\message2.txt
');
Example -- Using the sendmail Function
The following example sends e-mail with the retrieved Web page archive attached if it contains any matches for the specified word:
%The urlread function loads the contents of the Web page into the %MATLAB workspace. recentFile = urlread('http://www.mathworks.com/matlabcentral/fileexchange/lo adFileList.do?objectType=fileexchange&orderBy=date&srt3=0'); %The findstr function searches for the word "Simulink". hits = findstr(recentFile,'Simulink'); %The if statement checks for any hits. if(isempty(hits) == 0) %If there are hits, the Web page will be saved locally using the %urlwrite function. urlwrite('http://www.mathworks.com/matlabcentral/fileexchange/l oadFileList.do?objectType=fileexchange&orderBy=date&srt3=0','co ntains_simulink.html'); %The zip function creates a zip archive of the retrieved web page. zip('simulink_matches.zip','contains_simulink.html');%The setpref function supplies your e-mail address and SMTP server %address to MATLAB.
setpref('Internet','SMTP_Server','mail.server.network');
setpref('Internet', 'E_mail', 'youraddress@yourserver.com');
%The sendmail function sends an e-mail with the zip archive of the
%retrieved Web page attached.
sendmail('youraddress@yourserver.com', 'New Simulink Files Founds', 'New Simulink files have been uploaded to MATLAB Central. See the attached zip archive.', 'simulink_matches.zip');
end;
Creating and Uncompressing Zip Archives | Performing FTP File Operations |
© 1994-2005 The MathWorks, Inc.