Sign In | Subscribe
Start learning today, and be successful in your academic & professional career. Start Today!
Loading video...
This is a quick preview of the lesson. For full access, please Log In or Sign up.
For more information, please see full course syllabus of Introduction to PHP
  • Discussion

  • Study Guides

  • Download Lecture Slides

  • Table of Contents

  • Transcription

  • Related Services

Bookmark and Share
Lecture Comments (8)

0 answers

Post by Adil Alkarkhi on August 6, 2013

How can I find sendmail.ini? any help please?

1 answer

Last reply by: sorin dragon
Mon Mar 14, 2016 4:43 PM

Post by John Cox on April 26, 2013

I am using a PC with Windows 7. I don't have any "local" email, i.e. DSL provider. I do have accounts on yahoo, gmail and I do not have a dedicated IP address from my PC. I have tried for hours without success to configure sendmail and also tried Mercury (from XAMPP). The only way to avoid an 'Error Sending Email' is to configure php.ini to use mailtodisk.exe as sendmail_path = "C:\Users\larry\xampp\mailtodisk\mailtodisk.exe". This works only to the extent that there is no error, but of course there is no email sent or received. Is the problem in using sendmail as sendmail_path = "\"C:\Users\larry\xampp\sendmail\sendmail.exe\" -t" due to the fact that I cannot use localhost for my mail server?

0 answers

Post by Okoa Kinesy on November 23, 2012

Hi Matthew I am trouble send my email from the php script. The script is returning the string "email sent successfully". However it does this even if I reset the ini files to their default values (ie. "smtp_server=localhost" in sendmail.ini). So it seems like the script is not communicating with the ini files. Any suggestions?

1 answer

Last reply by: Anthony Reynolds
Thu Jul 5, 2012 5:09 PM

Post by Anthony Reynolds on July 5, 2012


1 answer

Last reply by: Matthew M.
Tue Feb 7, 2012 4:26 AM

Post by DANIEL VACA on December 12, 2011

I can only get contatctUs.php to work if $contactInfo['email'] is set to a validated email. But to get the email validated I need to also have the password.

That is, I can get contatctUs.php to send TO any email; But I can only get it to send FROM emails that I own.

Sending Email Using PHP

  • PHP provides the built-in mail() function to send email messages from scripts.
  • The Simple Mail Transfer Protocol, or SMTP , is the protocol used to send and receive email messages on the Internet. It is implemented by mail servers .
  • php.ini can be configured so that mail() will work in either a Windows or Linux/UNIX environment.
  • When in a Windows environment, the configuration directive SMTP is typically set. When in a Linux/UNIX environment, the configuration directive sendmail_path is typically set.
  • Mail servers typically require an email client to authenticate themselves before they will relay their email message to the recipient.
  • fake sendmail for Windows is an application included with XAMPP that allows the sending of emails using mail() with mail servers requiring authentication.
  • sendmail.ini is the fake sendmail configuration file used to specify a username and password to use for authentication.
  • The additional_headers argument of mail() is used to specify who an email is from by specifying a ‘From’ header that takes on the form: 'From:' .
  • Additional Resources:

Sending Email Using PHP

Lecture Slides are screen-captured images of important points in the lecture. Students can download and print out these lecture slide images to do practice problems as well as take notes while watching the lecture.

  • Intro 0:00
  • Lesson Overview 0:14
    • Lesson Overview
  • Built-in Mail Extension 1:28
    • Built-in Mail Extension
  • SMTP Overview 2:04
    • SMTP Overview
  • SMTP - Windows 3:08
    • SMTP - Windows
  • SMTP - Linux/UNIX 4:58
    • SMTP - Linux/UNIX
  • Mail Configuration Directives 6:35
    • Mail Configuration Directives
    • Coding Example
  • Mail Server Authentication 10:20
    • Mail Server Authentication
  • fake sendmail Program 12:27
    • fake sendmail for Windows
    • Main 'add-on' feature
    • Username & Password
  • SMTP - XAMPP for Windows 14:07
    • SMTP - XAMPP for Windows
  • Sendmail Example 16:19
    • Sendmail Example
  • mail() Function 18:39
    • mail() Function
    • additional_headers
    • 'From' Header
  • mail() Function Coding Example 21:09
    • mail() Function Coding Example
  • Web Application Development 31:43
    • Version 9.0 Changelog
  • ContactUs.php 32:52
    • ContactUs.php
    • contactInfo
  • Version 9.0 Coding Example 34:31
    • Version 9.0 Coding Example
  • Homework Challenge 41:32
    • Homework Challenge

