Categories
Articles

How to Send Email From XAMPP using localhost on a Mac and your Gmail Account


By Lon Hosford

This is sample code for using SMTP and your Google GMail account with XAMPP installed on a Mac and PHPMailer.xampp_in_envelope_150x96
Often you want to send mail from your web site to yourself from a user form or even to others. But you may be using XAMPP as a local host and want to test that without having to upload to a public server. This script shows how you can do that with PHPMailer.

Step 1: Install XAMPP MacOS

Step 2: Download PHPMailer [ad name=”Google Adsense”]

Step 3: Create a testing php file shown below. Be sure you have the correct paths to class.phpmailer.php and class.smtp.php on lines 3 and 4 that you downloaded with PHPMailer.

<!--?php 
include("class.phpmailer.php"); 
include("class.smtp.php"); 
function test_gmail_smtp_basic() {
 	// Uncomment as needed for debugging
 	//error_reporting(E_ALL);
  	//error_reporting(E_STRICT);
 	// Set as needed
	date_default_timezone_set('America/New_York');
	$mail = new PHPMailer(); 
	// Optionally get email body from external file
 	$body = file_get_contents('contents.html');
 	$body = eregi_replace("[\]",'',$body);
  	$mail->IsSMTP();                            // telling the class to use SMTP
	$mail->Host       = "smtp.gmail.com";       // SMTP server
	$mail->SMTPDebug  = 2;                      // enables SMTP debug information (for testing)
                                                    // 0 default no debugging messages
                                                    // 1 = errors and messages
                                                    // 2 = messages only
	$mail->SMTPAuth   = true;                   // enable SMTP authentication
	//$mail->SMTPSecure = 'ssl';                // Not supported
	$mail->SMTPSecure = 'tls';                  // Supported
	$mail->Host       = "smtp.gmail.com";       // sets the SMTP server
	$mail->Port       = 587;                    // set the SMTP port for the GMAIL server
	$mail->Username   = "me@gmail.com";         // SMTP account username (how you login at gmail)
	$mail->Password   = "mygmailpassword";      // SMTP account password (how you login at gmail)

	$mail->setFrom('mememe@gmail.com', 'Joe Dirt');

	$mail->addReplyTo('mememe@gmail.com', 'Joe Dirt");

	$mail->Subject    = "PHPMailer Test Subject via smtp, basic with authentication";

	$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

	$mail->msgHTML($body);

	$address = "someone-else@their-email-domain.com";
	$mail->addAddress($address, "Miss Piggy");
	// if you have attachments
	$mail->addAttachment("phpmailer.gif");      // attachment 
	$mail->addAttachment("phpmailer_mini.gif"); // attachment

	if(!$mail->Send()) {
	  echo "Mailer Error: " . $mail->ErrorInfo;
	} else {
	  echo "Message sent!";
	}
}
// Test the connection
test_gmail_smtp_basic();
?>

What you may find is an issue with many examples heretofore is that SMTPSecure shown on line 25 needs to be TLS (Transport Layer Security) instead of SSL (Secure Sockets Layer). The other item is the port number is 587 as you see on line 29. You can disable line 29 once you have this fully debugged. Use with care. 

These images come with PHPMailer and are included here for your convenience.

phpmailer.gif

phpmailer.gif

Optional external html file for the body content. See line 12 in the code above.

<body style="margin: 10px;">
<div style="width: 640px; font-family: Arial, Helvetica, sans-serif; font-size: 11px;">
<div align="center"><img style="height: 90px; width: 340px;" alt="" src="phpmailer.gif" /></div><br>
<br>
&nbsp;This is a test of PHPMailer.<br>
<br>
This particular example uses <strong>HTML</strong>, with a <div> tag and inline<br>
styles.<br>
<br>
Also note the use of the PHPMailer logo above with no specific code to handle
including it.<br />
Included are two attachments:<br />
phpmailer.gif is an attachment and used inline as a graphic (above)<br />
phpmailer_mini.gif is an attachment<br />
<br />
PHPMailer:<br />
Author: Lon Hosford (somebody@no.net)
</div>
</body>

[ad name=”Google Adsense”]