writing outlook email add a signature

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

 Risposta accettata

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

14 Commenti

Thank you Cris for your answer, it's very helpful. I like the solution using the default signature already created in Outlook by;
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];
It works very well but the only issue I have is my Signature has a image (=logotype of my company) that dosen't appear when sending outlook email by this way. I have this message on sending email "We can't display this image" instead of displaying the image. The Signature in htm displays correctly the image. How can I do ?
Thanks in advance,
Arnaud
The html signature uses a relative path to the image, which does not work when creating the message pogrammatically in MATLAB. From this stackOverflow post
  • Also keep in mind that if the signature contains images, they must also be added to the message as attachments and the <img> tags in the signature/message body adjusted to point the src attribute to the attachments rather than a subfolder of the Signatures folder where the images are stored.
Coming up with the code to do all that exceeds my level of interest in this problem.
The code I provided came from Exampe 2 on Ron de Bruin's page, titled 'Example 2 : Insert the signature that you want without picture'. If you want a picture I suggest looking at example 1, titled 'Example 1 : Default signature with or without picture (easiest example)'.
Your best bet then is to adapt example 1. To work, the desired signature has to be automatically added to all your emails in Outlook already.
% Create object and set parameters.
h = actxserver('outlook.Application');
mail = h.CreateItem('olMail');
% Signature with image
mail.Display;
sig = mail.HTMLBody;
mail.Subject = subject;
mail.To = to;
mail.BodyFormat = 'olFormatHTML';
mail.HTMLBody = [body '<br><br>' sig];
He does mention a downside to this approach is the screen flickers. I'll leave it to you to finalize the code to meet your needs.
You're right Cris, 'Example 1 : Default signature with or without picture (easiest example)' is well adapted and works very well. It's easy and very helpful. The last pbme I have is that by default outlook adds 2 extra blank lines above the signature when creating a new email. The code below keeps the 2 extra lines betwen the end of body and the begining of the sig and I dont need these 2 extra lines;
mail.HTMLBody = [body sig];
I am wondering if it's possible to remove this extra line on Outlook by defaults (I have searched but I did not find) or if it's possible to delete them in Matlab when assembling body and sig;
mail.HTMLBody = [body **** sig];
Thanks again for your help,
Arnaud
Probably.
Are you sure the line breaks are not part of your signature?
The variable sig is a character array of html code. I suggest looking at the contents of sig in your code, and edit as you need to make it fit. See the info here for how to use the debugger to insert breakpoints and inspect variables in your code.
The other option is to create a new signature just for adding signatures to your emails from MATLAB. Since you can select any htm file to add as the signature, format this one so it appears correctly in your emails. This might be the easiest way to fix it.
Hi Cris,
I am 100% sure there is 0 line break in my signature. Outlook adds automaticcaly these line breaks when creating a new email and we retrieve both by doing this (2 line breaks + signature);
% Signature with image
mail.Display;
sig = mail.HTMLBody;
I guess Outlook adds these 2 line breaks for comfort but in my case I am trying to delete them either in Outlook or in Matlab, thanks in advance,
Arnaud
Cris LaPierre
Cris LaPierre il 30 Dic 2018
Modificato: Cris LaPierre il 30 Dic 2018
What in the html is adding the two lines? It is likely something like a <br> or <p>...</p>.
Take a look at the value of sig. Once you identify it, you can then edit sig to remove it. Use strrep, or just index into sig and delete it.
sig is composed by 2 extra lines + signature. The 2 extra line before the signature are added automaticaly by Outlook when creating a new message (I guess outlook consider its more comfortable writing an email above 2 blank lone of the signature);
firstName FamilyName
email@email.com
adress
I guess strrep is for replace a string by another one, how is it possible to use strrep to replace a blank by just nothing in order to delete it ?
thnaks in advance,
Arnaud
It is likely something like a <br>. Is strrep useful to delete blank line ?
Thanks in advance,
Arnaud
Hi Cris, what do you mean by "or just index into sig and delete it." ?
Thanks in advance,
Arnaud
I linked to the documentation for strrep. Click on it to read what it does and how to use it.
The variable sig is a character array containing the html code for your signature. Inside that html, there is code that is creating the 2 extra lines. You need to remove that code and just that code.
Index into sig and delete it means doing something like this
sig(15:20) = [];
Hi Cris, thank you for your help. I think delete the extra lines might work but I want to come back with this solution which is the most elegant one;
sFile = 'C:\Users\****\AppData\Roaming\Microsoft\Signatures\External.htm';
sig = getSig(h,sFile);
% 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
The only problem is that the function getSig "loses" the image contained in the signature External. Is it because of OpenAsTextStream(1, -2) in ? Is there another way to get the signature without losing the image ? I have tried this code but image is also lost;
sig=fileread(sFile);
Thanks in advance,
Arnaud
The issue, summarized here, is that the image is NOT contained in the htm file. It doesn't matter what method is used to load it, there is no image to capture.
The other issue is the path used in the htm file is relative. Nothing I've tried has gotten the image to appear using this approach. I've tried copying the signature folder to my current folder in MATLAB, as well as replacing the relative path with the absolute path.
I think you'll need to use the approach that captures the signature from a message (example 1 in Ron de Bruin's page. It looks like the following html code is being used to add a line
<p class=MsoNormal><o:p>&nbsp;</o:p>
You want to be careful to only remove the 2 extra lines, and not anything else. I can use strrep with the following code to send myself an email using a signature with an image:
mail.Display;
sig = mail.HTMLBody;
old = '<p class=MsoNormal><o:p>&nbsp;</o:p></p><p class=MsoNormal><o:p>&nbsp;</o:p></p><p class=MsoNormal><a name="_MailAutoSig">';
new = '<p class=MsoNormal><a name="_MailAutoSig">';
sig = strrep(sig,old,new);
Thank you very much Cris, it seems that this code is working for me;
h = actxserver('outlook.Application');
mail = h.CreateItem('olMail');
% Signature with image
mail.Display;
sig = mail.HTMLBody;
ToDelete='<o:p>&nbsp;</o:p></span></p>';
sig = erase(sig,ToDelete);
its not the more elegant code we have seen but this DIY is working,
Thanks again for you help Cris,
Arnaud

Accedi per commentare.

Più risposte (0)

Categorie

Prodotti

Release

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by