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 Build a Basic PHP Website (2018) Adding a Basic Form Utilizing Object Properties and Methods

Jake Ford
Jake Ford
9,230 Points

Using PHPMailer in Windows XAMPP with SMTP to send email

I just wanted to point out that this video doesn't give any hints on what to do if you are NOT using workspaces. It took me forever to figure out how to use SMTP to send mail, but this is my code:

require( 'phpmailer/PHPMailerAutoload.php' );

  $mail = new PHPMailer;

  if(!$mail->validateAddress($email)){
    echo 'Invalid Email Address';
    exit;
  }

  //Creating the email body to be sent
  $email_body = "";
  $email_body .= "Name: " . $name . "\n";
  $email_body .= "Email: " . $email . "\n";
  $email_body .= "State: " . $state . "\n";

  $mail->IsSMTP();
  $mail->SMTPAuth = true;
  $mail->Host = "mail.mydomain.net";
  $mail->Port = 25;
  $mail->Username = "me@mydomain";
  $mail->Password = "password";
  //Sending the actual email
  $mail->setFrom($email, $name);
  $mail->addAddress('me@mydomain.com', 'Me');     // Add a recipient
  $mail->isHTML(false);                                  // Set email format to HTML
  $mail->Subject = 'Calculation form results from ' . $email;
  $mail->Body = $email_body;

  if(!$mail->send()) {
    echo 'Message could not be sent. ';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
    exit;
  }

I had to include the following files in my project:

  • PHPMailerAutoload.php
  • class.smtp.php
  • class.phpmailer.php

In the video she tells you to use require(class.phpmailer.php), but I had to use:

require(PHPMailerAutoload.php) to get it to work.

You can also use this tutorial from treehouse that was in the notes after the video, but would still have to change your require() function to require(PHPMailerAutoload.php):

http://blog.teamtreehouse.com/sending-email-with-phpmailer-and-smtp

I'm still a little confused on why she is not doing it the way the example shows on github:

require(PHPMailerAutoload.php)

https://github.com/PHPMailer/PHPMailer

Simon Coates
Simon Coates
28,694 Points

I'd guess she kept things as simple as possible. Her focus may not have been on the email so much as using as a library and demonstrating the pattern. But i'll admit I had pretty much the same questions and irritation when i tried to use her notes to get email running on my live server.

Jake Ford
Jake Ford
9,230 Points

Yep,

She does kind of explain in the next video that the current settings won't work on a live server or anywhere but workspaces for that matter, but I was trying to get this mail to send before moving onto the next video! They need to update the tutorial she mentions in the notes, too. I just posted this in case anyone else has the same problems that I did in the future.

1 Answer

Awesome!! Thanks for sharing. But also be aware as Alena mentioned, you must be careful using this since your server can get flagged, which is not good at all if that happens.

It is best to use a separate email server.