How to use sendmail in an executable

9 views (last 30 days)
Matthew
Matthew on 24 Feb 2013
I want to write an executable file for other people to run on their computer (they do not have matlab) that will automatically send me an anonymous email with the results of their simulation. How do I do this if sendmail requires you to modify your settings? How will the executables run on someone else's computer work?

Answers (2)

Image Analyst
Image Analyst on 24 Feb 2013
You could have a text file with the information specific to that computer, and then read that in. You can get some stuff from the OS, for example:
% Get the email of the user who logged in to the computer.
userProfile = getenv('USERPROFILE');
lastSlashLocation = find(userProfile == '\', 1, 'last');
% Build up the email from this:
usersEmail = [userProfile(lastSlashLocation + 1 : end) '@yourDomain.com'];
  1 Comment
Image Analyst
Image Analyst on 24 Feb 2013
See this code. It should run with some modifications. I sanitized it a bit to take out my info and make it more generic. Go down line by line, read the comments, and make the obvious necessary modifications.
% testDescription is a structure with the requesters (recipient) email, and the requester's name.
function Send_EMail(testDescription, excelFullFileName)
try
% NOTE: CHANGE THE 2 LINES OF CODE WITH mySMTP and sendersEmail TO REFLECT YOUR SETTINGS.
mySMTP = 'yourMailServer.Mathworks.com';
% Assign the sender's email address. It can be an actual, real e-mail address,
% but it does not have to be an account that actually exists - you can make up one.
% sendersEmail = 'Matthews@Mathworks.com';
sendersEmail = 'ImageAnalysisTeam@Mathworks.com';
% Get the email of the user who logged in to the computer.
userProfile = getenv('USERPROFILE');
lastSlashLocation = find(userProfile == '\', 1, 'last');
usersEmail = [userProfile(lastSlashLocation + 1 : end) '@Mathworks.com'];
% Set your email and SMTP server address in MATLAB.
setpref('Internet', 'SMTP_Server', mySMTP)
setpref('Internet', 'E_mail', sendersEmail)
% Send an email to recipient with the file attached.
% recipient = sendersEmail; % Send to yourself.
% recipientsEMail = 'Matthews@mathworks.com';
recipientsEMail = testDescription.requestersEmail;
if length(recipientsEMail) < 5 || isempty(recipientsEMail)
warningMessage = sprintf('Warning: requester email address of "%s" is invalid.', recipientsEMail);
WarnUser(warningMessage);
% Bail out for an invalid email.
return;
end
% % Have user browse and specify the full file name of the file to attach.
% % startingFolder = pwd;
% startingFolder = 'C:\Users\Public\Documents';
% defaultFileName = fullfile(startingFolder, '*.*');
% [baseFileName, folder] = uigetfile(defaultFileName, 'Select a file to attach to the e-mail.');
% if baseFileName == 0
% % User clicked the Cancel button.
% return;
% end
attachedFullFileName = excelFullFileName;
% Try alternate names if this one didn't exist.
if ~exist(attachedFullFileName, 'file')
attachedFullFileName = strrep(lower(attachedFullFileName), '.xls', '.xml');
end
if ~exist(attachedFullFileName, 'file')
attachedFullFileName = strrep(lower(attachedFullFileName), '.xml', '.xlsx');
end
% Prepare the subject line.
% If the attached file is something like
% "C:\Users\Public\Documents\Final Stats Report.XML"
% then extract out the test name.
[folder baseFileName ext] = fileparts(attachedFullFileName);
subjectLine = sprintf('Your Results: %s', baseFileName);
% Prepare the message body of the e-mail.
% Outlook quirk. Outlook as a default setting where it will remove line breaks that it thinks are unnecessary.
% It will keep line breaks if the last character before it is a period, but for others, it seems to randomly
% and arbitrarily remove some of them. To get around this, the workaround (http://www.emailsignature.eu/phpBB2/outlook-is-stripping-line-breaks-from-plain-text-emails-t1775.html)
% is to put three spaces in front of EACH line in the email
messageBody = sprintf(' %s,', testDescription.requestersName);
messageBody = sprintf('%s\n The Image Analysis Lab is pleased to deliver the results from', messageBody);
messageBody = sprintf('%s\n the Image Analysis Test that you requested.', messageBody);
messageBody = sprintf('%s\n Your results are in the attached Excel workbook.', messageBody);
messageBody = sprintf('%s\n\n Please do not reply to this email because it is not monitored.', messageBody);
messageBody = sprintf('%s\n If you have any questions, requests, concerns, or comments,', messageBody);
messageBody = sprintf('%s\n please contact your image analysis person, or %s (who ran this test).', messageBody, usersEmail);
% Here's where we actually send out the e-mail with the file attached.
sendmail(recipientsEMail, subjectLine, messageBody, attachedFullFileName)
% Alert sender that the message, with attached file, has been sent.
message = sprintf('An email has been sent from\n\n%s\n\nto\n\n%s\n\nwith the attached file:\n\n%s',...
sendersEmail, recipientsEMail, attachedFullFileName);
uiwait(helpdlg(message));
catch ME
errorMessage = sprintf('Error sending email to %s in function Send_EMail().\nThe error reported by MATLAB is:\n\n%s', recipientsEMail, ME.message);
WarnUser(errorMessage);
end
% Change mouse pointer (cursor) to an arrow.
set(gcf,'Pointer','arrow');
drawnow; % Cursor won't change right away unless you do this.
return % from Send_EMail()

Sign in to comment.


Walter Roberson
Walter Roberson on 24 Feb 2013
If you have the Instrument Control toolbox, or if you use the FEX contribution "tcpudpip", then you could have the program telnet to port 25 of the mail server and send the email message using the SMTP protocol.
  5 Comments
Ferdinand
Ferdinand on 24 May 2013
Edited: Ferdinand on 24 May 2013
The "sendmail" function in R2011b and newer versions can do the same. If I am missing something let me know...
Image Analyst
Image Analyst on 24 May 2013
sendmail() was one of the lines in my code. After all the setup stuff, that is the line that actually does the sending.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!