The built-in PHP mail function will allow you to send an e-mail directly from a PHP script as long as you supply the required information such as the recipient, subject of the e-mail, and message or body of the e-mail. There is no need to supply a username, password, or specify which mail server should be used to send the mail since this information is already contained in PHP variables. When sending a mailing using the PHP mail function, it is suggested to also include the optional FROM header, so that any replies to the e-mail are sent to the correct e-mail address.
There are two ways to create a PHP page depending on your package and your preference:
Looking at the example above, you will notice that the PHP mail function takes three parameters as stated earlier in this article (recipient, subject of the e-mail, and message or body of the e-mail). Copy and paste the code above into your text editor but make sure to replace yourEmailAddress@yourDomain.co.uk with the e-mail address you want to send the mail to. Save the file as mail.php and if not using a text editor via SSH, upload the file via FTP to your webspace where it can be accessed.
Next, you simply have to access the file via your browser using the correct URL such as http://yourDomain.co.uk/mail.php
The page loaded should be blank since only PHP code has been written to send mail and no normal output text was added to the file. Within a minute or so, you should receive a e-mail titled "Subject of the e-mail" as we used in the PHP code. The sending address of the e-mail will likely be firstname.lastname@example.org or CGI Mailer. This is because no FROM address was specified. We will change this in a minute.
You should have now successfully sent your first e-mail using PHP! If you do not receive the e-mail, check your SPAM box and make sure to double-check the php code above. The code should be exactly the same aside from the recipient's e-mail address which you should replace with your own e-mail address.
This time, we are going to get a bit more technical and start adding variables, logic and an HTML form into our PHP page to create a means of contact. The example shown below is a simple form that uses a table to keep text and text boxes aligned, and has a text box for an e-mail address and message. A viewer of the page simply fills out these two text boxes and then clicks the button underneath the form to send it. The page will then display a notice that the message has been sent.
Take a quick look at the example below and see if you can make some sense out of it (It looks long but much space has been allowed to keep the example easier to read):
Comment lines are preceded with two forward slashes. These lines can be used to add notes or explain parts of the code and are ignored by PHP. Change the e-mail address in the third line of the example to your own e-mail address. Save the file as contact.php and if not using a text editor via SSH, upload the file via FTP to your webspace where it can be accessed.
Next, you simply have to access the file via your browser using the correct URL such as http://domain.co.uk/contact.php
Enter the information and click the Send E-mail! button to test. You can then change other values as necessary or add additional fields to the form using the example above as reference. You can also find more help by referencing PHP: mail - Manual