There is only one function needed to send an e-mail within PHP: mail(). The mail() function takes three essential arguements:
· the e-mail address of the intended recipient
· the subject of the e-mail
· the body of the email
Note that when sendng an e-mail, the subject and/or body can be empty strings. We can also specify a fourth arguement - a string containing additional headers. But in order to have the mail() function work properly, we need to specify a working local mail server from the php.ini file. Most servers default to localhost.
Now let's look at a very simple script that will send an e-mail to someone (under PHP). First we'll make a new text file called first_mail.php. Then we'll put the text we want to send into three variables, and then pass them into the mail() function:
Many of these arguements can be easily modified and formatted to fit your needs. For example, the trim() function may be used to clean up unnecessary white space around words.
Header Fields
A header field either represents an instruction to an e-mail program or summarizes the nature and structure of an e-mail message. Each header consists of a title followed by a colon, some white space, then some information. The most common headers are the ones you see when you're checking your email. They are as follows:
| To: | A comma-separated list of e-mail recipients. |
| From: | The sender's e-mail address. |
| Reply-to: | The e-mail address to which replies should be sent. |
| Cc: | Carbon Copy - A comma-separated list of additional recipients. |
| Subject: | The message's subject. |
SPAM Abuse
Even though this script is really simple, it has very powerful bulk-mailer capabilities that can be easily abused for spam. Spamming is considered illegal in many parts of the world, and is certainly not a desirable activity, wasting Internet bandwidth, and the time and download costs of the recipients. You should be careful not to use this kind of script to send unsolicited e-mails to those who don't want them.
Time to get ambitious and write some code in Part 3 >>