I'm trying to use the Sendgrid v3 API to send mail, using a template, personal data and one or more attachements. Been wretling with the attachement for days now, and can't seem to get around the problem of encoding the PDF to JSON and include the entire content of the PDF. When I test, I get the mail and there's an attachment to it. But just 400 bytes and Adobe thinks it's unreadable. When opening it in a text editor, the PDF contains the file name. "So, close, but no cigarr..."
Here's my code, minus the API key:
<%
Session.LCID=1053
dagen = FormatDateTime(Now,2)
bilaga1 = "D:\\www5.volvobil.net\KC-Admin-sg\docs\bilaga13.pdf"
Function ReadFile(sfilepath)
Const adTypeText = 2
Const adTypeBinary = 1
Set B64Code = CreateObject("ADODB.Stream")
b64Code.Open
testCode = b64Code.LoadFromFile(sfilepath)
b64Code.Position = 0
b64Code.Type = adTypeText
b64Code.CharSet = "us-ascii"
dim bd
bd = b64Code.ReadText
B64Code.Close
ReadFile = Base64Encode(bd)
End Function
Function Base64Encode(sText)
Dim oXML, oNode
Set oXML = CreateObject("Msxml2.DOMDocument.3.0")
Set oNode = oXML.CreateElement("base64")
oNode.dataType = "bin.base64"
oNode.nodeTypedValue = Stream_StringToBinary(sText)
Base64Encode = oNode.text
Set oNode = Nothing
Set oXML = Nothing
End Function
Function Base64Decode(ByVal vCode)
Dim oXML, oNode
Set oXML = CreateObject("Msxml2.DOMDocument.3.0")
Set oNode = oXML.CreateElement("base64")
oNode.dataType = "bin.base64"
oNode.text = vCode
Base64Decode = Stream_BinaryToString(oNode.nodeTypedValue)
Set oNode = Nothing
Set oXML = Nothing
End Function
Private Function Stream_StringToBinary(Text)
Const adTypeText = 2
Const adTypeBinary = 1
Dim BinaryStream 'As New Stream
Set BinaryStream = CreateObject("ADODB.Stream")
BinaryStream.Type = adTypeText
BinaryStream.CharSet = "us-ascii"
BinaryStream.Open
BinaryStream.WriteText Text
BinaryStream.Position = 0
BinaryStream.Type = adTypeBinary
BinaryStream.Position = 0
Stream_StringToBinary = BinaryStream.Read
Set BinaryStream = Nothing
End Function
Private Function Stream_BinaryToString(Binary)
Const adTypeText = 2
Const adTypeBinary = 1
Dim BinaryStream 'As New Stream
Set BinaryStream = CreateObject("ADODB.Stream")
BinaryStream.Type = adTypeBinary
BinaryStream.Open
BinaryStream.Write Binary
BinaryStream.Position = 0
BinaryStream.Type = adTypeText
BinaryStream.CharSet = "us-ascii"
Stream_BinaryToString = BinaryStream.ReadText
Set BinaryStream = Nothing
End Function
toppbilden = "<img src=https://static.volvobil.se/images/mailutskick/600_header2_island.jpg alt=head border=0 />"
mailrubrik = "Byte av bil / Replacement of Car"
rubriken = "VÄLKOMMEN ATT BYTA DIN BIL"
enrubriken = "Welcome to replace your car (English version below)"
portalen = "<A HREF=https://intranet.volvocars.net/volvo-car-group/hr-portal/Pages/Company-cars-general-information-Sweden-Swedish.aspx>My Employment</A>"
portalenen = "<a href=https://intranet.volvocars.net/volvo-car-group/hr-portal/Pages/Company-cars-general-information-Sweden.aspx>My Employment</a>"
template = "d-3be42a9ce1db4461b2f72256fbb198eb"
filen = ReadFile(bilaga1)
filename = "bilaga13.pdf"
Response.Write "Filenamn = "& filename &" Fil = "& filen
data = "{""from"":{""email"":""[email protected]""},""personalizations"":[{""to"":[{""email"":"""& epost &"""}],""dynamic_template_data"":{""receipt"":true,""name"":"""& namnet &""",""hallen"":"""& hallen &""",""regno"":"""& regno &""",""oldregno"":"""& oldregno &""",""bilen"":"""& bilen &""",""dagtiden"":"""& dagtiden &""",""levgubbe"":"""& levgubbe &"""}}],""attachments"": [{""content"": """& filen &""",""filename"":"""& filename &""",""type"":""application/pdf""}],""template_id"":"""& template &"""}"
link = "https://api.sendgrid.com/v3/mail/send"
Dim oXMLHTTP
Set oXMLHTTP = CreateObject("Msxml2.ServerXMLHTTP.6.0")
if oXMLHTTP is nothing then Set oXMLHTTP = CreateObject("Microsoft.XMLHTTP")
oXMLHTTP.Open "POST", link, False
oXMLHTTP.setRequestHeader "Content-Type", "application/json;charset=UTF-8"
oXMLHTTP.setRequestHeader "Authorization", "Bearer <API-key>"
oXMLHTTP.send data
If oXMLHTTP.Status = 200 Then
PostData = oXMLHTTP.responseText
Else
response.Write "Status: " & oXMLHTTP.Status & " | "
response.Write oXMLHTTP.responseText
End If
SET oXMLHTTP = NOTHING
SET FormConad = NOTHING
Set objFSO = Nothing
Set objFileOut = Nothing
Set objXML = Nothing
Set objDocElem = Nothing
Set objStream = Nothing
%>
The output is the base64 code on a web page and Status: 400 | {"errors":[{"message":"Bad Request","field":null,"help":null}]} from Sendgrid.
Nothing wrong with the attached file, I've tried a few.
I'd be very grateful for your input.
Thanks,
Hasse
Looks like are streaming the content of the file as Text and not Binary.
To summarise;
bilaga1
variable should not containD:\\
as that will likely cause theLoadFromFile()
method to error. It should beD:\
to be a valid file path.ADODB.Stream
should be usingadTypeBinary
notadTypeText
.As a side-note:
Here is a function I wrote for a SendGrid Library I built a little while ago, should give you some idea of how to add attachments.
The code is part of a class so some elements like
m_attachments
(which is aScripting.Dictionary
that stores the attachments against the class) will not be accessible from this sample. The idea is to show you how theADODB.Stream
should be implemented.Here is the
MailAttachment
class.