Transcription: Sending Email Using PHP

Hello again, and welcome back to's Introduction to PHP course.0000

Today's lesson is going to be a rather fun lesson, because we are going to be learning about how to use PHP's built-in mail function,0004

which is going to allow us to send emails from our PHP scripts.0010

Specifically, we are going to talk a little bit about the built-in extension.0016

And then, we are going to go over some basics of SMTP, which stands for Simple Mail Transfer Protocol.0021

And what that is: that is how email messages get transferred from a sender to a recipient on the Internet.0028

So, we are going to talk a little bit about that, and we are going to talk about how it works in both Windows and in a Linux or UNIX operating environment.0034

We are going to talk about some mail configuration directives that are within php.ini.0042

We are going to go over a little bit about mail server authentication.0049

And then, talk about a program provided by XAMPP called fake sendMail for Windows,0053

which allows you to send email from your PHP programs with a little bit more flexibility than comes built into PHP by itself.0059

And specifically, we are going to talk about the function that does it all, which is the mail function,0070

which is the built-in function of PHP that allows you to send email.0075

And then, we are going to update our web application to include a contactUs page that is going to allow a customer to email the store administrator.0078

As mentioned, PHP provides a built-in mail extension, and what it does is: it provides two functions, but the main function that is used is this function here, mail.0090

What that allows you to do is send emails from your PHP scripts.0103

Now, in order to use the mail function from your scripts, there are several configuration directives that you will need to set up in php.ini.0107

And we are going to walk through that, but before we do that, I am going to go and give a little bit of overview about how email on the Internet works.0118

SMTP, or as it is known, Simple Mail Transfer Protocol, is the protocol that is used between servers on the Internet to exchange email.0126

When you send an email from one email address to another, 0138

SMTP is the rules that are used to get that email from where it originated to where it's going.0142

The protocol is implemented by things known as mail transfer agents, or MTA's,0150

Or, as they are more commonly known, and you have probably heard them referred to as, a mail server.0156

And whenever you send an email from an email client like Outlook or via a web-based email like Gmail,0161

your message gets sent along to a mail server, an SMTP server, as it is sometimes referred to,0169

which will relay it (pass the message along) to the recipient's receiving mail server.0176

Now, I just want to go over how this works in Windows, because there is a little bit of difference between0190

how email is sent, typically, in Windows, and in a Linux and UNIX operating system.0194

We have a diagram here, and we have a client computer, and this little symbol is for the Thunderbird email client.0201

It is free from Mozilla, and it is an email client used to send and receive emails.0214

What happens is: when you are in Thunderbird on your computer, your Thunderbird application--when you go to send an email,0219

it contacts a mail server, typically on your local network, and it sends a message to it by SMTP.0227

What happens is: once the mail server receives that message, it reads it and figures out where it needs to go.0237

And it sends it through the Internet over to whoever the recipient is, whatever domain they reside on.0244

It is going to send it to their mail server, and it is going to use SMTP to do that.0252

And then, what happens is: when the client on the other side, the recipient, opens up their Thunderbird client0257

