In a recent discussion regarding automating e-mails in Access someone brought to my attention that Windows 10 has curl built into it, as of build 1803, which allows sending e-mails, amongst many other things.
This got me curious if it could be a viable replace for say CDO Mail. As such, I did some research, development and testing and ended up with the following function:
'---------------------------------------------------------------------------------------
' Procedure : curl_SendEmail
' Author : Daniel Pineault, CARDA Consultants Inc.
' Website : http://www.cardaconsultants.com
' Purpose : Send e-mail via Windows 10 curl API
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
' (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-sa/4.0/
' Req'd Refs: Late Binding -> none required
' Requirements: Windows 10 V1803 or later
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sSTMP : SMTP server to be used to send the e-mail from
' mail.servername.com
' sPortNo : Port to be used
' 465, 587
' sAccountUserName : SMTP server username
' sAccountPassword : SMTP server password
' sFrom : E-mail address sending the email
' sTo : E-mail address of the recipient
' sFile : Template file of the e-mail message to be sent
'
' Usage:
' ~~~~~~
' ? curl_SendEmail("mail.servername.com", 465, "user@servename.com", "mypassword", _
' "me@servername.com", "you@servername2.com", "C:\email.txt")
'
' Revision History:
' Rev Date(yyyy-mm-dd) Description
' **************************************************************************************
' 1 2021-06-08 Initial Release
'---------------------------------------------------------------------------------------
Function curl_SendEmail(ByVal sSTMP As String, _
ByVal sPortNo As Integer, _
ByVal sAccountUserName As String, _
ByVal sAccountPassword As String, _
ByVal sFrom As String, _
ByVal sTo As String, _
ByVal sFile As String)
On Error GoTo Error_Handler
Dim sCmd As String
'Build the curl command
'**************************************************
sCmd = "curl" & _
" --max-time 10" & _
" --ssl-reqd" & _
" --url ""smtps://" & sSTMP & ":" & sPortNo & "/""" & _
" --user """ & sAccountUserName & ":" & sAccountPassword & """" & _
" --mail-from """ & sFrom & """" & _
" --mail-rcpt """ & sTo & """" & _
" --upload-file """ & sFile & """"
' Debug.Print sCmd
'Run the curl command
'**************************************************
' CreateObject("Wscript.Shell").Run "cmd /k " & sCmd, 0, False 'Hide window from user / Production
CreateObject("Wscript.Shell").Run "cmd /k " & sCmd, 1, True 'Display window to user / Development
Error_Handler_Exit:
On Error Resume Next
Exit Function
Error_Handler:
MsgBox "The following error has occured" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Source: curl_SendEmail" & vbCrLf & _
"Error Description: " & Err.Description & _
Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
, vbOKOnly + vbCritical, "An Error has Occured!"
Resume Error_Handler_Exit
End Function
Plain Text Email Body
and the email.txt file used as the –outload-file in the function above resembles:
From: SenderEmailAddress To: RecipientEmailAddress Subject: Your Subject Line Date: Tue, 8 Jun 2021 17:18:39 -0400 Content-Language: en-us Your Actual E-mail Body.
HTMLText Email Body
And for HTML messages you can do something like the following for the –outload-file in the function
From: SenderEmailAddress To: RecipientEmailAddress Date: Tue, 8 Jun 2021 17:18:39 -0400 Content-Language: en-us MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/html; charset="UTF-8" Subject: Your Subject Line Your Actual HTML E-mail Body.
but you can customize these settings depending on your needs, character set, …
My Conclusions
Although the above is quite simple, I did try countless variations, to try and return the output from the curl command, but nothing worked. I tried outputting to a file using the DOS redirect ‘>textfile’ technique, the clipboard, waiting for the Shell instance to no longer be busy… nothing worked. Even the –output in curl itself failed to generate/populate a text file for me to be able to read. Thus, IMHO, this is yet another poorly implemented utility and offers us no automated way to know if it executed properly or not, unlike say CDO Mail. Thus, it isn’t something I’d recommend in any production system that you need to count on.
I finally managed to get the output redirected and it can be done, not just with the standard command prompt >, but rather 2> (prefixing it with 2 will cause it to redirect/output the stderr rather than stdout). So for instance:
sCmd = "curl" & _
" --max-time 10" & _
" --ssl-reqd" & _
" --url ""smtps://" & sSTMP & ":" & sPortNo & "/""" & _
" --user """ & sAccountUserName & ":" & sAccountPassword & """" & _
" --mail-from """ & sFrom & """" & _
" --mail-rcpt """ & sTo & """" & _
" --upload-file """ & sFile & """ 2> C:\Temp\MyOutputFile.txt"
Another option would be 2>&1, but I haven’t tested that one yet.
As to why curl’s –o doesn’t work, no clue!
Also, as explained to me in a forum: “you can never get the SMTP delivery XXX reason code. All you’ll ever get is success/failure about whether the message has queued or not.” somewhat limiting it’s production value IMHO.
One thing I was impressed with was the execution speed of curl. It was lightning fast to send e-mails and also did not suffer from any security pop-ups and other nonsense we now experience with automating Outlook.
I still am not done though and plan on working on this further as I find it very hard to believe Microsoft has implemented such a tool without any way of getting a response from it.
>> I find it very hard to believe Microsoft has implemented such a tool without any way of getting a response from it <<
cURL is not a MS product!
I haven't tested, but you might want to try with the -v/–verbose switch, or -t/–trace.
Interested to see what you find out.
Keep up the great work,
d
Sadly, neither helped with the matter. You could be right, it could be MS’ implementation that is the root cause of the issue. How frustrating. They add a tool, but cripple it in a way that it because pretty much useless.
I recently downloaded and tried the standalone curl 7.77.0 and it made no difference, I could not get the output to a file, thus I am unable to retrieve the results through automation which is very frustrating. So I can easily launch a command, have it display to the users (in the command prompt), but seem to have no way to actually capture the output and report it back into Access.
For now, this is where my adventure into curl ends.
Maybe the issues you describe have something to do with Microsoft. I use CuRL from MS Access to send html requests to an MMS service, sending both SMS and MMS messages and receiving result data. It has been working for several years without issue. I installed CuRL myself so maybe that’s the difference.
“receiving result data”
in Access? or in the Command Prompt? Could your elaborate a little? Share the command you use (omitting any confidential info)?
The above is regarding sending e-mails, perhaps I should explore other uses to see if the issue is specifically regarding e-mailing.
The MS cURL version is a limited. Try using the latest version from the following site:
https://curl.se/download.html
I haven’t tested it, but Patrick Headley said he receives result data with that site’s version using windows scripting runtime to create a shell object with the following vba call:
lngTextSuccess = objShell.Run(strCurlCommand, 0, True)
hth