Sending mail with PHP Mailer from your domain email address

Sending mail with PHP Mailer from your domain email address
Sending email is important if you have any blog or website to communicate with your readers or users. There is a simple PHP function from where you can easily send an email form your server which is
mail($to, $subject, $message, $headers);
You can send email with this function for simple use but if you want more functionalities like sending email with smtp, with username, password you should use other mail scripts like PHP Mailer.

In today's post we will discuss how to send email with PHP Mailer step by step.

First download the latest script from following link.
Put this script in the directory from where you want to send the email. Now you will be having a folder with name phpmailer where you putĀ  your downloaded script. Now create a PHP file and write the following lines.
$To = '';	// receiver's email address
$Subject = 'Subject of mail';
$Name = "Sender's Name";
$Message = "Add your email text";
$From = '';  // Sender's email address

sendPhpMail($To,$Subject,$Message,$Name, $From );
Now we will write function sendPhpMail(). which will be as following

function sendPhpMail($To,$Subject,$Message,$Name, $From){
	$server = "Your SMTP Server";    // SMTP server
        $SenderName = "Sender Name"; 
        $SenderEmail = "Your website mail address";  // SMTP account username
        $Password = "Password of your email address"; // SMTP account password

        $mail             = new PHPMailer();
	$body             = $Message;
	$mail->IsSMTP(); // tell class to use SMTP
	$mail->Host       = $server; 
	$mail->SMTPDebug  = false; // enables SMTP debug information (for testing)
                                      // 1 = errors and messages
				      // 2 = messages only
	$mail->SMTPAuth   = true;    // enable SMTP authentication
	$mail->Host       = $server;
	$mail->Username = $SenderEmail; 
	$mail->Password   = $password;   
	$mail->SetFrom($SenderEmail, $SenderName);
	$mail->Subject    = $Subject;
	$address = $To;
	if($attachment!='') {
	if(!$mail->Send()) {
		return false;
  //	  echo "Mailer Error: " . $mail->ErrorInfo;
	} else {
		return true;
//	  echo "Message sent!";
Before writing function snippet we will add the php file from the folder we downloaded which is class.phpmailer.php. In this function we will define some variables in which we defined our SMTP values, you can get them from your server admin panel like cPanel.

Recommended: Configure Email client from cPanel

Here we added our server name, our domail email address and user name of your website and password of email we entered before. After that you have few options which you can change according to your server requirements. Like if you are using godaddy server you do not have to use
$mail->SMTPAuth   = true;
You can skip this line. If you are having any problem with sending mail you can activate the debug option like this
$mail->SMTPDebug  = true; // 1 for errors and messages and 2 for messages only
Once your debug is complete, make it false again.
Tags: PHP Mailer
comments powered by Disqus