In this lesson we'll create this send a message dot BHP file which is responsible for validating our
contact form data and transmitting the email using the S.A. IP protocol so start a new document and
save the file as send NSG dot BHP and make sure you save it in your perfect cup directory although we
won't be storing this data into our database.
We're going to start our p p script with a database connection this will come in handy if you ever need
to store the contact form submissions into a database.
This is commonly done for future reference and that way you can track your customers.
Next we'll request the form data using the post method and we've stored the data into three variables
f name email and message.
So this is the data coming from our contact form.
We've used my Eskew Well I real escape string just to prevent any Eskew oil injection.
And so again that adds an additional layer of security.
We also need to create a variable that defines the recipient's email address.
So we're going to store that in a variable called email too.
This is where the form data will submit to.
So you'll receive this email here.
So you're going to plug in your email address.
Next will create a variable that stores the subject line of the contact form so we'll just put test
the message and now we'll validate the form data.
So this is just done in a basic if statement and we've done this quite a few times.
If f name is greater than 50 characters we're going to echo the error code F name long.
If it's less than two characters f name short if the email is more than 50 characters email long if
it's less than two characters emails short if the email is in the incorrect format we have the e format
error code and message long and message short.
If validation passes we have our L statement.
If the validation passes we can proceed to sending the message using p p Mailer.
We start with a require statement that includes an essential file in the p p Mailer directory and that's
called P P Mailer auto load dot BHP.
Next we create a new P P Mailer object and store that in a mail in a variable called mail and this is
the actual email submission code block and I'll just go through it.
So the this line over here sets them the BHP mailer to use the SMP P protocol.
Next we have the host the s empty P host is your outgoing mail provider.
So an example would be G.M. if you're using gmail as your outgoing email provider the SMP host is SMP
PDR gmail dot com.
Now if you're not using Gmail this information would be available through your email service providers
Web site or you can call them depending on who you're using.
Next we specify that our host requires authentication which is almost always the case.
So that's why it's at the true we then set the user name which is your email address and the password
you use for this email account.
I've left it blank but you will need to plug that in.
Next we enable TR less encryption for security purposes Gmail uses pt. 5 8 7 to send emails via SMP.
So again you'll need to check with with your service provider to find out which port they use.
Next we set up our reply to email and so that's stored in our email variable from email is stored in
the email to variable.
This is who the email is from.
And for that we have used f name to the person that filled out the contact form is the one sending the
message.
You can add additional recipients as well.
So here we've just plugged in a demo a demo email you don't have to have additional recipients.
This is optional
this line sets the email to HMO format and that's better for styling purposes over text based emails
than we have the email subject which is stored in the subject variable.
The email content the body content which is stored in the message variable so remember we defined all
these variables up here.
All this data comes from our contact form on the contact page.
Lastly we have an if statement if the email message was not sent for any reason we're going to echo
message could not be sent.
Now the only other alternative to this is that the email was sent in which case we're going to echo
the word true this code.
This access code will then be sent to our contact dot BHP page and interpret it using JavaScript and
Ajax to alert the user of the success.
State
and so that's the entire script there.
So I'm going to refresh the contact got BHP page.
Now I've plugged in our password for this email account.
I'll just fill out the contact form details
and click to submit an
SO HERE WE GET THE SUCCESS message.
Message sent and it should appear in the inbox and there it is.
It's fairly quick
and so that's what the email looks like in the inbox.
You can see the subject text message
and it's from Bob to admin at you excel dot com and then the message body
if we click reply you can see that the reply address is Bob.
Gmail dot com so everything is working the way it should be.