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 Simple PHP Application Wrapping Up The Project Sending The Contact Form Email

Brenda Krafft
Brenda Krafft
18,036 Points

Should the PHPMailer code work on a Windows machine?

I have my Shirts4Mike project up and running, with the PHPMailer code in place. I get no errors, but no email actually gets sent.

I think I read somewhere that Windows doesn't have an email server, so I'm in the process of reading up on how to set one up.

But my husband says that he can put code into an Excel sheet and get it to send an email, so I shouldn't need to do this.

Do I need to set up a separate email server in order to send email from my code on a Windows machine?

3 Answers

Codin - Codesmite
Codin - Codesmite
8,600 Points

PHPmailer does require a websever it can speak to via SMTP. If you are on Windows Server you can set the Mailserver up on your local machine or alternatively you can point PHPmailer to any other SMTP server including free SMTP offered by some free email companies such as gmail.

Excel also requires an SMTP connection setup in the VB script, maybe one is already configured on the excel sheet your husband uses, but it is still required.

To use gmail as a SMTP server:

<?php
require 'PHPMailerAutoload.php';

$mail = new PHPMailer;

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

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = "smtp.gmail.com";                       // sets GMAIL as the SMTP server
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = "yourusername@gmail.com";           // GMAIL username
$mail->Password = "yourpassword";                     // GMAIL password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

$mail->setFrom('from@example.com', 'Mailer');
$mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
$mail->addAddress('ellen@example.com');               // Name is optional
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');

$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

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

This is a free service availible to anyone with a GMAIL account (which is free).

My guess is that you are on a Windows operating system and running a program like XAMPP to have a local operating environment. If so, it is possible to configure XAMPP to send email although I don't know how to do it. I had a similar issue with the Slim course. It worked fine in a live environment. You can also setup PHPMailer to send through gmail.

Brenda Krafft
Brenda Krafft
18,036 Points

Thank you so much. Excellent responses. I'm glad to know that I'm not wasting my time setting this us.