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

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

<?php
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

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

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

        //Content
//        $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.