View Full Version : using email_send_mandrill()


08-27-2013, 05:00 PM
Woul like to report on this new function. After a suggestion from Selwyn, I tried it.

In my scenario, I have printed invoices with a barcode. Once printed, they must go to proofing before they can be sent.

After they are proofed, there will often be a big stack - maybe 50 to 100 that they want to send.

So the operater scans them with a barcode reader (to a temporary file) and then I send them all at once.

We were running into issues with the provider, basically smtp issues, using either Outlook or Alpha's email.

I signed up for Mandrill, got a key, etc. Then I tested sending 100 emails. Worked flawlessly. For 100, takes maybe 60 seconds.

So I can highly recommend it - to get instructions look in the release notes, and search for Mandrill.

PS: the first 12,000 emails each month are free! After that it is a pittance for the cost.

12-28-2013, 12:01 PM

It has been 4 months since you started using Mandrill for your email. Are you still satisfied with them? We are struggling to find a reasonable email service that allows our officers (we are non-profit educational association) to send out monthly newsletters, etc.

We are divided into districts that may have as many as 500 members each. The issue we are experiencing is that sending out such an email triggers SPAM designations, etc. We use iContact for bulk emails to all 5,000 members with no problems. We tried having Zebrahost develop a live connection to our database using their Email Marketer product but they were unable to make it work.

We are looking for a solution that allows smaller email sets to be sent out (50 to 500) based upon a grid search of WITHOUT triggering SPAM classifications. Would Mandrill do the trick (based upon your experience)


12-28-2013, 12:16 PM
Yes, completely satisfied. I also have a private version of the code, that will most likely run on almost any verson of Alpha, like 10.5 for example. However, that vesrion was privately produced, and not available, free.

Most Alpha clients would never exceed 12,000 per month, but even if you do, it costs a pittance per thousand. Most companies using Mandrill are sending massive amounts of emails.

Because you are bypassing your email provider, the number you send at one time or per month is irrelavent. Will not trigger spam issues - which is why I went to it.

12-28-2013, 12:28 PM

Thank you for the speedy reply! I appreciate you taking the time during the holiday season to give the feedback.

Mandrill sounds like the way to go for our needs. We are running V12 on the WAS.

You mention you have privately produced code that is not available for free. Is it available for purchase from the developer? (either you or the person/company who provided it for you) If so, I would be interested in discussing it further. Feel free to contact me via private message to discuss the details.

Thank you again and Happy New Year!

02-17-2014, 01:58 AM
Hi there,
I have looked in the wiki and also in the alpha anywhere help but was not able to find the documentation to use the
email_send_mandrill() function. Would you be able to tell me where i can find this function and how to use it?
Thank you so much your help is greatly appreciated.

02-17-2014, 02:10 AM
I just found it...


Email - Sending Email Using the Mandrill Email Service - Alpha Anywhere has always offered Xbasic functions to send email. However, these functions require that you have access to a SMTP mail server. The existing functions in Xbasic include:

With the emergence of email services, such as Mandrill (www.mandrillapp.com) an alternative, possibly easier, certainly more powerful, way of sending emails from your applications is now possible.
A new function,

allows you to send email using the Mandrill service.
Benefits of using a 3rd party emailing service include the easier setup, better deliverability, and access to powerful value added features offered by the service, such as tracking whether people open your email message, etc.
The full functionality of the Mandrill email service can be read by navigating to this address:
In order to use the Mandrill email server, you will have to visit their web site and apply for a key. The key allows you to send a certain number of free emails each month. Beyond that, there is a fee.
NOTE: Alpha Software is not involved in any way at all in the fee and does not receive any payment at all if you use the Mandrill service.
There are two different ways in which you can use the email_send_mandrill() function.
1. Simple Method: Define Message Using Xbasic Dot Variable
In the simple method, you define an Xbasic dot variable that defines the properties of the message you want to send and then you call email_send_mandrill(), passing in your Mandrill key and the dot variable. This simple method does not expose all of the functionality of the Mandrill service, but it is very easy to set up and use, and it is great for simple email messages.
For example:

'create a .dot variable to define the message
dim ms as p
ms.send_to = "john@acme.com:John Smith,sally@acme.com:Sally Jones"
ms.send_to_cc = ""
ms.send_to_bcc = ""
ms.from_email = "sales@alpha.com"
ms.subject = "Information You Requested"
ms.message_html = "Here is the <b>information</b> you requested."
ms.message_text = "Plain text version of the message"
ms.attachments = "c:\files\mychart1.pdf,c:\files\mytext1.txt"

