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
Sukhjinder Bansil
Courses Plus Student 22,505 PointsTrying to set up a working email, with SMTP Postmarkapp, but nothing displays when I've sent the email
Hi All this is my first experience with PHP and I am trying to set up a contact page with a third party mail server. The mail server I am using is Postmarkapp and every time I submit the form a blank index.php page returns, instead of the contact.php?status=thanks page. Any help or advice will be much appreciated as I am currently bamboozled!! Here is the code below:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = trim($_POST["name"]);
$email = trim($_POST["email"]);
$message = trim($_POST["message"]);
if($name == "" OR $email == "" OR $message =="") {
echo "You must specify a value for name, email address and message.";
exit;
}
foreach( $_POST as $value ){
if( stripos($value,'Content-Type:') !== FALSE ){
echo "There was a problem with the information you entered.";
exit;
}
}
if ($_POST["address"] != "") {
echo "Your form submission has an error.";
exit;
}
require_once("inc/phpmailer/class.phpmailer.php");
$mail = new PHPMailer(); // defaults to using php "mail()"
if (!$mail->ValidateAddress($email)){
echo"You must specify a vaild email address.";
exit;
}
$email_body = "";
$email_body = $email_body . "Name: " . $name . "<br>";
$email_body = $email_body . "Email: " . $email . "<br>";
$email_body = $email_body . "Message: " . $message;
$mail->isSMPT();
$mail->SMTPAuth = true;
$mail->Host = 'smtp.postmarkapp.com';
$mail->Username = 'myusername';
$mail->Password = 'mypassword';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->SetFrom($email, $name);
$address = "sukhjinder.bansil@gmail.com";
$mail->AddAddress($address, "Sukh Bansil");
$mail->Subject = "Sukh Bansil Web Developer Form Submission | " . $name;
$mail->MsgHTML($email_body);
if(!$mail->Send()) {
echo "There was a problem sending the email: " . $mail->ErrorInfo;
exit;
}
header("Location: contact.php?status=thanks");
exit;
}
?><?php
include('inc/header.php');
include('inc/body.php'); ?>