1

嗨,我是 php 和邮戳的新手,正在尝试将表单提交设置到我的电子邮件中。我有电子邮件工作,但我无法让它显示标题(“位置:thanks.php)页面。任何帮助将不胜感激。谢谢。

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;
    }

}
4

2 回答 2

1

大概$postmark->send()在它工作时返回true。您的 if/then 语句说“发送成功时退出”。

如果您将header()调用移到 if/then 中,它应该可以按预期工作。您还需要处理$postmark->to调用失败的情况,此时可能会重定向到错误页面。

于 2015-02-06T20:02:59.087 回答
1

使用我们新的官方支持的库可能更容易,它提供了 API 调用响应的完整详细信息。http://developer.postmarkapp.com/developer-official-libs.html#php

于 2015-02-09T18:31:13.603 回答