The below Tip describes, How you can send email in Servoy by adding images inline to your message body.
We can create the email attachments by using the createBinaryAttachment() method of the mail plug-in and pass the array of attachments to the sendMail() method to send the mail along with attachments. The code snippet below describes the same…
// creating attachments var attach1 = plugins.mail.createBinaryAttachment( 'flower1.jpg',plugins.file.readFile('c:/temp/flower1.jpg')); var attach2 = plugins.mail.createBinaryAttachment( 'flower2.jpg',plugins.file.readFile('c:/temp/flower2.jpg'));
var msgText = 'This is the mail body.This is some HTML Texts.';
// sending the mail var success = plugins.mail.sendMail( Andrew Pinsky ‘, ‘John Cobb ‘, ‘subject’, msgText,null,null,new Array(attach1, attach2)); if (!success) { plugins.dialogs.showWarningDialog(‘Alert’,’Failed to send mail’,’OK’);
}
Now, the images, we will be sending as attachments to the mail. But, if you want to add these images inline in the body of your mail, then you have to do a little trick. In the SRC attribute of your IMAGE tag, you need to pass a reference to the relevant attachment so that Servoy will modify the header of the attachment to, not to show up the attachments as attachments to the mail. It will be added as an inline to the mail body. You need to add the attachment name where-ever you want in your message body in between “%%” and “%%” (without double quote). The following code snippet reveals the trick.
// creating attachments var attach1 = plugins.mail.createBinaryAttachment( 'flower1.jpg',plugins.file.readFile('c:/temp/flower1.jpg')); var attach2 = plugins.mail.createBinaryAttachment(
'flower2.jpg',plugins.file.readFile('c:/temp/flower2.jpg'));
// adding attachments to the body of the message. by passing the reference
var msgText = ‘This is the mail body.This is some HTML Texts. Here, I am adding my image inline. My another image. ‘;
// sending the mail var success = plugins.mail.sendMail( ´Andrew Pinsky ‘, ‘John Cobb ‘, ‘subject’, msgText,null,null,new Array(attach1, attach2)); if (!success) { plugins.dialogs.showWarningDialog(‘Alert’,’Failed to send mail’,’OK’);
}
Now, the images will be added as inline to your message body.