Notes About the Properties in the Dot Variable
The .send_to and .send_to_cc addresses are a comma delimited list of addresses. The email address is followed (optionally) by a colon and then a friendly name.
The .send_to_bcc property allows you to specify a single bcc address. If you specify more than one address, you will get an error.
.send_to_cc and .send_to_bcc are optional. You can omit them entirely.
.attachments is optional. It contains a comma delimited list of filenames to attach to the email.
CC and BCC Addresses
Even though the Dot variable allows you to specify separate comma delimited lists for the TO and CC addresses, in reality, Mandrill does not support CC addresses. Furthermore, Mandrill only supports a single BCC address.
If you specify any CC addressesl, the TO list and the CC list of addresses is combined into a single list and the emails are sent to all addresses in the combined list. However, the Mandrill internal .preserve_recipients property is set to false, so that none of the recipients will see the names of any other recipients.
If you provided a list of names in the TO list, and no names in the CC list, the recipients of the email would all be able to see the names of the other recipients.
Once you have defined the .dot variable, you can send the message. For example

dim pResult as p
pResult = email_send_mandrill("mysecretkey",ms)

The function returns a .dot variable with several properties:
.error - a logical value .t. or .f.
.result - an Xbasic array with one item for each address in the recipient list
.json - the JSON settings that were constructed from the .dot variable passed into the function. You can use this value as a starting point should you wish to use the second method (described below) for calling the email_send_mandrill() function
Here is what a typical item in the .result array looks like:
result[1].email = "john@acme.com"
result[1].status = "sent"
result[1]._id = "some message id" --- used to make queries against the Mandrill API for message status

Here is a complete example of sending an email in the simplest possible way (eliminating all optional properties in the Xbasic .dot variable). As you can see, it just a few lines of code.
'create a .dot variable to define the message
dim ms as p
ms.send_to = "john@acme.com:John Smith,sally@acme.com:Sally Jones"
ms.from_email = "sales@alpha.com"
ms.subject = "Information You Requested"
ms.message_html = "Here is the <b>information</b> you requested."
dim pResult as p
pResult = email_send_mandrill("mysecretkey",ms)

Alternative Method for Specifying Attachments - Specifying the Attachment Data
We have previously indicated that attachments are specified by setting the .attachments property to a comma delimited list of filenames. An alternative method is to set the .attachmentsArray property as a property array with .name, .type and .content properties.
.name - the filename (no drive/path, just the name and extension) of the file.
.type - the mime type
.content - the base64 encoded data

For Example
dim ms.attachmentsArray[1] as p
ms.attachmentsArray[1].name = "chart.pdf"
ms.attachmentsArray[1].type = resolve_mime_type("pdf")
ms.attachmentsArray[1].content = base64encode(file.to_blob("c:\myfiles\chart.pdf"))

Merge Variables - Mail Merge Variables into the Message Body and Subject
An advanced feature of the Mandrill service is to allow you to specify placeholders in the message subject and body that will be replaced with variables that you supply at the time Mandrill sends out the email to each recipient.
The format for the placeholders is:

For example, here is how the message subject or body could have been specified using placeholders:
ms.subject = "*|Fname|*, here is the Information You Requested"
ms.message_html = "*|Fname|* *|Lname|* here is the information you requested."

When you use placeholders in the subject or body, you must specify the placeholder values. Obviously, you need to specify placeholder values for each recipient.
For example, assume that you had specified two recipients as follows:
ms.send_to = "john@acme.com:John Smith,sally@acme.com:Sally Jones"

You would then need to specify the FName and LName variables for john@acme.com and for sally@acme.com.
Here is how you would do this:

'define the .merge_vars[] array with 2 items
dim ms.merge_vars[2] as p

'define the actual variables for the first recipient.
'since there are two variables, we define a sub-array with two items
dim ms.merge_vars[1].vars[2] as p

'here are the variables for the first recipient
ms.merge_vars[1].vars[1].name = "FName"
ms.merge_vars[1].vars[1].content = "John"
ms.merge_vars[1].vars[2].name = "LName"
ms.merge_vars[1].vars[2].content = "Smith"
ms.merge_vars[1].rcpt = "john@acme.com

'here are the variables for the second recipient
dim ms.merge_vars[2].vars[2] as p
ms.merge_vars[2].vars[1].name = "FName"
ms.merge_vars[2].vars[1].content = "Sally"
ms.merge_vars[2].vars[2].name = "LName"
ms.merge_vars[2].vars[2].content = "Jones"
ms.merge_vars[2].rcpt = "sally@acme.com

