Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Using Postmark with PHPMailer?

I'm attempting to use Pastmark as a mail server for my site (to manage any incoming messages from potential clients). However, I can't figure out using any of the resources on Treehouse how to actually make this work. What I'd like is to receive the emails sent from my contact form to my personal email, however this is proving to be more difficult that I thought.

I keep running into a problem with the From Signature. I get an error on Postmark like this: "Sender Signature not defined for From address". I've tried several things but none of them seem to be working. Additionally the docs on Postmark don't really seem helpful. Here's the PHP code from my site that I am using to communicate with Postmark.

    // require phpmailer
    require("includes/phpmailer/PHPMailerAutoload.php");
    //require("includes/phpmailer/class.phpmailer.php");

    // create phpmail
    $mail = new PHPMailer;

    // mail server - postmark setup
    $mail->IsSMTP();
    $mail->Host = "smtp.postmarkapp.com";
    $mail->SMTPAuth = true;
    $mail->Username = "myPostmarkAPIKey";
    $mail->Password = "myPostmarkAPIKey";
    $mail->Port = 2525;

    // email contents setup
    $mail->setFrom($email, $name);
    $mail->addAddress('mypostmarkstuff@inbound.postmarkapp.com', 'Wilson Gardner');
    $mail->isHTML(false);
    $mail->Subject = $subject;
    $mail->Body = $email_body;

    if(!$mail->send()) {
      echo("<script>alert('There was an error. " . $mail->ErrorInfo . "')</script>");
      exit;
    }
    header("location:contact-page.php?status=received");
  }

2 Answers

So I'm answering this question myself since I figured things out myself and if anyone is on the same thought train as me that might benefit them. The issue is this...

1) You MUST have an email that corresponds with your website domain (AKA it can't be public). That means no @yahoo.com or @live.com or @hotmail.com etc. It's not too awfully difficult to do that. You're not required to do so when you create a website, it's sort of an extra. I use GoDaddy, and the process was super easy. The only irritating thing about it is that you have to go through some extra steps in order to receive the emails sent to your new email on your devices (phones, tablets, and desktop apps).

2) Once you have this email, you must set the From field in your email to YOUR OWN email. That's super confusing, yes, but just follow my lead. The reasoning behind this is that Postmark will be receiving these emails from your site. It wants to know (always) where they are coming from. You have to use YOUR email with YOUR domain in the $mail->setFrom() field.

Example: $mail->setFrom("thisismyemail@mysitesdomain.com");
(This is YOUR email that you can register as Sender Signature with Postmark). This will get rid of any errors that might have been occurring.

I didn't get this for a while but it sort of makes sense now.

3) Now you're receiving emails, but where do the emails go? This is where you set the $mail->addAddress() field. You want to send it to either that same email (what I ended up doing) or another email where YOU WILL RECEIVE ALL EMAILS SENT FROM YOUR WEBSITE. This may seem redundant, but you're using this email to send from your website to the Postmark server up to this point. Now you need somewhere for it to go. If you use the email provided by the client, well, it gets sent to the client (that's really not helpful to them or you). So here you'll just have to be somewhat redundant and send the email to yourself. THIS IS WHY IT IS IMPORTANT TO INCLUDE THE CLIENTS EMAIL IN YOUR SUBJECT! You will just have to copy and paste the client's email from the subject of the email you received from your site into a response email that you will send to them with your response.

Overall, it's a little more work than if it were to go directly to your email from the clients email to your email, but the truth is that these days email servers reject something like 80-90% of emails because it gets flagged as spam. If you start receiving emails from your site without using a third party mail server, you could potentially have issues with your email client filtering emails from customers/clients and trashing them.

Any additional questions about what I discussed, just shoot me an email or message or post here on the forum!

Hi Wilson,

I don't have any experience with postmark but I found this old question that had a similar error message as you reported: https://teamtreehouse.com/community/phpmailer-and-postmark-confusion

Hampton's link doesn't work but Daniel's comment may get you pointed in the right direction.

I found that in a google search for: phpmailer and postmark

Several other treehouse links came up. If this link doesn't help you then you may want to look through all the others if you haven't already.