Jump to content
xisto Community
Sign in to follow this  
sagardash

Php Send Mail Through Phpmailer sending mail through phpmailer

Recommended Posts

Can anyone here please tell me how to send mail through PHPMailer in php ;) I have search in many source code on web and cant find anything...please help me...

 

i am also unsuccessful in sending mail through SMTP in windows platform....

because i dont know weather there is requirement of any smtp server software in windows platform or not...

if require then how to configure that..what will be free software

 

 

please help me i am new to PHP...

Share this post


Link to post
Share on other sites

I am not familiar with PHPMailer (program) but the standard SMTP settings are:INCOMING MAIL SERVER : mail.your_domain.com [or subdomain]OUTGOING MAIL SERVER : mail.your_domain.com [or subdomain]Outgoing PORT : 25Incoming PORT : 110USERNAME : THE-USERNAME-YOU-SET-UP-IN-CPANEL@your_domain.com [ Setup mails in the ADD remove mails in Cpanel ]PASSWORD : [The password that you setup in Cpanel ]If you just want to send PHP mail from your web script, simply use the mail() function, such thatmail($to, $subject, $message, $headers)where, $to, $subject, $message, $headers are standard PHP handlers.

Share this post


Link to post
Share on other sites

Right I understand what you are saying. First of all just use the basic PHP send mail function, no need for phpmailer, however if you still want to use it then you will need to include the OpenSSL extension in your php installation.

The reason the basic php send mail function won't work is because you need an smtp server running in the background. Download the free one from here http://www.softstack.com/freesmtp.html. Simply open the software and there you go, you don't need to do anything else.

Now you can use the php send mail function easily using port 25!

Now if you REALLY want to use phpmailer after that then here's a tutorial I wrote about 2 years ago for another site that might help you out with learning how the software works and how it's configured, however if you're one of these people that want it quick and easy and don't want to bother learning then I suggest you ignore the tutorial.

Creating and Using a Site Configuration File

One of the things I like to do when I build a site is to create a configuration file that handles miscellaneous settings that I may need over and over again. So, I create a file called config.php in /home/mywebsite/public_html/config.php and I set it up with an array called $site with my keys and values the settings I use in the site. In this tutorial, I will cover how to define some settings we will use for the PHPMailer extender class. Here’s a view of my configuration file: config.php

<?php   // Configuration settings for My Site   // Email Settings   $site['from_name'] = 'My Name'; // from email name   $site['from_email'] = 'email@mywebsite.com'; // from email address   // Just in case we need to relay to a different server,   // provide an option to use external mail server.   $site['smtp_mode'] = 'disabled'; // enabled or disabled   $site['smtp_host'] = null;   $site['smtp_port'] = null;   $site['smtp_username'] = null;   ?>
The previous example should be very self explanatory, so we’ll move on and cover those settings later on when we start to use them.


The PHPMailer Extender Class

First, I want to emphasize, you do not need to create an extender class, but to make life easier for us, I’m going to show you how to anyways.

The extender class will basically call the PHPMailer() class and then setup the basic values for you such as the Email address you want to send from, mail server settings and etc. Each of these settings are inherited by the config.php by default, but you may also overwrite them when you call our extender class. For example, if you do not define the settings in the extender class, they will be set by default and this in turn, allows you to setup the basic values without actually going through the motions every time. That’s the beauty of it!

Here’s a look at our extender class:

MailClass.inc

