Quantcast

Documentation Center

  • Trial Software
  • Product Updates

Sending Email

To send an email from MATLAB®, use the sendmail function. You can also attach files to an email, which lets you mail files directly from MATLAB. To use sendmail, you must first set up your email address and your SMTP server information with the setpref function.

The setpref function defines two mail-related preferences:

  • Email address: This preference sets your email address that will appear on the message. Here is an example of the syntax:

    setpref('Internet','E_mail','youraddress@yourserver.com');
    
  • SMTP server: This preference sets your outgoing SMTP server address, which can be almost any email server that supports the Post Office Protocol (POP) or the Internet Message Access Protocol (IMAP). Here is an example of the syntax:

    setpref('Internet', 'SMTP_Server', 'mail.server.network');
    

You should be able to find your outgoing SMTP server address in your email account settings in your email client application. You can also contact your system administrator for the information.

    Note   The sendmail function does not support email servers that require authentication.

Once you have properly configured MATLAB, you can use the sendmail function. The sendmail function requires at least two arguments: the recipient's email address and the email subject:

sendmail('recipient@someserver.com', 'Hello From MATLAB!');

You can supply multiple email addresses using a cell array of strings, such as:

sendmail({'recipient@someserver.com', ... 
'recipient2@someserver.com'}, 'Hello From MATLAB!');

You can also specify a message body with the sendmail function, such as:

sendmail('recipient@someserver.com', 'Hello From MATLAB!', ... 
'Thanks for using sendmail.');

In addition, you can also attach files to an email using the sendmail function, such as:

sendmail('recipient@someserver.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 email with the sendmail function, such as:

sendmail('recipient@someserver.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 email with the retrieved Web page archive attached:

% NOTE: CHANGE THESE 2 LINES OF CODE TO REFLECT YOUR SETTINGS.
mySMTP = 'mail.server.network';
myEmail = 'youraddress@yourserver.com';

% Set your email and SMTP server address in MATLAB.
setpref('Internet','SMTP_Server',mySMTP);
setpref('Internet','E_mail',myEmail);

% Locate the list of files at the MATLAB Central File Exchange
% uploaded within the past 7 days, that contain "Simulink."
filex = 'http://www.mathworks.com/matlabcentral/fileexchange/';
params = {'duration','7','term','simulink'};

% Save the Web content to a file.
urlwrite(filex,'contains_simulink.html','get',params);

% Create a zip archive of the retrieved Web page.
zip('simulink_matches.zip','contains_simulink.html');

% Send an email (to yourself) with the zip archive attached.
recipient = myEmail;
subj = 'List of New Simulink Files';
msg = ...
  'Attached: new Similink files uploaded to MATLAB Central.';
attFile = 'simulink_matches.zip';
sendmail(recipient,subj,msg,attFile);
Was this topic helpful?