(let's say they are using Thunderbird as well) to download their email, typically what happens is: they will download their email0264

from the mail server, using what you might have heard of as the POP or IMAP protocols.0269

We are not really going to be talking about that too much in this lesson.0278

We are going to be concerned with the sending of email, so we are going to talk about the SMTP structure.0280

So, in Windows, this is typically how it works: you have an email being sent from your client, and it is sent to a mail server,0285

which sends it out over the Internet; and the mail server is typically on your local network.0292

Now, the typical setup in a Linux or UNIX environment is a little bit different.0299

In this case, we have as our client PHP; and an email client typically refers to something that sends or receives email.0304

And because we are going to be sending email from PHP, we can consider PHP a client.0311

Now, if you notice, there is no mail server over here, like there was in the setup.0317

On a typical Linux setup, there is a program called sendmail, which you may have heard of before, which is its own mail server.0322

So, typically, we already have PHP running; it has sendmail running on the same computer.0334

So, instead of forwarding a message by SMTP out to a local mail server, who will then forward it along to the client,0339

like this, what happens is: PHP or your mail client (you can do Thunderbird, as well) will send the message along to the local sendmail program running.0349

And then, what sendmail does is: that is the actual mail server, and it is the one that goes out through the Internet 0361

and, via SMTP, sends it along to the client's mail server.0368

And then, same as before, the recipient can download it by a POP or IMAP to read the email.0373

The main difference here is that typically, on Linux, you have your SMTP server running on the same computer as your email client.0381

Now, as mentioned, php.ini provides a number of configuration directives that allow you to configure how the mail function is going to work.0396

And the way you set the configuration directives depends on whether you are going to be in a Windows environment or in a Linux or UNIX environment.0406

Typically, when you are using a Windows environment, you are going to be setting a directive called SMTP.0416

What that does is: that allows you to specify the name of your local email server that you are going to use to send out your messages.0422

In our diagram, that was that first server, where the client passed the email along to be sent out over the Internet.0432

In Linux environment, you are going to be using, typically, the directive sendmail_path.0439

What that does is: that gets set to the location of where the sendmail program is on your local machine,0443

because, as we said, sendmail runs on the same machine as your email client or PHP.0451

With the sendmail_path, we actually specify the file location of that sendmail program or sendmail binary.0457

And that way, PHP knows how to invoke the sendmail program to get it to send the email out over the Internet.0463

If we go and take a look...if we open up php.ini, quite a ways down the page, 0469

in the section where extensions are configured, there is a section called Mail Function.0477

You can see a couple of directives here: here is the SMTP directive that we talked about; and this, as mentioned, says "For Win32 only."0485

This is typically what is used in the Windows environment.0494

What you would do is set it to your local SMTP server, and that is going to depend on what you use for your email.0497

For a lot of people, if you have, for example, cable Internet or DSL, your cable Internet provider or ISP0507

gives you the name of a server you can use as an SMTP server, which allows you to send email.0517

For example, sometimes Road Runner is like a Time Warner cable Internet service.0522

Often, their SMTP server that they give to you is called something like

And so, what that does is...what this directive is saying is: that is telling PHP that, when it sends an email using the mail function,0541

that it is supposed to send the email to this SMTP server on your local network,0553

and then, to have that SMTP server actually send it out over the Internet.0561

We will set this back to its default value.0567

Now, the other directive we talked about was the sendmail_path; and this, as you can see, says "for UNIX and Linux."0573

What that is: if you look further down, it is basically a path to the sendmail binary, which is a sendmail program that runs on your computer.0580

Now, this is a Windows path, because we are running a Windows server.0593

We are going to talk in a little bit about a sendmail program that is provided by XAMPP, so that you can mimic the sendmail program in Linux.0598

But typically, this is going to be set to a UNIX path, and it is going to be set to wherever your sendmail binary is located at.0606

Let me just go ahead and comment this back out and save.0615

One of the things that often happens is: your outgoing mail server, your SMTP server, often requires authentication.0623

And that is because, if it didn't require authentication, it is considered an open relay, they call it.0633

Spammers would take advantage of it and just send email requests to it, and they could use that server to send out email 0640

as much as they wanted to, to different places.0646

So, typically, your SMTP server, like the one provided by your local cable provider,0649

is going to require some sort of authentication, so it knows that you are allowed to send emails from that server.0656

It can be done in a couple of different ways.0662

One common thing is to use a username and password that your ISP provider will give you.0664

Another thing that can be used in combination with the username and password, or on its own, is: your ISP will require that0672

any emails sent using their SMTP server, their local outgoing mail server, be originated from a computer that is on the same local network as the mail server.0681

So, for example, if you have a cable Internet service, you have to be logged onto your computer at home,0693

that is accessing the Internet through your cable modem, in order to use, for example, your local SMTP server.0702

If you try to access it elsewhere, it may not authenticate you.0708

In a Windows environment, we can use the smtp directive; and the Windows environment is what we are developing in, in this course.0714

And if that doesn't work, because authentication is required (a username and password is required), there is a program0721

that I mentioned, called fake sendmail for Windows, that is provided with XAMPP.0730

What it does is: it gives you a few more options for being able to connect to your local SMTP server, allowing you to provide authentication.0736

As mentioned, the program is called fake sendmail for Windows, and it is located in...there is a directory in your XAMPP installation0750

called sendmail, and that is called sendmail.exe, which is this program here.0760

And the feature that it adds onto the base functionality that PHP provides through php.ini, which just lets you provide0768

a basic SMTP server through the smtp directive, and doesn't allow for any authentication...fake sendmail has an add-on feature0777

that allows you to be able to specify a username and password to your SMTP server, so that, for those of you whose ISP's require you0788

to do authentication before sending an email--it is going to give you the ability to do that.0799

The username and password that you provide to your SMTP server gets configured in a file called sendmail.ini,0805

which is in the sendmail directory of the XAMPP installation.0816

That configuration file is how you specify to this fake sendmail program how you are supposed to connect to your SMTP server.0819

What that does is: you specify the name of the server (for example, or .net).0829

And then, you provide your username and password your ISP provider is giving you, in order to authenticate yourself to that server.0836

Let's take a look at how this fake sendmail program changes things when we are working with XAMPP for Windows.0848

As you can see, in this diagram, we have our mail server back, and then we also have fake sendmail in the diagram, as well.0858

And so, the way using the fake sendmail program works is kind of a hybrid approach to sending email in a Windows environment.0867

You have your email client (in this case, PHP), and instead of sending an email directly to the mail server 0875

by the smtp configuration directive in php.ini, it sends it to a fake sendmail program.0884

It is configured with the sendmail_path; so even though we are not using the UNIX computer, we configure the sendmail_path directive.0892

And as we saw in the php.ini file, XAMPP goes ahead and, when you have configured set the sendmail_path directive0899

for you to point to this fake sendmail program.0911

And what you do is: you turn that directive on, and so when PHP goes to send a message, it sends a message to the fake sendmail.0913

And then, what sendmail does--it is not an actual SMTP server; it is not an outgoing mail server, like the real sendmail is--what it does is:0924

it goes ahead and still uses the local outgoing mail server on your network.0936

But what it does is: it allows you to configure it, so that it can provide a username and password, so it can authenticate.0942

It sends it out to your mail server by SMTP, using authentication.0950

And then, everything goes as normal from there; your local mail server sends it by SMTP 0967

through the Internet, over to your recipient client on the other side.0975

If we go and take a look at the sendmail.ini file, which is the fake sendmail configuration file, we can see, it has a number of directives in it.0980

It is formatted very similar to php.ini.0991

The main directive you are going to use is smtp_server, which corresponds to the smtp directive in php.ini.0995

That is going to be the name of your ISP's outgoing mail server; so again, it could be like

And then, it has a couple of other configuration directives that you can do.1015

You can set (and this is going to be helpful--we are going to use this when we show an example of sending an email) error logs1020

and debug logs, and they provide you information, if you are having trouble configuring the sendmail.exe, the fake sendmail, to work.1031

That is going to be useful to you, if you are having problems.1039

Then down here (and this is sort of the key difference), there is an auth_username and an auth_password directive,1042

which is going to allow you to enter whatever username and password your ISP provided to you to use to authenticate yourself to the SMTP server.1048

So, for example, the username could be your email address; you could say, "My email address is,1062

and my password is" I wouldn't do something like this, but it would be "password."1067

And so, that is going to configure sendmail, so that when PHP goes ahead and tries to send an email,1073

it is going to run this fake sendmail program, which is going to read this sendmail.ini configuration file.1081

It is going to be able to gather your username and password to authenticate itself to your local SMTP server, 1087

specified up here, and then send the email that way.1096

Let me just go ahead and put these back.1100

So now, let's talk about the mail function, which is the function built into PHP that allows you to send email.1121

It is what gets configured through php.ini, as we had talked about.1127

It has three required arguments: you have to have a to email address; you have to provide a subject; and you have to provide a message.1133

It also has two optional arguments, and it also returns a boolean value 1141

indicating whether the email was successfully passed on to the mail server for delivery.1149

We are going to talk a little more about that, because, just because the mail function returns true, 1155

indicating that your email message was delivered to the mail server, it doesn't necessarily mean the email is going to be properly delivered.1160

There may be problems on the mail server, such as if you have an incorrect email address or something like that.1168

So, you have to kind of be careful of that; we are going to talk about some different cases where the mail function1174

will return true, even though your email doesn't actually get sent.1181

Now, one important thing to note is that the first optional argument, which, if you look at the PHP documentation for mail--1185

the mail function--is called additional_headers, and it is an optional argument, but it is typically almost always required.1193

And what the argument is, is a string where you can pass email headers, like From, CC, BCC, and so forth.1202

Now, in order to send an email, you have to have a from email address specified.1213

So, even though there is no from argument in mail, what you have to do is specify a from header--1218

create a string that contains a from header that has the form right here--it has From: and then the email address you are sending it from.1228

You have to create that as a string, and add that as a fourth argument to the mail function.1237

An example of using the mail function: we have your to email address as the first argument.1242

You have some sample subject text; you have the content of your email message.1248

And then, this optional, but pretty much required, parameter is the additional_headers parameter.1254

And here we have provided the header From: and we provided a from email address.1263

Let's go and take a look at one of our scripts that we have created to show this.1269

First of all, let's look at mail.php, which is a script we have created to demonstrate just the basic mail functionality.1277

What we have done is: we have hard-coded a couple of different values.1288

We have hard-coded a to email address, a subject, and a test message.1291

We have also created email headers, which holds this required From: header that has the from email address.1297

And then, we make a call to the mail function, including the to email address, the subject, the message, and then this email header.1308

And then, what happens is: we output a message saying whether the email was sent successfully, or there was an error.1317

What happens is: when we run the mail function, as mentioned, it returns a boolean value that gets stored in the variable sent.1323

And so, we can test sent down here: if it's true, the email was sent successfully; if not, there was an error.1329

But as mentioned before, it doesn't necessarily mean the email is actually going to be delivered.1335

It just means it was successfully passed along to the mail server.1338

If we go and try to run this page, everything is hard-coded; it is just going to go ahead and try and send out an email.1343

And if we go ahead and load the page, you can see, we get an error.1349

And it is saying "failed to connect to mail server at localhost; verify your "SMTP" and "smtp_port" configuration directives in php.ini are set properly."1356

And we also get our own error message.1366

That is saying that our mail is not configured properly, so what we are going to do now is go ahead and walk through the process1369

of configuring php.ini and sendmail.ini, so that we can send emails from our PHP scripts.1376

Now, we are going to be using the fake sendmail program, because the mail server that we use at requires SMTP authentication.1385

And so, using sendmail is going to allow us to provide a username and password to authenticate ourselves.1395

If we go to php.ini, we are going to comment out the smtp directive, because we are not going to be having PHP try and connect to the local SMTP server.1400

We are going to have it use the fake sendmail program.1414

So, we uncomment this sendmail_path directive, which, when you set up XAMPP, is automatically configured to point to the fake sendmail program.1416

And then, if we go into our sendmail.ini folder, we are going to enter the SMTP server we use at, which is called

We are going to go ahead and turn on the error log file and the debug log file, because those will be useful to us in a bit.1444

And then, we are going to go ahead and enter our SMTP username and password that we need for authentication.1451

Now, the username we use is; that is our email address.1457

And then, our password...I am going to do it off the screen what we need to authenticate with.1464

Now, because we have changed our php.ini file, we need to restart Apache, as we always do any time we make changes.1478

So, I'm going to stop it and restart it.1489

And if we go and reload the page, we should see that our email was sent successfully.1493

What happened is: this time, when we ran it, when we called the mail function, PHP looked at the php.ini configuration file.1506

It saw that we are using a sendmail binary to send our email to the local SMTP server, and then it went ahead and invoked that.1514

It read the sendmail.ini configuration file to get our username and password to be able to send it to the server.1527

So, if we go and look at our local instance of Mozilla, the Mozilla email client is running, and we can see that we just received the email from our web page.1533

It says mail function test; and I'll just turn the headers off.1547

And so, it confirms that our email function has worked.1553

One thing to note is: if we go back to our mail page, typically, you actually prepend the mail function with the error control operator,1562

because if an error gets generated, you get this sort of ugly warning message, as we had seen earlier.1575

I'm going to go ahead and do that here, and then we're going to create an error message that is going to get 1583

this PHP errorMsg variable that we talked about when we learned about the error control operator.1591

And then, if we have an error, it is going to output it down here.1598

And so, just to show how that works, I am going to go back to our php.ini file, and just temporarily get rid of the sendmail.1601

I'll just use and make up some SMTP server that shouldn't work.1621

If we restart our Apache, and we reload the's taking a little while, and that is probably because we have messed up the configuration.1627

You can see that we have gone ahead and seen that there was an error sending the email.1658

But we didn't get that ugly warning message, because we used the error suppression operator.1665

We'll go back and look at mail.php; we can see how that worked.1670

One other example page that we have created is called emailForm.php.1683

What this is: it is sort of an expansion on what we have just learned; it is a way to...instead of hard-coding an email into our system, 1690

it's going to allow us to specify an email address, a subject, and a message, and actually send the email out.1699

And before I do that, let me go ahead and correct the configuration again on php.ini and restart the mail server.1706

And I am going to go ahead and run that mail.php page again, to confirm that our email functionality is working again.1719

"Email was sent successfully," and if I go and look at my inbox, I can see that it was sent; so, the email is back, working again.1731

The way the email form works is: you enter a return address email, or a "from" email (and this is something a customer could do on your website).1739

I'm just going to create a test subject, test email comments...1749

And if we click Send Email, it is going to be using the same process,1759

except it is going to be using the data we provided here, to go ahead and send the email.1763

If I go and look at my inbox, we can see an email created; it has Test subject as the subject, which is what I provided.1767

And it has From:, which is what I provided in the form, and then also the comments that I provided in the message part of the form.1779

So, if we go and look at emailForm.php, when we take a look at it, it takes two GET variables.1790

It has an action variable, and then it also has a contactInfo variable.1800

And what happens is: when you go to that page originally, no action variable is provided, and so what it does is: it loads the form that we saw.1804

The form has three different fields in it that all populate a contactInfo associative array.1814

Here, they populate it with the keys from, subject, and message.1820

And then, when we submit the form, we go ahead and submit it with the hidden field, called action, that says send email.1825

And the form, if you notice, submits to itself, emailForm.php.1831

So, when the form reloads, it is going to come down here and test to see what HTML output...1837

It is going to say, "If action is null, or if action is not equal to sendmail, output the form."1842

Well, because the action is going to be set to sendEmail now, it is going to go down here, 1847

and it is going to process the information that we submitted, and send out the email.1851

As you can see, we have hard-coded the to email address, because we want to send it to the admin, for example, on this website,1857

which is just going to be our email address we have set up.1865

And then, it pulls the from, subject, and email message from the contactInfo variable that was provided by a GET.1869

We go ahead and add the From: header, which is necessary to send the email.1876

And then, go ahead and run the mail function, and use the error suppression operator so we don't get an ugly warning message.1880

And then, we output an error message if an error occurred, and if not, we output that the email was done successfully.1887

And as we saw when we submitted the form, it is currently working; and so, everything works as expected.1894

So now, we are going to talk about updating the web application.1905

Now that we have learned about how to configure email to work in our PHP setup, 1907

and we have learned about how to use the mail function, we are going to add...1912

we are going to call it a contactUs our web store application.1916

What that is going to do is: that is going to allow a customer to go ahead and send some comments to the store administrator by email,1921

saying maybe they like this or that about the store.1929

It is an example of a way, if you were to have your own website, you could have people submit information or comments to you.1931

It is going to make use of the mail function, and we are going to make use of some email-related constants that we are going to add to config.php.1938

We are going to be creating contactUs.php; we are going to be editing the config file, 1948

because we are going to be adding some constants, which you will see in a minute.1954

We are also going to edit the footer, and that is because 1956

we are going to add a link to the contactUs page at the bottom of all of our pages.1959

And then, we are not going to talk about it, but there are some modifications that are going to occur to store.css that format the contactUs form.1964

Really quickly, to talk about the new page, contactUs.php: similar to the example we have just gone over, 1973

it is going to take two GET input parameters: one is going to be called action, and one is going to be called contactInfo.1980

And action, as before, is going to determine whether the page is just going to show the form to the user1988

that allows them to enter their comments, or whether it is actually going to 1992

process the comments that they submitted by a GET and actually send out the email.1995

What we are going to do is: we are going to use the value contactUs; 2002

we are going to set the value action equal to contactUs when we want it to process the email.2005

So, when we have a user submit the form, we are going to submit the hidden input field, as well, that is called action.2010

And it is going to be set to the value contactUs, so that when the page reloads, it is going to know that, 2016

instead of outputting the form, it is supposed to go ahead and try and send the email.2023

contactInfo is an associative array that contains the information the customer submits on the Contact Us form.2028

It is going to contain their name, their email address, and whatever comments they submitted.2036

And we are also going to have it so that the form validates contactInfo, just to make sure that all three of these fields...2044

We are going to require that all three of these fields be provided.2051

And just so you know, the default action for the page is just to display the Contact Us form.2055

That is if no action is specified to the form, or you specify an action that isn't the value contactUs.2060

The default action is to always just go ahead and display the Contact Us form.2067

Let's go take a look at what the form looks like.2071

If we look at the web application--this is the new version, 9.0, we can look at...2075

Well, first, let's go to store.php; and we can see, there has been a link added to the bottom of all the pages, 2080

because we just added a link to footer.html.2087

And if we go and take a quick look at that, you can see, we just added one more link to the bottom here.2091

And when we click on that Contact Us link, it is going to take us to contactUs.php.2099

This is what the form looks like: it has a place where a customer can enter their name, their email address, and their comments.2103

And so, I'll go ahead and enter my name, my email address, and then some comments about the store.2110

And click on the Contact Us button; it is going to go ahead and send an email to a preconfigured address, which we have set up in config.php.2126

So, the email was sent successfully; it says "Thank you for your comments."2139

Actually, since I have my email client running, we can go in and look at it; and because the preconfigured email that it gets sent to 2142

is our email, we can see that it sent an email, and it said "Matthew Machaj had some comments for the store."2148

And then, it output my comments that I had submitted on the form.2156

Let's go and take a look at the code that does this.2160

First of all, let's take a look at config.php; and in config.php, we have gone ahead and added two constants.2164

We have added one called ADMIN_EMAIL_TO, and what that is: any time a user submits comments on the Contact Us form,2173

it is going to get sent to this email address; because we always want it sent to the same spot, we set it up as a constant.2183

And in this case, we are calling it ADMIN_EMAIL_TO.2188

Additionally, we always want our administrator, when they look up their comments received from the store in their email client--2192

we want to have the same subject line, so that they can identify where they are coming from.2200

And so, we have created a constant for that called CONTACT_US_EMAIL_SUBJECT.2205

It just says "Educator Store comments," which lets the administrator know that somebody submitted comments from the Educator store.2210

Now, the contactUs.php page--at the top, it processes the two GET variables that it can use.2218

One is action, and one is contactInfo, as we had talked about.2229

And then, in the data processing section, if the action is set to contactUs, which means the user submitted the form,2234

it goes ahead and just checks to make sure all of the fields on the form were not empty.2240

And if so, it goes ahead and will output the appropriate page title;2246

if not, it's going to output an error message saying "You must complete all fields to use this form."2250

So, for example, if we go back, and we leave out comments, it is going to give you the error message, 2255

saying, "You need to complete all the fields on the form."2265

This is error processing, as we had seen before.2271

And then, on the output section, we output a common title, so the page is always going to say Contact Us.2274

If no error has occurred, and the action has not been specified, or it is not equal to contactUs,2279

then we are saying, "OK, go ahead and output the Contact Us email form," and the Contact Us form submits its GET data to itself.2286

It uses the contactInfo associative array, and then, it also defines, as a hidden GET variable, action with contactUs.2295

So, when the form is submitted, you will know how to process the contactUs information.2307

Also know that it is not supposed to output the form, but in this part down here, it is actually going to say,2312

if the action equals contactUs, it is going to go ahead and send the email.2318

And in this case, we can see, we created some short variables for the customer's name, the email, the comments they submitted...2323

which loads information from the contactUs GET variable.2330

What it does is: we then go ahead and build up an email message, which you saw in the email program we had.2336

And what it does is set it up so that it outputs the person's name.2342

It said, "So-and-so had some comments on the store," and then we add to that this email message, the comments that were provided.2345

And by default, it is going to send the email in plain text; so here, we are using new lines, as opposed to breaks, to format the email how we want.2356

We create the email header, and in this case, instead of having it hard-coded, 2365

we use the email address provided by the customer and set the header that way.2370

And then, here, we go ahead and call the mail function.2376

And as you can see, we have used the two constants we created in config.php.2379

For the to address, we have set it equal to the ADMIN_EMAIL_TO constant, so it is always going to send it to our administrator email account.2383

It is always going to have the subject specified by the CONTACT_US_EMAIL_SUBJECT constant.2390

Here, we have included the message that we built up, up here, which has the customer's name and their comments.2397

And then, we have also added the email headers variable, which has the From: header for the email message.2402

Down here, if this is successful (and here we use the error suppression operator again), it outputs a Thank You message.2410

If not, it says "There was an error in sending your comments."2417

And so, that is how the Contact Us page works.2422

One thing you might note is that (and this is kind of related, but it's a commenting thing) is: sometimes, you will see that,2424

at the end of if statements, for example, we have if action equals null, or action is not equal to contactUs,2433

when you are getting ready to enter another elseif statement, you can add a comment to the end, 2440

on the same line as the curly brace of the if statement, saying the test condition that was used to enter that.2446

So, down here, we know that the section up here was entered if action was equal to null or if action was not equal to contactUs.2452

Similarly, at the bottom of this elseif statement, we can, at the end of the code, add a comment saying,2460

"OK, this section was entered from action=contactUs."2467

That is just a way to provide some feedback to you, as far as comments go, within your script, to make it a little bit easier to read,2469

because when you have long elseif and if statements, like this, you might not know exactly what the test condition was2477

that had you enter that; and so, this just adds a little more information to that.2485

So now, I just want to quickly talk about the homework challenge we are going to have for this course.2494

I would like you to go ahead and try and set up your XAMPP configuration, so that you can send email from your PHP scripts.2497

And you may just be able to edit the php.ini file; just edit that smtp directive.2504

Maybe your local SMTP server doesn't require authentication, or it does require authentication, but it doesn't require using your password;2510

it just requires that you send the email from a computer on your network.2517

So, I would suggest going ahead and trying just setting that first.2522

You will need to contact your ISP to find out what the domain name of your SMTP or outgoing mail server is.2525

Alternatively, you may have to use the fake sendmail program, if your outgoing or SMTP mail server requires authentication.2532

And what you can do there is: as we had gone over, you can edit sendmail.ini 2541

in order to add your username and password, so that you can perform authentication.2546

You also are going to need to change, in php.ini (not to forget)--you need to enable the sendmail_path directive,2551

which is going to tell PHP to use the fake sendmail program.2558

Once you have done that, just go ahead and set up a sample script that is going to call the mail function with some hard-coded values,2562

making sure to include the From: header as that fourth argument to the mail function.2569

And just do that in order to test the configuration; go ahead and try sending an email, 2574

and then make sure that you can see it in your email client, that it is actually being sent, just to verify the configuration.2578

Once you have done that, just create a script, similar to what we did in the course today,2587

that contains a form where a user can put their email address, a subject, and a message.2592

And, using an action GET parameter, have the script submit the form to itself;2597

and based on the action that says something like contactUs or sendEmail, 2604

have the email be generated, using the information that the user provided in the form, and have it send out the email.2609

Be sure to test to verify that the mail function worked and was successful.2616

And then, also check in your email client to make sure that the email was delivered.2621

That ends today's lesson; thank you for watching look forward to seeing you next time.2625