1

Hi I'm new to php and postmark and am trying to get a form submission set to my email. I have the email working however I can't get it to show the header("Location: thanks.php) page. Any help would be greatly appreciated. Thanks.

require("postmark.php");

$postmark = new Postmark("API KEY","calvin.hemington@example.com","$email");

if($postmark->to("calvin.hemington@example.com")->subject("Mission Woodshop | " . $name)->plain_message($email_body)->send()){
    exit;
}


header("Location: thanks.php");
exit;

<?php

/**
 * This is a simple library for sending emails with Postmark created by Matthew Loberg (http://mloberg.com)
 */

class Postmark{

    private $api_key;
    private $data = array();

    function __construct($apikey,$from,$reply=""){
        $this->api_key = $apikey;
        $this->data["From"] = $from;
        $this->data["ReplyTo"] = $reply;
    }

    function send(){
        $headers = array(
            "Accept: application/json",
            "Content-Type: application/json",
            "X-Postmark-Server-Token: {$this->api_key}"
        );
        $data = $this->data;
        $ch = curl_init('http://api.postmarkapp.com/email');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        $return = curl_exec($ch);
        $curl_error = curl_error($ch);
        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        // do some checking to make sure it sent
        if($http_code !== 200){
            return false;
        }else{
            return true;
        }
    }

    function to($to){
        $this->data["To"] = $to;
        return $this;
    }

    function subject($subject){
        $this->data["subject"] = $subject;
        return $this;
    }

    function html_message($body){
        $this->data["HtmlBody"] = "<html><body>{$body}</body></html>";
        return $this;
    }

    function plain_message($msg){
        $this->data["TextBody"] = $msg;
        return $this;
    }

    function tag($tag){
        $this->data["Tag"] = $tag;
        return $this;
    }

}
  • 1
    If the "send" is successful, you are just "exiting" your code from inside the "if" block. The header(...) call needs to be inside that block. – Matt Runion Feb 06 '15 at 20:00
  • Calvin, can you link to the "postmark.php" file/library you're using to access Postmark? As others have the "exit" line inside of the if block appears to be exiting the script before you have sent the Location header, but it is not possible to verify this without looking at the source code for "postmark.php" – Andrew Theken Feb 06 '15 at 20:55
  • I posted the post.php file. The send works without any errors it just isn't directing to the thanks.php file even when I put the header("Location:thanks.php") inside the if statement. – Calvin Hemington Feb 07 '15 at 02:33

2 Answers2

1

Presumably $postmark->send() returns true when it works. Your if/then statement says 'exit when the send succeeds'.

If you move the header() call into the if/then it should work as expected. You'll also want to handle the case where the $postmark->to call fails, possibly redirect to error page at that point.

LG_PDX
  • 834
  • 8
  • 12
1

It may be easier to use our new officially supported lib which gives full details on responses for API calls. http://developer.postmarkapp.com/developer-official-libs.html#php

JP Toto
  • 1,032
  • 9
  • 10