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

Adam Smallman
Adam Smallman
4,182 Points

Issue with sending email from the form PhP

Hey guys! I have been following the videos, making sure I do it right, but on the final part once all is finished, the form will not send out an email. It comes up with an error. I know that I had to put in a SMTP for gMail or whatever I am using, So I did. Here is the code.

[CODE]<?php if ($_SERVER["REQUEST_METHOD"] == "POST") { $name = trim($_POST["name"]); $email = trim($_POST["email"]); $message = trim($_POST["message"]);

if ($name == "" OR $email == "") {
    echo "Please enter your name and email!";
    exit;
}

foreach( $_POST as $value) {
    if (stripos($value, 'content-type:') !==False){
        echo "There was a problem with the info you provided.";
        exit;
    }
}

if ($_POST["address"] !=="") {
    echo "Your form has an error";
    exit;
}

require_once('inc/class.phpmailer.php');      

$mail = new PHPMailer();

if (!$mail->ValidateAddress($email)){
    echo "You must specify a valid email address.";
    exit;
}

$mail->IsSMTP(); // telling the class to use SMTP

$mail->Host       = "smtp.gmail.com"; // SMTP server

$mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)

// 1 = errors and messages

// 2 = messages only

$mail->SMTPAuth   = true;                  // enable SMTP authentication

$mail->SMTPSecure = "tls";                 // sets the prefix to the servier

$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server

$mail->Port       = 465;                   // set the SMTP port for the GMAIL server

$mail->Username   = "My Email";  // GMAIL username

$mail->Password   = "My Email Password";        

$email_body = "";
$email_body = $email_body . "name: " . $name . "<br>";
$email_body = $email_body . "email: " . $email . "<br>";
$email_body = $email_body . "message: " . $message;

$mail->SetFrom($email, $name);
$address = "my email";
$mail->AddAddress($address, "Lion");
$mail->Subject    = "Lion Form | " . $name;
$mail->MsgHTML($email_body);

if(!$mail->Send()) {
  echo "There was a problem sending the form.: " . $mail->ErrorInfo;
    exit;
  }

} ?>[CODE]

This is the error it comes back with,

SMTP -> ERROR: Failed to connect to server: () The following From address failed: test@gmail.com : Called Mail() without being connected There was a problem sending the form.: The following From address failed: test@gmail.com : Called Mail() without being connected

So there must be something wrong with the SMTP. What it is though I have no idea!

Where I have put myemail or my password I put I am real pass and email! =D Also Where comes back with the error saying Test@gmail.com is what I use to test the form while posting. I have tried real emails too but still the same error just replacing test@gamil.com with whatever I put.

I have tried it on Both the localhost and on a free web host.

It's for a college project ^_^

Thanks guys!

1 Answer

https://support.google.com/mail/answer/78775?hl=en

If you tried configuring your SMTP server on port 465 (with SSL) and port 587 (with TLS), but are still having trouble sending mail, try configuring your SMTP to use port 25 (with SSL).