Problem to send email (cell array not accepted)

  • Follow


Hello,

I use a code found on Mathworks.com to send mails via Outlook from my matlab code. The synthax to call this function is like sendmail(to, subject, body, attachment). The problem I have is that it does not accept cell array in my case ! Normally it should work. So for a text of several lines. The body = {'line 1', 'lin2', 'lin3'} is not accepted...

Could you help me please?

I am using Matlab 2010a, Win 7 pro and Outlook 2007.

Here is the code:

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');
mail = h.CreateItem('olMail');
mail.Subject = subject;
mail.To = to;
mail.BodyFormat = 'olFormatHTML';
mail.HTMLBody = body;

% 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;
0
Reply Audric 2/27/2011 6:10:21 PM

On 27/02/11 12:10 PM, Audric wrote:
> Hello,
>
> I use a code found on Mathworks.com to send mails via Outlook from my
> matlab code. The synthax to call this function is like sendmail(to,
> subject, body, attachment). The problem I have is that it does not
> accept cell array in my case ! Normally it should work. So for a text of
> several lines. The body = {'line 1', 'lin2', 'lin3'} is not accepted...
>
> Could you help me please?
>
> I am using Matlab 2010a, Win 7 pro and Outlook 2007.
>
> Here is the code:
>
> 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');
> mail = h.CreateItem('olMail');
> mail.Subject = subject;
> mail.To = to;
> mail.BodyFormat = 'olFormatHTML';
> mail.HTMLBody = body;
>
> % 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;

I would point out that the cell array you are passing as the body is 
*not* in HTML format, but the script expects it to be 'olFormatHTML'
0
Reply Think 2/27/2011 6:32:55 PM


On Feb 27, 1:10=A0pm, "Audric " <audricdebaisi...@gmail.com> wrote:
> Hello,
>
> I use a code found on Mathworks.com to send mails via Outlook from my mat=
lab code. The synthax to call this function is like sendmail(to, subject, b=
ody, attachment). The problem I have is that it does not accept cell array =
in my case ! Normally it should work. So for a text of several lines. The b=
ody =3D {'line 1', 'lin2', 'lin3'} is not accepted...
>
> Could you help me please?
>
> I am using Matlab 2010a, Win 7 pro and Outlook 2007.
>
> Here is the code:
>
> 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 =3D actxserver('outlook.Application');
> mail =3D h.CreateItem('olMail');
> mail.Subject =3D subject;
> mail.To =3D to;
> mail.BodyFormat =3D 'olFormatHTML';
> mail.HTMLBody =3D body;
>
> % Add attachments, if specified.
> if nargin =3D=3D 4
> for i =3D 1:length(attachments)
> mail.attachments.Add(attachments{i});
> end
> end
>
> % Send message and release object.
> mail.Send;
> h.release;

the body has to be a string character. If you have multiple lines,
with each line as a char in a cell then instead of passing the cell
array pass this:

sprintf('%s\n', body{:})

which adds a newline to the end of each element of the body cell
array.

/reza
0
Reply reza 2/27/2011 6:37:19 PM

Thanks for these quick replies.
With a combination of your two answers I manage to solve my problem.

Only strings are accepted with html tags. So the right thing to do is to place
For multiple lines body: 'line 1 <br /> line 2 <br /> line 3'. 
For multiple recipients: 'recipient1; recipient2; recipient3'

Thanks and regards,
0
Reply Audric 2/27/2011 7:11:04 PM

Hi, I have the same problem. Could you explain what you meant by :
or multiple lines body: 'line 1 <br /> line 2 <br /> line 3'

How did you change your code?

Thanks

Peter
0
Reply ppl4world (5) 4/15/2011 11:49:20 PM

"Audric" wrote in message <ike42c$1vc$1@fred.mathworks.com>...
> Hello,
> 
> I use a code found on Mathworks.com to send mails via Outlook from my matlab code. The synthax to call this function is like sendmail(to, subject, body, attachment). The problem I have is that it does not accept cell array in my case ! Normally it should work. So for a text of several lines. The body = {'line 1', 'lin2', 'lin3'} is not accepted...
> 
> Could you help me please?
> 
> I am using Matlab 2010a, Win 7 pro and Outlook 2007.
> 
> Here is the code:
> 
> 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');
> mail = h.CreateItem('olMail');
> mail.Subject = subject;
> mail.To = to;
> mail.BodyFormat = 'olFormatHTML';
> mail.HTMLBody = body;
> 
> % 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;

Like Peter, I would also like to know how you modified the sendolmail function to be able to send a cell array as the message body? Thank you in advance!
0
Reply Tom.Cheng (2) 3/9/2012 4:58:12 PM

Here is my solution

% mailbody is my string with multiple lines,  I need to send

mailbody = cellstr(mailbody) % to get a vector of 1 line string
mailbody2 = '';
for i = 1:numel(mailbody)
      mailbody2 = cat(2, mailbody2, char(mailbody(i)), '<br />');
end

%Then you can send it with Sendolmail(to, mailsubject, mailbody, attachments)
0
Reply audricdebaisieux (8) 3/9/2012 5:30:20 PM

"Audric" wrote in message <jjdenc$ks6$1@newscl01ah.mathworks.com>...
> Here is my solution
> 
> % mailbody is my string with multiple lines,  I need to send
> 
> mailbody = cellstr(mailbody) % to get a vector of 1 line string
> mailbody2 = '';
> for i = 1:numel(mailbody)
>       mailbody2 = cat(2, mailbody2, char(mailbody(i)), '<br />');
> end
> 
> %Then you can send it with Sendolmail(to, mailsubject, mailbody, attachments)

Your code works flawlessly, thank you for your help Audric!

One thing I would say though is to make sure to pass the argument "mailbody2" instead of "mailbody" into the sendolmail function, for anyone else that's reading.
0
Reply Tom.Cheng (2) 3/15/2012 2:31:35 PM

7 Replies
270 Views

(page loaded in 0.123 seconds)

Similiar Articles:













7/21/2012 11:15:57 PM


Reply: