Search code examples
coldfusionmailgun

Cfhttp and mailgun api


I’ve been trying to configure Mailgun API with Coldfusion's Cfhttp and can’t figure out the format to pass the variables. I configured it in about 5 minutes with Python, and have to roll it to a Coldfusion front end. I’ve used the same basic structure as I did in Python, and it all works great in Python.

I’ve been able to make a connection, but I have not been able to send mail. I know it has to do with the structure, and I can’t find a working example anywhere.

How do you build the cfhttp to pass the required data? Is the data=data I would use in the request in Python the same as a cfhttpparam in ColdFusion? And is it a header, or a body?

My Coldfusion code is below, i've replaced secret information with domain.com


<cfscript>
mailgun_variables = '{
    "company_id": 1008,
    "company_contact": "Joe Smith",
    "random_key": "91A303C8-91FB-AA1D-DE07F18782594721"
    }';

data= '{
    "from": "[email protected]",
    "to": "[email protected]",
    "subject": "Subject line",
    "template": "welcome",
    "o:tag": ["welcome", "welcome-countdown", "welcome-alt", "coldfusion"],
    "h:X-Mailgun-Variables": #mailgun_variables#}';
</cfscript>

<cfhttp url="https://api.mailgun.net/v3/domain.com/messages"
        method="POST"
        username="api"
        password="APIKEY"
        result="response" >
    <cfhttpparam type="header" name="Content-Type" value="application/json" />
    <cfhttpparam type="header" name="data" value="#serializeJSON(data)#">
</cfhttp>

My error message is a 400 Bad request, {"message":"from parameter is missing"}.

Any guidance or links to examples would be much appreciated. Or maybe you can obviously spot what I am doing wrong?


Solution

  • I ended up having to do a lot of trial and error, but have working code if anyone else ever runs into this issue. Mailgun support was no help.

    The answer ended up being that everything had to be passed as cfhttpparam formfields. I also ran into a lot of challenges with tagging and using template variables, but got it all worked out.

    I added it to Github for anyone to use, and will continue to improve it there as I get more in-depth with it.

    https://github.com/convurt/Mailgun-Coldfusion-Sendmail

    cfhttp(method="POST", url="https://api.mailgun.net/v3/DOMAIN.COM/messages", result="response", username="api", password="MAILGUN API KEY") {
    cfhttpparam(name="from", type="formfield", value="#email_from#");
    cfhttpparam(name="h:Reply-To", type="formfield", value="#reply_to#");
    cfhttpparam(name="to", type="formfield", value="#email_to#");
    cfhttpparam(name="subject", type="formfield", value="#email_subject#");
    // loop over tag list, I could not get it to work correctly as a list
    for (t in tags) {
        cfhttpparam(name="o:tag", type="formfield", value="#t#");
    }
    cfhttpparam(name="template", type="formfield", value="#template#");
    // custom paramamter for template
    cfhttpparam(name="t:variables", type="formfield", value="#t_variables#");