P HP has a male function that will send an email.
This might look simple to use.
You just call the function and pass in the recipient's email address the subject and the message.
However using this function requires the mail server settings to be configured in the P HP configuration
file which you might not have access to.
Plus formatting the message and headers so the message is sent properly can be quite complex.
There's a simple alternative to using this function and that's to use a third party mail package.
Like P H.
P mailer.
And this is what most p HP developers do pay HP Maila is basically a series of class files that you
save with your code.
Require those files and then you can create objects of those classes.
P P Mailer provides an object to easily send an email using an S M T P server.
Let's look at an example.
We'll start with an empty P H P script in the root.
To add P HP P mailer to this code will start by downloading it and extracting the downloaded zip file
to a folder with our code.
It's common practice to save third party packages in a folder called vendor which is where I've extracted
the contents of the zip file too.
To use the downloaded classes.
First we need to include this code.
Which I'll paste in the index.
Doug P HP file the P HP mailer.
Classes are inside names spaces which are basically like folders and sub folders for classes.
These two use statements allow us to use the p h.
P mail classes without prefixing them with their name spaces.
Next we need to change.
These require lines to the folder where we've saved the P H P Mailer package and this is the vendor
folder we just created.
Now we're ready to use P HP mailer.
First we'll create an object of the P HP Maler class.
Then we call methods and set properties of the object to tell it to use our mail server.
This line tells P 8 p mailer to use S M T P.
Then we have the host which is the address of your mail server for example.
S M T P R G mail that come then we tell P P mailer that the server requires authentication and we set
the username and the password reset the type of authentication and the port you can get these settings
from your mail server service provider.
Once we've configured the mail server we can add a sender a recipient a subject and a message.
To send the message we call the send method.
And just so we know it's worked.
Let's print out a message.
If a problem occurs then we want to know what it was.
We can use exception handling for this.
First we need to pass true to the constructor.
When we create the P eight p Mailer object this then labels are exceptions.
Then we can wrap this code in a tri block.
And add a corresponding catch block where we can print out the details of the error.
Let's give that a try.
If I open the P 8 p script.
We get a message saying the message has been sent.
And in the recipient's inbox there's the message sent from HP.
P HP Maila has excellent documentation and several cold examples on how to use its various features.
One page to note is the one on S M T P E debugging.
By setting the S N T P debug property of the P It p Mailer object you can get varying amounts of debugging
data from the mail server which can be extremely helpful if you have any connection.
Are sending problems.