In addition to supplying merge variables for each recipient, you can also supply global merge variables - for all recipients. You do this by specifying the .global_merge_vars property array. If a merge variable is defined at the individual recipient level, it will override the corresponding global merge variable.
For example:
dim ms.global_merge_vars[1] as p
ms.global_merge_vars[1].name = "Company"
ms.global_merge_vars[1].content = "Alpha Software"

Using a DataSource Property
Instead of supplying the actual email addresses and merge variables directly in the dot variable you can pass in a 'dataSource' (an array of JSON objects), and then indicate that the .send_to property should be populated from the data source.

For example, consider the following definition of the .dataSource property:
ms.dataSource = <<%txt%
{email: "sam@acme.com", firstName: "Sam", lastname: "Smith"},
{email: "joe@acme.com", firstName: "Joe", lastname: "Jones"}

If the above .dataSource property has been defined, then you can define the .sent_to property as follows:

ms.send_to = "{datasource:email}"

This indicates that the .send_to property should be populated from the 'email' property in the dataSource.
If you pass in a .dataSource property, then there is no need to pass in the .merge_vars property - these properties are automatically generated from the data in the .dataSource property.
Here is what happens if you pass in a .dataSource property: The .message_html, .message_text and .subject properties are scanned to see if any merge-variables are used. If any merge variables are found in any of these properties, then the merge variables for each address are automatically extracted from the .dataSource property.
Using the .dataSource property, it is extremely easy to create a mass mailing with custom merge variables for each recipient.

2. Advanced Method: Define Message Using a JSON String
The JSON method exposes the full functionality of the Mandrill service. To see all of the options that are available, please see the Mandrill API documentation.
If you want to use advanced features, like track opens, or schedule delivery, you will need to use this option.
Here is an example of how you can use this method:

DIM json as C = <<%str%
"message": {
"to": [
"email": "john@acme.com",
"name": "John Smith"
"from_email": "Sales@alpha.com",
"subject": "Information You Requested",
"html": "Here is the <b>information</b> you requested",
"text": "Plain Text Message",
"preserve_recipients": false
"asynch": false,
"ip_pool": null,
"send_at": null
dim pResult as p
pResult = email_send_mandrill("mysecretkey",json)

Generating the JSON String From Xbasic
Xbasic has powerful functions for generating JSON from an Xbasic dot variable. For example, the json_generate() function will generate JSON from a .dot variable. A powerful way to programmatically construct the JSON string that you pass into the email_send_mandrill() function is to build up an Xbasic .dot variable with the various properties that you want to set, then call json_generate() to generate the JSON.

02-17-2014, 02:11 AM
Did you get all of that???


03-30-2015, 10:47 AM
This is great thanks for posting this. Have you figured out how to get an error to the user if the email fails?
I have been playing with it a bit but nothing yet.

03-30-2015, 11:20 AM
Here is a function I use for Mandrill

function mandrill_send as L(subject,message,attachment)
if HOSTAVAILABLE("smtp.mandrillapp.com",587)=.t.
timedout=timedout + 1
goto wasbusy
' mandrill_send=.f.
' exit function
end if
dim shared myaddress as c
dim shared emailto as c
dim shared attachment as c
dim shared subject as c
dim shared message as c
dim usename as c = "redacted"
dim pass as c = "redacted"
dim mailfrom as c = "smtp.mandrillapp.com"
dim ps as p
dim pm as p
dim pm.subject as c
dim pm.message as c
dim pm.html_message as c
subject="Prime Aerospace Testing Requirements"
if (email_smtp_open(ps, mailfrom, 587, usename, pass)) then
pm.to = var->emailto
pm.from = var->myaddress
pm.subject = var->subject
pm.message = var->message
pm.attachments = var->attachment
email_smtp_send(pm, ps)
exit function
end if
end function

It the sender does not have a good and reliable broadband provider with a decent amount of broadband speed, then you may periodically lose contact with the internet temporarily and get a fail - if that happens, (if it returns false) tell the operator what happened and to run it again. If the hostavailable fails tell them they don't have internet connectivity.

03-30-2015, 06:33 PM
Hi Chad,
I have not personally figured out a way to get an error to the user.
While I have written and tested this code in another post,
I have not yet used it on a production basis yet.
I have been using Mandrill now for about 6 months through an eMail relay server and that has always worked great.
The amount of emails I send through the relay server is only maybe 100 at a pop so pretty simple.
I will be using my other code in a production basis later on sometime in May and your welcome to check in with me again to see how it is working for me.
When I move to the production server I will be ramping up my sending of emails from 100's now to 10,000's and growing to 100,000's per day.
This is where the other code that I wrote will be a huge help in getting those emails out in a minute or two versus days!!