Send email through Outlook with a DeferredDeliveryTime

9 views (last 30 days)
Dear all,
I have designed a script to send automated emails to all my experiment participants (i.e. 48 hours before each experimental session):
% Importing the excel file containing participants information
ExcelPath = [pwd '\MailDataBase.xlsx'];
DataTable = readtable(ExcelPath);
DataCells = table2cell(DataTable);
% Parameters
UserName=getenv('USERNAME');
% Will only send email to participants that have not yet been contacted
% (last column of excel file should be 1 = already sent, 0 = not sent yet
for k=1:size(DataCells,1)
if DataCells{k,end} ~= 1
% Store data to be sent
MailList(k) = DataCells(k,1);
MailSubj(k) = {sprintf('! Caffeine experiment reminder for session %d (in 48h) !',DataCells{k,end-1})};
MailDates(k) = {datenum(DataCells{k,3})};
ExpDates(k) = DataCells(k,2);
% Update the last column (1 = mail is sent)
DataCells(k,end) = {1};
end
end
% For each mail to be sent
for k=1:length(MailList)
% Clear data
if exist('mail','var'); clear mail; end
if exist('h','var'); clear h; end
% Initialize connection with Outlook
h = actxserver('outlook.Application');
% Load signature (if any)
SigFiles = dir(['C:\Users\' UserName '\AppData\Roaming\Microsoft\Signatures\' '*.htm']);
if ~isempty(SigFiles) % Problem if more than 1 signature !!
SigFile = [SigFiles(1).folder '\' SigFiles(1).name];
end
Signature = getSig(h,SigFile);
% Initialize local parameters
bodyHTML1 = getSig(h,[pwd '\MailContent1.html']);
bodyHTML2 = getSig(h,[pwd '\MailContent2.html']);
bodyHTML = [bodyHTML1 sprintf('%s',ExpDates{k}) bodyHTML2];
%Sends email using MS Outlook. The format of the function is
%Similar to the SENDMAIL command.
% Taken from:
% https://ch.mathworks.com/matlabcentral/answers/94446-can-i-send-e-mail-through-matlab-using-microsoft-outlook
% Create object and set parameters.
mail = h.CreateItem('olMail');
mail.Subject = MailSubj{k};
mail.To = MailList{k};
mail.Cc = 'corentin.wicht@unifr.ch'; % Might be useless since stored in emails sent !
mail.BodyFormat = 'olFormatHTML';
mail.HTMLBody = [bodyHTML '<br><br>' Signature];
mail.DeferredDeliveryTime = [datestr(MailDates{k},'dd/mm/yyyy') ' ' '10:15:00 AM']; % NOT WORKING !!! SHIT
% Send message and release object
mail.Send;
h.release;
end
% Update the excel file
DataTableOut = cell2table(DataCells);
DataTableOut.Properties.VariableNames = DataTable.Properties.VariableNames;
writetable(DataTableOut,ExcelPath);
fprintf('The excel table has been updated in: %s\n',ExcelPath);
% Modified GetBoiler function from Ron de Bruin's Excel Automation post
function sig = getSig(h, sFile)
fso = h.CreateObject('Scripting.FileSystemObject');
ts = fso.GetFile(sFile).OpenAsTextStream(1, -2);
sig = ts.ReadAll;
ts.Close;
end
Most of the code is inspired from:
Regarding the possibility to differ the delivery time/date, I found a line of code here:
My problem is that mail.DeferredDeliveryTime seems to store something (code works fine) but the mail(s) get(s) sent immediately, hence the delay is not working..
Would you have any explanation?
Best regards,
Corentin Wicht

Answers (0)

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!