SMTP for Gmail tutorial

  1. If you don’t have one, register a GMail account or setup your domain for Google applications.
  2. Download a recent version of PHPMailer (I used the version 5.02)
  3. Check with your web hosting provider if port 465 (TCP out) is open, if not ask him to open that port
  4. Include the PHPMailer class file:require_once('phpmailer/class.phpmailer.php');
  5. Create those two constant variable to store your GMail login and password. Use the login for your Google application mail if you have one.
    define('GUSER', 'you@gmail.com'); // Gmail username
    define('GPWD', 'password'); // Gmail password
  6. Use the following function to send mail messages (add the function in one of your included files):
    function smtpmailer($to, $from, $from_name, $subject, $body) { 
    	global $error;
    	$mail = new PHPMailer();  // create a new object
    	$mail->IsSMTP(); // enable SMTP
    	$mail->SMTPDebug = 0;  // debugging: 1 = errors and messages, 2 = messages only
    	$mail->SMTPAuth = true;  // authentication enabled
    	$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
    	$mail->Host = 'smtp.gmail.com';
    	$mail->Port = 465; 
    	$mail->Username = GUSER;  
    	$mail->Password = GPWD;           
    	$mail->SetFrom($from, $from_name);
    	$mail->Subject = $subject;
    	$mail->Body = $body;
    	$mail->AddAddress($to);
    	if(!$mail->Send()) {
    		$error = 'Mail error: '.$mail->ErrorInfo; 
    		return false;
    	} else {
    		$error = 'Message sent!';
    		return true;
    	}
    }

    Most of the setting inside the function are required by GMail. While searching for tutorials I found articles with different settings for the port and security. My advice use them as in this tutorial.

  7. Call the function within your code:
    smtpmailer('to@mail.com', '', 'from@mail.com', 'yourName', 'test mail message', 'Hello World!');

    Use this more “advanced” usage inside your application:

    if (smtpmailer('to@mail.com', 'from@mail.com', 'yourName', 'test mail message', 'Hello World!')) {
    	// do something
    }
    if (!empty($error)) echo $error;

Advanced setup with fall-back SMTP server

Because of the limit it might be useful to use a secondary SMTP server if the Gmail option didn’t send the message. For this functionality we need to replace the part with the SMTP settings a little bit. First create login/server variables for the 2nd SMTP server:

define('SMTPUSER', 'you@yoursmtp.com'); // sec. smtp username
define('SMTPPWD', 'password'); // sec. password
define('SMTPSERVER', 'smtp.yoursmtp.com'); // sec. smtp server

Next we need to create a if/else statement using the variables for the second server (replace).

function smtpmailer($to, $from, $from_name, $subject, $body, $is_gmail = true) { 
	global $error;
	$mail = new PHPMailer();
	$mail->IsSMTP();
	$mail->SMTPAuth = true; 
	if ($is_gmail) {
		$mail->SMTPSecure = 'ssl'; 
		$mail->Host = 'smtp.gmail.com';
		$mail->Port = 465;  
		$mail->Username = GUSER;  
		$mail->Password = GPWD;   
	} else {
		$mail->Host = SMTPSERVER;
		$mail->Username = SMTPUSER;  
		$mail->Password = SMTPPWD;
	}        
	$mail->SetFrom($from, $from_name);
	$mail->Subject = $subject;
	$mail->Body = $body;
	$mail->AddAddress($to);
	if(!$mail->Send()) {
		$error = 'Mail error: '.$mail->ErrorInfo;
		return false;
	} else {
		$error = 'Message sent!';
		return true;
	}
}

And use the function now as followed:

$msg = 'Hello World';
$subj = 'test mail message';
$to = 'to@mail.com';
$from = 'from@mail.com';
$name = 'yourName';

if (smtpmailer($to, $from, $name, $subj, $msg)) {
	echo 'Yippie, message send via Gmail';
} else {
	if (!smtpmailer($to, $from, $name, $subj, $msg, false)) {
		if (!empty($error)) echo $error;
	} else {
		echo 'Yep, the message is send (after hard working)';
	}
}

 

Leave a comment