Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

vbscript - Send email to mailgun with attachment from classic asp

I am trying to get an old application from classic asp to send emails through mailgun api. I can get it to where it will send a regular email with no attachments but I can not get it to work when I am trying to send as multipart/form-data. It errors out with no detailed error response.

Here is my latest attempt of the function to send the email.

Function SendMailSync(toAddress, fromAddress, subject, body, htmlBody, attachment)
    Dim httpPostData
    Dim mailGunMessageUrl
    Const MAILGUN_BASE_URL = "https://api.mailgun.net/v3/www.domain.com"
    Const MAILGUN_API_KEY = "myapikey"
    httpPostData = "from=" & fromAddress
    httpPostData = httpPostData & "&to=" & toAddress
    httpPostData = httpPostData & "&subject=" & subject
    httpPostData = httpPostData & "&text=" & body
    httpPostData = httpPostData & "&html=" & htmlBody
    if attachment <> "" then
        httpPostData = httpPostData & "&attachment=" & attachment
    end if
    sBoundary = String(6, "-") & Replace(Mid(CreateObject("Scriptlet.TypeLib").GUID, 2, 36), "-", "")
    set http = CreateObject("MSXML2.ServerXMLHTTP.6.0")
    mailGunMessageUrl = MAILGUN_BASE_URL & "/messages"
    http.Open "POST", mailGunMessageUrl, false, "api", MAILGUN_API_KEY
    http.setRequestHeader "Content-Type", "multipart/form-data; boundary=" & sBoundary
    http.setRequestHeader "Authorization", "Basic AUTH_STRING"
    http.Send httpPostdata
    If http.status <> 200 Then
        Response.Write "An error occurred: " & http.responseText
    End If
    SendMailSync = http.responseText
    Set http = Nothing
End Function

Here is the function where it will send a basic email with no attachment.

Function SendMailSync(toAddress, fromAddress, subject, body, htmlBody)
    Dim httpPostData
    Dim mailGunMessageUrl
    Const MAILGUN_BASE_URL = "https://api.mailgun.net/v3/www.domain.com"
    Const MAILGUN_API_KEY = "myapikey"
    httpPostData = "from=" & fromAddress
    httpPostData = httpPostData & "&to=" & toAddress
    httpPostData = httpPostData & "&subject=" & subject
    httpPostData = httpPostData & "&text=" & body
    httpPostData = httpPostData & "&html=" & htmlBody
    set http = CreateObject("MSXML2.ServerXMLHTTP.6.0")
    mailGunMessageUrl = MAILGUN_BASE_URL & "/messages"
    http.Open "POST", mailGunMessageUrl, false, "api", MAILGUN_API_KEY
    http.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    http.setRequestHeader "Authorization", "Basic AUTH_STRING"
    http.Send httpPostdata
    If http.status <> 200 Then
        Response.Write "An error occurred: " & http.responseText
    End If
    SendMailSync = http.responseText
    Set http = Nothing
End Function

Does anyone see what else I need to tweak in order to get the multipart/form-data to send.

question from:https://stackoverflow.com/questions/65925134/send-email-to-mailgun-with-attachment-from-classic-asp

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...