<?php   require_once($_SERVER['DOCUMENT_ROOT'].'/lib/phpmailer/class.phpmailer.php');   class FreakMailer extends PHPMailer   {   var $priority = 3;   var $to_name;   var $to_email;   var $From = null;   var $FromName = null;   var $Sender = null;  function FreakMailer()   {   global $site;  // Comes from config.php $site array  if($site['smtp_mode'] == 'enabled')   {   $this->Host = $site['smtp_host'];   $this->Port = $site['smtp_port'];   if($site['smtp_username'] != '')   {   $this->SMTPAuth = true;   $this->Username = $site['smtp_username'];   $this->Password = $site['smtp_password'];   }   $this->Mailer = "smtp";   }   if(!$this->From)   {   $this->From = $site['from_email'];   }   if(!$this->FromName)   {   $this-> FromName = $site['from_name'];   }   if(!$this->Sender)   {   $this->Sender = $site['from_email'];   }   $this->Priority = $this->priority;   }   }   ?>

The PHP Mail Class
FreakMailer Class Code Breakdown

The FreakMailer class previously displayed is pretty simple. You only need a very basic understanding of Object Oriented Programming to use it, so let’s break it down now.

First, we are going to call the class.phpmailer.php file from within our phpmailer lib directory under the document root. This allows us to extend the PHPMailer class because it makes that object available. You could include this elsewhere, but this is a good place to do so.

require_once($_SERVER['DOCUMENT_ROOT'].'/lib/phpmailer/class.phpmailer.php');Class Control Structure

Next, we define the class control structure and give our new class a name while extending the PHPMailer class.

class FreakMailer extends PHPMailer {Class VariablesMoving along, we now setup the internal variables. Most of these are set to null by default so that we can do some triggering later on to determine if you want to overwrite the default values from the config.php file.var $priority = 3;   var $to_name;   var $to_email;   var $From = null;   var $FromName = null;   var $Sender = null;

Let’s take a look at these values now:

* $priority – This sets the mail priority by default. Values: 1 = High, 3 = Normal, 5 = Low
* $to_name – This is the name of the person you are sending to
* $to_email – The E-Mail address of the person you are sending to
* $From – The E-Mail address you want to send from
* $FromName – The Name of the sender.

Now that we have those variables defined, we can discuss the FreakMailer() function
FreakMailer() Function

This is the function that basically sets up the default values for the PHPMailer to send E-Mail with. In other words, it’s the whole reason we are using this class.

First, we call the $site array from our config.php so that it can be used within this function and class. There are a couple of ways we can do this, we could point to it from outside of the class, or we can just global it. Using the global call is the easiest method and it works, so let’s just do that!

function FreakMailer()   {   global $site; // Comes from config.php $site arrayNext, we start the bulk of the operations here and start passing in values to the PHPMailer class. There’s not much to explain here, if the internal value ($this->setting) of the setting is not defined after you instantiate the class, it basically calls it from the config.php and we’ll use that instead. I mentioned earlier that you can override the values in the config.php and this is where those checks come into play.  if($site['smtp_mode'] == 'enabled')   {   $this->Host = $site['smtp_host'];   $this->Port = $site['smtp_port'];   if($site['smtp_username'])   {   $this->SMTPAuth = true;   $this->Username = $site['smtp_username'];   $this->Password = $site['smtp_password'];   }   $this->Mailer  = "smtp";   }  if(!$this->From)   {   $this->From = $site['from_email'];   }   if(!$this->FromName)   {   $this->FromName = $site['from_name'];   }   if(!$this->Sender)   {   $this->Sender = $site['from_email'];   }   $this->Priority = $this->priority;   }

The most important thing you need to understand is that all of the functionality in the PHPMailer is still present and can be used even though we’ve extended the class. The only thing we’ve done here is created an extension (hence extends) that takes care of the repetitive stuff we don’t want to do every time we need to send an E-Mail

Now that we have a good understanding of the extender class, let’s move along and start sending some E-Mail!

Sending E-Mail with PHP
Sending E-Mail with PHPMailer

We’ve done our work and we’ve got everything ready to go to start sending E-Mail with PHPMailer. Let’s give it a go and see how everything works!
Basic Test

This test is very important to this tutorial because we will be referring to this basic test code throughout the tutorial when I show you how to use different features with PHPMailer. If this test does not work for you, read through the tutorial again and keep trying until it does work, othewise you will be lost later on!

Our first code example is going to be a file looks like this:

<?php   // Grab our config settings   require_once($_SERVER['DOCUMENT_ROOT'].'/config.php');   // Grab the FreakMailer class   require_once($_SERVER['DOCUMENT_ROOT'].'/lib/MailClass.inc');   // instantiate the class   $mailer = new FreakMailer();   // Set the subject   $mailer->Subject = 'This is a test';   // Body   $mailer->Body = 'This is a test of my mail system!';   // Add an address to send to.   $mailer->AddAddress('foo@host.com', 'Eric Rosebrock');   if(!$mailer->Send())   {   echo 'There was a problem sending this mail!';   }   else   {   echo 'Mail sent!';   }   $mailer->ClearAddresses();   $mailer->ClearAttachments();   ?>

Let’s break down this file so that we have a good understanding of what it does.

First, we are going to include our config.php file within the Document Root so that we have the $site settings available.

// Grab our config settings
require_once($_SERVER['DOCUMENT_ROOT'].'/config.php');

You could do this next step within the config.php file, but to make things easier, I chose not to.

// Grab the FreakMailer class
require_once($_SERVER['DOCUMENT_ROOT'].'/lib/MailClass.inc');

Next, we are going to call up our FreakMailer class and when we do this, we’ll also initialize the PHPMailer class as well by the extends definition in the FreakMailer class.

// instantiate the class
$mailer = new FreakMailer();

Ok, so now we have PHPMailer ready to go with all of our default settings, let’s go ahead and define a subject:

// Set the subject
$mailer->Subject = 'This is a test';

Now let’s define the body of the message:

// Body
$mailer->Body = 'This is a test of my mail system!';

NOTE:
If you are using plain text E-Mail, which is the default, you need to convert new lines by using \n or \r\n and you should use double quotes in the strings such as $mailer->Body. Otherwise for single quotes you can start your string and type it out however you want it in your PHP script and press <ENTER> for each new line you wish to make and when you are done with your string, just end it with the semicolon like normal.

Now, add an address to send to. The AddAddress accepts two inputs. The first is the E-mail address to send to and the second is the Name of the person you are sending to.

// Add an address to send to.
$mailer->AddAddress('foo@host.com', 'Eric Rosebrock');

Next, we send the message and look for an error:

if(!$mailer->Send())
{
echo 'There was a problem sending this mail!';
}
else
{
echo 'Mail sent!';
}

Obviously, if an error is detected, you will see There was a problem sending this mail!, otherwise you will see: Mail sent!

Finally, we will clear the attatchment list and the Address list. This is primarily for sending Mailing lists, but I do it anyways as a (bad?) habit.

$mailer->ClearAddresses();
$mailer->ClearAttachments();
?>

If you have just sent yourself an E-Mail with PHPMailer, then congratulations, you’re on your way to sending E-Mail with PHP the easy way!
Common Problems

Here’s a list of some common problems you may have with sending E-Mail through PHPMailer (these problems would probably be the same with the standard mail() function as well.).

* No SMTP server Running on the local machine – You need some type of SMTP server running!
* Improper setup of the PHP script – Please review through the tutorial again until it works.
* The Apache Web Server is not allowed to relay through the SMTP server on the local machine (typical with some web hosts).
* You did not define the recipient properly.




Now this is only the beginning of the tutorial but you should get a better understanding of how phpmailer works and be able to carry on with some of the more advanced functions afterwards!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.