MATLAB: Writing outlook email add a signature

mailMATLABoutlooksignature

Hello everybody,
thanks in advance for your help. I am writing outlook email through Matlab;
h = actxserver('outlook.Application');
mail = h.CreateItem('olMail');
mail.Subject = Subject;
mail.BodyFormat = 'olFormatHTML';
mail.HTMLBody = BodyText;
mail.To = emailadress;
mail.Send;
h.release;
BodyText is on HTML format. I am trying to add a signature that already exists in Outlook but I dont how to do it. Anyone has a solution ?
If not possible maybe I can add my signature at the end of BodyText but I don't know how to add a logotype image that will be correctly displayed in the sent email (given that BodyText is created by a word document) ?
Thanks in advance for you help
Arnaud

Best Answer

It appears to depend on if a signature is added by default to your emails or not. You can see one way of accomplishing it on stackexchange here.
Another good place to look for help is apparently VBA discussion forums. Here's a good one. Of course, you need to adapt the VBA to run in MATLAB, but it's pretty similar.
Since I don't have a default signature in my emails, I thought I'd take a stab coding something to add my external signature to a MALTAB generated outlook message. This worked for me. I'm borrowing heavily from the 2nd link I shared to get the signature, as well as this MATLAB Answers post on sending mail through Outlook. I've only had to make a couple simple modifications.
function sendolmail(to,subject,body,attachments)
%Sends email using MS Outlook. The format of the function is
%Similar to the SENDMAIL command.
% Create object and set parameters.
h = actxserver('outlook.Application');
% Extract signature html. In Windows 10, the htm file is located here
% C:\Users\<UserName>\AppData\Roaming\Microsoft\Signatures
sFile = 'C:\Users\****\AppData\Roaming\Microsoft\Signatures\External.htm';
sig = getSig(h,sFile);
mail = h.CreateItem('olMail');
mail.Subject = subject;
mail.To = to;
mail.BodyFormat = 'olFormatHTML';
mail.HTMLBody = [body '<br><br>' sig];
% Add attachments, if specified.
if nargin == 4
for i = 1:length(attachments)
mail.attachments.Add(attachments{i});
end
end
% Send message and release object.
mail.Send;
h.release;
end
% 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