Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP Enhancing a Simple PHP Application Integrating Validation Errors Displaying the Error Message

Zeljko Porobija
Zeljko Porobija
11,491 Points

There was a problem sending the email: Could not instantiate mail function.

That's the error message I get when I send the mail - whether with valid or invalid mail form. I suppose that it has to do with the inability of my localhost to send mails. Or is there any other reason behind that?

5 Answers

Hi there,

You can employ it on any .php page that you'd like. If you're using variables, then you'd just have to define them before the email body object and that's really the only requirement. Whenever the .php page is loaded, the email code executes with whatever values or body you're assigning - you don't even need variables, just the execute code.

Hi there,

I had this issue with XAMPP locally; try this out in your email code on the line right after $mail->isSMTP();

 $mail->SMTPOptions = array(
      'ssl' => array(
          'verify_peer' => false,
          'verify_peer_name' => false,
          'allow_self_signed' => true
      )
  );
Zeljko Porobija
Zeljko Porobija
11,491 Points

Hi Matthew, the trouble is that I nowhere find the line $mail->isSMTP(). What is the file in question? Thnx in advance for your help!

Here's what my localhost has to send an email and I'm using XAMPP:

 //Send the email
      require 'PHPMailer/PHPMailerAutoload.php';

      $mail = new PHPMailer;

      //$mail->SMTPDebug = 3;                               // Enable verbose debug output

      $mail->isSMTP();                                      // Set mailer to use SMTP

      $mail->SMTPOptions = array(
      'ssl' => array(
          'verify_peer' => false,
          'verify_peer_name' => false,
          'allow_self_signed' => true
      )
  );

      $mail->Host = "your host";  // Specify main and backup SMTP servers
      $mail->SMTPAuth = true;                               // Enable SMTP authentication
      $mail->Username = "your email address";                 // SMTP username
      $mail->Password = "your password";                           // SMTP password
      $mail->SMTPSecure = "tls";                            // Enable TLS encryption, `ssl` also accepted
      $mail->Port = 587;                                    // TCP port to connect to

      $mail->From = "your email address";
      $mail->FromName = "you";
      $mail->addAddress("recipient email");     // Add a recipient


      $mail->isHTML(true);                                  // Set email format to HTML

      $mail->Subject = 'subject!';
      $mail->Body    = "body goes here";
      $mail->AltBody = 'body goes here.';

      if(!$mail->send()) {
          echo 'There was an error sending the contact form email. <br>';
          echo 'Mailer Error: ' . $mail->ErrorInfo;
      } 


      }
}
Zeljko Porobija
Zeljko Porobija
11,491 Points

Well, what I'm trying to comprehend is to which file of the project you particularly refer? I suppose that the code you suggest can be employed somewhere within index.php (in folder contact). Or I need to add another file?