Main Content

sendmail

Send email message to address list

Syntax

sendmail(recipients,subject)
sendmail(recipients,subject,message)
sendmail(recipients,subject,message,attachments)

Description

sendmail(recipients,subject) sends email to recipients with the specified subject. For a single recipient, specify recipients as a character vector or a string. For multiple recipients, specify recipients as a cell array of character vectors or a string array. subject must be a character vector or string.

sendmail(recipients,subject,message) includes the specified message. If message is a character vector or a string, sendmail automatically wraps text at 75 characters. To force a line break in the message text, use 10, as shown in the Examples. If message is a cell array of character vectors or a string array, then each element represents a new line of text.

sendmail(recipients,subject,message,attachments) attaches the files listed in the attachments input argument. attachments can be a character vector, cell array of character vectors, or string array.

Examples

Send a message with two attachments to a hypothetical email address:

sendmail('user@otherdomain.com',...
         'Test subject','Test message',...
         {'folder/attach1.html','attach2.doc'});

Send a message with forced line breaks (using 10) to a hypothetical email address:

sendmail('user@otherdomain.com','New subject', ...
        ['Line1 of message' 10 'Line2 of message' 10 ...
         'Line3 of message' 10 'Line4 of message']);

The resulting message is:

Line1 of message
Line2 of message
Line3 of message
Line4 of message

Tips

  • The sendmail function does not support HTML-formatted messages. However, you can send HTML files as attachments.

  • If sendmail cannot determine your email address or outgoing SMTP mail server from your system registry, specify those settings using the setpref function. For example:

    setpref('Internet','SMTP_Server','my_server.example.com');
    setpref('Internet','E_mail','my_email@example.com');

    To identify the SMTP server for the call to setpref, check the preferences for your electronic mail application, or consult your email system administrator. If you cannot easily determine the server name, try 'mail', which is a common default, such as:

    setpref('Internet','SMTP_Server','mail');
  • By default, the sendmail function does not support email servers that require authentication. To support these servers, change your system settings and set preferences for the SMTP user name and password, with commands in the following form:

    props = java.lang.System.getProperties;
    props.setProperty('mail.smtp.auth','true');
    
    setpref('Internet','SMTP_Username','myaddress@example.com');
    setpref('Internet','SMTP_Password','mypassword');
  • To override the default character encoding, set the preference for email character encoding as follows:

    setpref('Internet','E_mail_Charset',encoding); 
    where encoding is a character vector specifying the character encoding, such as 'SJIS'.

Alternatives

On Windows® systems with Microsoft® Outlook®, you can send email directly through Outlook by accessing the COM server with actxserver. For an example, see Solution 1-RTY6J.