Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP

How do I set up PostmarkApp API to send emails?

Well, after working out that sites hosted on GoDaddy cannot use PostmarkApp to send smtp emails, I've attempted to use the PostmarkApp API to send emails.

This actually works, and the email is sent, but instead of redirecting to my 'thanks' page, it redirects to an ugly message ({"To":"cheryl@realsocial.com.au","SubmittedAt":"2015-06-11T20:49:47.4163661-04:00","MessageID":"4dbb201c-bfbb-4b99-9227-d7b647f3810c","ErrorCode":0,"Message":"OK"})

I don't understand any of the code I've used to get this to work, so I have no idea how to fix it (i.e. JSON and CURL)

Here is the website I've followed to get this code: http://www.abeautifulsite.net/sending-emails-with-postmark-and-php/

Here is my code:

define('POSTMARK_API_KEY','MY-KEY');
function send_email($mail) {
  $json = json_encode(array(
    'From' => $mail['from'],
    'To' => $mail['to'],
    'CC' => $mail['cc'],
    'Bcc' => $mail['bcc'],
    'Subject' => $mail['subject'],
    'Tag' => $mail['tag'],
    'HtmlBody' => $mail['html_body'],
    'TextBody' =>$mail['text_body'],
    'ReplyTo' => $mail['reply_to'],
    'Headers' => $mail['headers'],
    'Attachments' => $mail['attachments']
    ));
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, 'http://api.postmarkapp.com/email');
  curl_setopt($ch, CURLOPT_POST, true);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Accept: application/json',
    'Content-Type: application/json',
    'X-Postmark-Server-Token: ' . POSTMARK_API_KEY
    ));
  curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
  $response = json_decode(curl_exec($ch), true);
  $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  curl_close($ch);

  return $http_code === 200;
}

$sent = send_email(array(
  'to' => 'cheryl@myemail.com',
  'from' => 'Me <cheryl@myemail.com>',
  'subject' => "Free Market Report Form Submission | " . $name,
  'html_body' => $email_body
  ));
//Did it send successfully?
if(!$sent) {
  echo 'The email could not be sent';
} 

header("Location: index.php?status=thanks");
exit;

It would be so great if you could help me out! Thanks

OK, I think I've managed to answer my own question! Would still love to hear if I've done this incorrectly or if there is a better way to do something. I've added line 2 so that curl isn't echoed out.

$ch = curl_init();
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_URL, 'http://api.postmarkapp.com/email');
  curl_setopt($ch, CURLOPT_POST, true);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Accept: application/json',
    'Content-Type: application/json',
    'X-Postmark-Server-Token: ' . POSTMARK_API_KEY
    ));
  curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
  $response = json_decode(curl_exec($ch), true);
  $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  curl_close($ch);

  return $http_code === 200;
}