« [News] Yahoo News PR | Main | [tip] How to avoid errors on relations »

July 01, 2005

[Tip] Email plugin: dynamic SMTP authentication

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.

| Posted by David Workman on July 1, 2005 at 01:38 PM in Tips | Permalink

Comments

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

Posted by: Graham Greensall | Jul 7, 2005 7:44:15 AM

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.servoymagazine.com/home/2005/01/news_new_releas.html

Posted by: David Workman | Jul 7, 2005 8:09:55 AM

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!

Posted by: Dean Westover | Jul 13, 2005 11:24:01 PM

Harjo to the rescue. Attachment tip here:

http://www.servoymagazine.com/home/2005/07/tip_email_plugi_1.html

Posted by: David Workman | Jul 15, 2005 3:17:22 PM

Post a comment