Sending Mail through PHP Mailer using TLS

<?php
	define('SMTP_SERVER', 'mail.servername.com');
	define('SMTP_PORT', 587);
	define('SMTP_USERNAME', 'user@servername.com');
	define('SMTP_PASSWORD', 'password');
	define('MAIL_ORDERS_ADDRESS', 'user@servername.com');
	define('MAIL_ORDERS_NAME', 'User');

	require_once "PHPMailerAutoload.php";

	try{
		$mail = new PHPMailer(true);
		$mail->IsSMTP();
		$mail->SMTPAuth = true;
		$mail->Host = SMTP_SERVER;
		$mail->Port = SMTP_PORT;
		$mail->Username = SMTP_USERNAME;
		$mail->Password = SMTP_PASSWORD;
		$mail->SMTPSecure = 'tls';
		$mail->SetFrom(MAIL_ORDERS_ADDRESS, MAIL_ORDERS_NAME);
		$mail->Subject = 'Mail test ';
		$mail->MsgHTML('<b>Ahoy</b>');
		$mail->AddAddress('receiver@servername.com', 'Receiver');

		if(!$mail->Send()) throw new Exception($mail->ErrorInfo);
	}
	catch(Exception $e){
		echo $e->getMessage();
	}

?>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s