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

cathymacars
seal-mask
.a{fill-rule:evenodd;}techdegree
cathymacars
Full Stack JavaScript Techdegree Student 15,941 Points

PHPMailer: Called Mail() without being connected

Hello, I'm trying to send out an email w/ PHPMailer, and although I followed the videos, I can't seem to get it to work.

I have only one input, where the user types in their email, if he/she wants to be notified when whatever. For now all I want, is to receive an email with the email they have input in the field as the message body, so that I can store that to use later. But, when I type in an email (real or not, but always valid) to test that input, I get the following error: "The following From address failed: root@localhost : Called Mail() without being connected". Below, my code:

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

  require_once("includes/phpmailer/class.phpmailer.php");
  $mail = new PHPMailer();

  if (!$mail->ValidateAddress($email)){
    echo'<div class="alert-box">';
      echo'<span>Favor digitar um email válido.</span>';
      echo'<a href="index.php">fechar</a>';
    echo'</div>';
  }

  $mail->IsSMTP();
  $mail->SMTPAuth = true;
  $mail->Host = 'smtp.gmail.com';
  $mail->Port = 465;
  $mail->SMTPSecure = 'tls';
  $mail->Username = "myemail@gmail.com"; // my email which I hope to receive the data inputed on the field
  $mail->Password = "mypassword";

  $mail->SetFrom($email, 'Interessado'); // the email from the person who filled the form, that will be in the body of the message that I will receive
  $address = "myemail@gmail.com"; // my email which I hope to receive the data inputed on the field
  $mail->AddAddress($address, "Fine Design");
  $mail->Subject = "Fine Design - Avise me!";
  $mail->MsgHTML($email);

  if(!$mail->Send()) {
    echo'<div class="alert-box">';
      echo'<span>';
      echo $mail->ErrorInfo;
      echo '</span>';
      echo'<a href="index.php">fechar</a>';
    echo'</div>';
  } else {
    header("Location: index.php?status=thanks");
  }
}?>

Help?

7 Answers

James Barnett
James Barnett
39,199 Points

You've got your port & protocol mismatched. port 465 is for ssl and port 587 is for tls.

Change this:

$mail->SMTPSecure = 'tls';

To this:

$mail->SMTPSecure = 'ssl';

source: http://www.web-development-blog.com/archives/send-e-mail-messages-via-smtp-with-phpmailer-and-gmail/

Casey Ydenberg
Casey Ydenberg
15,622 Points

For the record, from my WAMP, I could only connect to smtp.postmarkapp.com by setting the Port to 2525 and the SMTPSecure to 'starttls'. We'll see what happens when I post the page to Hostmonster ...

James Barnett
James Barnett
39,199 Points

Casey - Many ISPs block port 25, in a futile attempt to stop residential machines infected with malware from being used as part of a spam botnet.

As you've seen you can often use an alternate port, such as 2525.

I'm still not entirely sure how this relates to the Catarina's original question.

Casey Ydenberg
Casey Ydenberg
15,622 Points

The relationship is that I found this post in a google search of the error message, and thought the info might help someone who is in a similar predicament. Thank you for your help (to Catarina and me!)

Casey

mike last
mike last
2,460 Points

Hi I'm using the below code and being told that: Called Mail() without being connected, appart from being in a different order I cant see why my code isn't working and Catarina's is. Could it be to do with gmail application specific passwords? any help appreciated thnx

if(! isset($error_message)) { $email_body = ""; $email_body = $email_body . "Name: " . $name . "<br />"; $email_body = $email_body . "Email Address " .$email . "<br />"; $email_body = $email_body . "Message " . $message . "<br />";

    $mail->SetFrom($email, $name);
    //UPDATE AS NECESERRY
    $mail->Username = 'my@email.address';
    $mail->Password = 'myPassword';
    $address = "my@email.address";
    $mail->AddAddress($address, "Codes Clothes");
    $mail->FromName = 'mike last TEST FORM_TEST_V1';
    $mail->Host = 'smtp.gmail.com';
    $mail->SMTPSecure = 'ssl';
    $mail->Port = 465;
    $mail->IsSMTP();
    $mail->SMTPAuth = true;


    $mail->Subject = "Codes Clothes contact form Submission | " .$name;
    $mail->MsgHTML($email_body);

    $mail->From = $mail->Username;
    if($mail->Send()) {
        header("Location: form_test_v1.php?status=thanks"); //MAKE SURE TO CHANGE THE LOCATION TO YOUR CURRENT PAGE
        exit;
    } 
    else {
        $error_message = "There was a problem sending the email: " . $mail->ErrorInfo;
    }

}