← servoy magazine

[Tip] Email plugin: dynamic SMTP authentication

  • Tips
[image unavailable: Serverpic]

by David Workman
Sotlex, Inc

Do you need to send mail with more than one SMTP server per Servoy server? As it turns out, the latest version of the mail plugin allows you to override the Server mail plugin settings for SMTP authentication when you use the sendMail function in a method.

Here's the format for sendMail. Notice the last input parameter:

boolean sendMail( to, from, subject, msgText, [cc], [bcc], [attachement/attachments array], [overridePreferenceSMTPHost/properties array])

The sample code for the function does not show what format the properties array for the SMTP host override should be. Here's an example that shows how to do this:

var authorization = new Array("mail.smtp.host=[smtp mail server]", "mail.smtp.auth=true",
                    "mail.smtp.username=[user name]", "mail.smtp.password=[password]");

var emailSend = "[email address]";
var emailFrom = "[email address]";
var emailSubject = "SMTP help";
var emailMsg = "test code";

var success = plugins.mail.sendMail(emailSend, emailFrom, emailSubject, emailMsg, null, null, null, authorization );

By switching out the values that this method recieves, you can control which SMTP mail server is used at the client level at runtime.

Comments (4)

Graham Greensall
An update on using multiple SMTP accounts with the Send Mail plugin. It works brilliantly - although I've lost some hair in the process so here's some gotcha's to avoid. 1 Assuming you are getting the SMTP account details from stored fields (and not hard coding) be sure to close the inverted commas "" after the = sign var authorization = new Array("mail.smtp.host=" + smtp mail server, "mail.smtp.auth=true", "mail.smtp.username=" + user name, "mail.smtp.password=" + password); 2 Send Mail will fail if Subject or MsgText are empty. Either error trap or add a space ie: MsgText += ' ' 3 Send Mail failure messages can be found in the Server Log. Finally, double-check that your Users have entered their passwords correctly before you spend 2hrs debugging & testing your code trying to work out why its failing Confused
David Workman
It will also fail if any of your email addresses are not formatted correctly. One of the many functions in Marcel's indispensable tools plugin validates email addresses: http://www.kabootit.com/home/2005/01/news_new_releas.html
Dean Westover
This article and comments are helpful and very much appreciated! Another article with sample code for the handling of attachments would also be good. Thank you!
David Workman
Harjo to the rescue. Attachment tip here: <a href="http://www.kabootit.com/home/2005/07/tip_email_plugi_1.html">http://www.kabootit.com/home/2005/07/tip_email_plugi_1.html</a>