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 trialJoshua Farley
11,312 PointsPostmark App Configuration
I am using a class.phpmailer.php file from Google to send email through a contact form and relaying that through Postmark App. I am using DreamHost as the host.
I have setup the Postmark server and have connected the DNS TXT data from Postmark to the server and Postmark has confirmed connection.
However, I am getting an error when I try to submit the form:
"There was a problem sending the email: The following From address failed: planner@domain.com : Called Mail() without being connected."
It seems to be trying to pull the regular PHP mailer without using the SMTP function. Here is the relevant code, provided the class.phpmailer.php file doesn't need to be edited in any way. This is from the process.php file:
<?php
require_once("class.phpmailer.php");
$mail = new PHPMailer(); // defaults to using php "mail()"
if (!$mail->ValidateAddress($email)){
echo "You must specify a valid email address.";
}
$email_body = "";
$email_body = $email_body . "Name: " . $name . "<br />";
$email_body = $email_body . "Phone: " . $phone . "<br />";
$email_body = $email_body . "Email: " . $email . "<br />";
$email_body = $email_body . "Message: " . $message;
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = "smtp.postmarkapp.com";
$mail->Port = 26;
$mail->Username = "##############################";
$mail->Password = "##############################";
$mail->SetFrom("planner@domain.com", $name);
$address = "customer@email.com";
$mail->AddAddress($address, "Message");
$mail->Subject = "Contact from your website contact form | " . $name;
$mail->MsgHTML($email_body);
if(!$mail->Send()) {
echo "There was a problem sending the email: " . $mail->ErrorInfo;
exit;
}
header ("Location: contact-thanks.html");
exit;
?>
2 Answers
Joshua Farley
11,312 PointsThank you for the help everyone! Everything from the Comments on this helped. The main thing was the autoloader, security, port, and debug that helped. Here is the final script that made the contact form work with Postmark:
<?php
require_once 'PHPMailerAutoload.php';
$mail = new PHPMailer(); // defaults to using php "mail()"
if (!$mail->ValidateAddress($email)){
echo "You must specify a valid email address.";
}
$email_body = "";
$email_body = $email_body . "Name: " . $name . "<br />";
$email_body = $email_body . "Phone: " . $phone . "<br />";
$email_body = $email_body . "Email: " . $email . "<br />";
$email_body = $email_body . "Message: " . $message;
//Mail sending data
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = "smtp.postmarkapp.com";
$mail->Username = "##############################";
$mail->Password = "##############################";
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;
$mail->SMTPDebug = 1;
$mail->SetFrom("from@email.com", $name);
$address = "customer@email.com";
$mail->AddAddress($address, "Message");
$mail->Subject = "Contact from your website contact form | " . $name;
$mail->MsgHTML($email_body);
if(!$mail->Send()) {
echo "There was a problem sending the email: " . $mail->ErrorInfo;
exit;
}
header ("Location: contact-thanks.html");
exit;
?>
thomascawthorn
22,986 PointsIs it trying to pull the regular php mailer because that's the file you're including?
<?php
require_once("class.phpmailer.php");
When you say you got the php mailer class from Google - is that a google search for from google themselves?
If the origin of the mailer class doesn't matter, jump onto github and pull in the latest version which includes an autoloader to automatically pull in the required mailer classes.
You shouldn't have to include specific mailer classes if you pull in the autoloader. This is from their github page:
<?php
require 'PHPMailerAutoload.php';
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user@example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;
If you're still getting the same error after this change, you'll know the bug is not a result of using the wrong mailer class.
Joshua Farley
11,312 PointsI just changed it to the autoloader and got the same result.
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer(); // defaults to using php "mail()"
if (!$mail->ValidateAddress($email)){
echo "You must specify a valid email address.";
}
?>
thomascawthorn
22,986 Pointscurses!
thomascawthorn
22,986 PointsAre you working locally? I've been googling, but there seems to be a wide variety of issues ranging from port number to hosting provider.
Does your hosting plan allow it?
Joshua Farley
11,312 PointsI set the port to 587. I also added in the TLS encryption. This sent the email. However, it doesn't show up in the target email specified in the Postmark settings. The form processes like normal. I went to the Postmark site and it shows that the email was received by Postmark, but it is not showing up in the email. Any suggestions?
Tom Cawthorn : I have never had an issue like this until switching to DreamHost, but using a 3rd party emailer is better practice anyways. DreamHost says that they do allow PHP mail as long as the from address is from a DreamHost email account. However, I believe that this is why it was not working in the first place. DreamHost has most of its servers blacklisted.
thomascawthorn
22,986 PointsHmm, if the message has made it as far as Postmark then it sounds like an issue at their end or with the receiving mailbox classifying it as junk etc... I couldn't tell you, but it's probably best to raise a ticket with Postmark because chances are they would've seen this before. You could also ask a new question on the Treehouse forum :-)
In your 'best answer' code, you're still adding in $mail->SMTPDebug = 1. It's probably best to remove this unless you're specifically debugging the mailer - i.e. you don't want it knocking around in production!
Happy New Year :-)