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


How can I send an email with phpmailer?

I have a contact form and set the action to load my php script that handles the form data. When I hard code the email address that the form needs to go to - it works. When I try to use the variables that I create from the php script, it doesn't work. Even though I keep getting a 'Message has been sent' message... I don't know what to do

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'phpmailer/src/Exception.php';
require 'phpmailer/src/PHPMailer.php';
require 'phpmailer/src/SMTP.php';

if(isset($_POST)) {

$customerName = $_POST['name'];
$customerEmail = $_POST['email'];
$customerMessage = $_POST['message'];

echo $customerName;
echo $customerEmail;
echo $customerMessage;

try {       
$mail = new PHPMailer(true);                              // Passing `true` enables exceptions

        $mail->setFrom($customerEmail, $customerName);
        $mail->addAddress('example@gmail.com', 'Kristian');     // Add a recipient
        $mail->addReplyTo($customerEmail, $customerName);

//        $mail->addAttachment('');         // Add attachments

//        $mail->isHTML(true);                                  // Set email format to HTML
        $mail->Subject = 'Message from CC website';
        $mail->Body    = $customerMessage;
//        $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

        if($mail->send()) {
        echo 'Message has been sent';
        } else {
            echo "Not sent";
} catch(Exception $e) {
    echo $e->getMessage();
} else {
    echo "Variables not set";


2 Answers

Bapi Roy
Bapi Roy
14,237 Points

Hey Kristian Woods,

I assume you are going to use SMTP. To do so you need to set SMTP config above "//Recipients"

 $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'user@example.com';                 // SMTP username
    $mail->Password = 'secret';                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                                    /

I'm just running this from my local environment. I haven't got servers set up. I don't know what information to enter. Thanks for helping out, Bapi.

Bapi Roy
Bapi Roy
14,237 Points

SMTP server information can be obtained from your email service provider.