Asif18

{ 66 Comments }

This article is about send emails in SMTP(Simple Mail Transfer Protocol) using phpmailer by own server or gmail server. Every host provider will provide us the mail server and its configurations. SMTP is using for send mails and POP3 is using for receive email to our mail box. Here we are using PHPMailer for send emails using SMTP in php. Every host provider such as godaddy, hostgator, 1&1, Plesk ect has various SMTP configurations. Here is the list of SMTP Server's

send-mails-in-smtp-using-phpmailer-gmail-or-own-server-by-asif18

Create library file for the SMTP Settings 'library.php':

<?php
error_reporting(0);
define("SMTP_HOST", "SMTP_HOST_NAME"); //Hostname of the mail server
define("SMTP_PORT", "SMTP_PORT"); //Port of the SMTP like to be 25, 80, 465 or 587
define("SMTP_UNAME", "VALID_EMAIL_ACCOUNT"); //Username for SMTP authentication any valid email created in your domain
define("SMTP_PWORD", "VALID_EMAIL_ACCOUNTS_PASSWORD"); //Password for SMTP authentication
?>

Create new page called 'sendmail.php' for send SMTP mails:

<form action="" method="post">
    <table>
    <tr>
    	<td><input type="email" placeholder="Email" name="email" /></td>
    </tr>
    <tr>
    	<td><input type="submit" name="send" value="Send via SMTP" /></td>
    </tr>
    </table>
</form>

Make the form post and do the below actions:

<?php
include 'library.php';
include "classes/class.phpmailer.php"; // include the class file name
if(isset($_POST["send"])){
	$email = $_POST["email"];
	$mail	= new PHPMailer; // call the class 
	$mail->IsSMTP(); 
	$mail->Host = SMTP_HOST; //Hostname of the mail server
	$mail->Port = SMTP_PORT; //Port of the SMTP like to be 25, 80, 465 or 587
	$mail->SMTPAuth = true; //Whether to use SMTP authentication
	$mail->Username = SMTP_UNAME; //Username for SMTP authentication any valid email created in your domain
	$mail->Password = SMTP_PWORD; //Password for SMTP authentication
	$mail->AddReplyTo("reply@yourdomain.com", "Reply name"); //reply-to address
	$mail->SetFrom("from@yourdomain.com", "Asif18 SMTP Mailer"); //From address of the mail
	// put your while loop here like below,
	$mail->Subject = "Your SMTP Mail"; //Subject od your mail
	$mail->AddAddress($email, "Asif18"); //To address who will receive this email
	$mail->MsgHTML("<b>Hi, your first SMTP mail has been received. Great Job!.. <br/><br/>by <a href='http://asif18.com'>Asif18</a></b>"); //Put your body of the message you can place html code here
	$mail->AddAttachment("images/asif18-logo.png"); //Attach a file here if any or comment this line, 
	$send = $mail->Send(); //Send the mails
	if($send){
		echo '<center><h3 style="color:#009933;">Mail sent successfully</h3></center>';
	}
	else{
		echo '<center><h3 style="color:#FF3300;">Mail error: </h3></center>'.$mail->ErrorInfo;
	}
}
?>

Mostly SMTP's are using for send Bulk emails. There are many bulk email providers such as Amazon SES(Simple Email Service). We can use Amazon SES, Google mail server etc. to send SMTP mails via google means just change the SMTP settings like below,

<?php
include "classes/class.phpmailer.php"; // include the class name
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // 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; // or 587
$mail->IsHTML(true);
$mail->Username = "mohamedasif18@gmail.com";
$mail->Password = "**********";
$mail->SetFrom("anyemail@gmail.com");
$mail->Subject = "Your Gmail SMTP Mail";
$mail->Body = "<b>Hi, your first SMTP mail via gmail server has been received. Great Job!.. <br/><br/>by <a href='http://asif18.com'>Asif18</a></b>";
$mail->AddAddress("mohamedasif18@gmail.com");
 if(!$mail->Send()){
	echo "Mailer Error: " . $mail->ErrorInfo;
}
else{
	echo "Message has been sent";
}
?>

Note : Gmail is using SSL for security. for this you need to enable "extension=php_openssl.dll" in your php.ini file, if you are using ubuntu here you can go! the above gmail code will works in ubuntu without any php.ini modifications.

If you want to send emails to many without knowing others email address like newsletter and subscription emails replace these lines after '$mail->Subject = "Your SMTP Mail";',

$recipients = array(
   'asif18@asif18.com' => 'Asif 18',
   'mohamedasif18@gmail.com' => 'Mohamed Asif'
);
foreach($recipients as $email => $name){
	$mail->AddAddress($email, $name); //To address who will receive this email
	$mail->MsgHTML("<b>Hi, your first SMTP mail has been received. Great Job!.. <br/><br/>by <a href='http://asif18.com'>Asif18</a></b>"); //Put your body of the message you can place html code here
	$mail->AddAttachment("images/asif18-logo.png"); //Attach a file here if any or comment this line(using absolute path), 
	$mail->Send(); //Send the mails
	// if you want to does not show other users email addresses like newsletter, daily, weekly, subscription emails means use the below line to clear previous email address
	$mail->ClearAddresses();
}

$mail->ClearAddresses(); will clear the previous email address while sending bulk emails like newsletters, email subscriptions. By this other users email addressess will not be shown on their Mailbox 'To' field. If you want to show all the emails to other users means just comment or remove the line.

Download files below,

Comments (66)