1and1 Help Centre Categories

print article

Send E-mails Using PHP

For webhosting packages.

Learn how to send e-mails directly from your hosting server using the built-in PHP mail function.

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.

Simplest example

There are two ways to create a PHP page depending on your package and your preference:

  • You may open a text editor such as wordpad or notepad to write the PHP code, save the file and then upload it via FTP to your webspace.
  • You may log into your webspace via SSH and use a text editor such as nano to write the PHP code and then save the file.

<?php mail('yourEmailAddress@yourDomain.co.uk','Subject of the e-mail','This is the body of the e-mail!'); ?>

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 cgi-mailer@perfora.net 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.

Getting More Technical

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):

<?php

// THE BELOW LINE STATES THAT IF THE SUBMIT BUTTON
// WAS PUSHED, EXECUTE THE PHP CODE BELOW TO SEND THE
// MAIL. IF THE BUTTON WAS NOT PRESSED, SKIP TO THE CODE
// BELOW THE "else" STATEMENT (WHICH SHOWS THE FORM INSTEAD).
if ( isset ( $_POST [ 'buttonPressed' ] )){

// REPLACE THE LINE BELOW WITH YOUR E-MAIL ADDRESS.
$to = 'yourEmailAddress@yourDomain.co.uk' ;
$subject = 'From PHP contact page' ;

// NOT SUGGESTED TO CHANGE THESE VALUES
$message = $_POST [ "message" ] ;
$headers = 'From: ' . $_POST[ "from" ] . PHP_EOL ;
mail ( $to, $subject, $message, $headers ) ;

// THE TEXT IN QUOTES BELOW IS WHAT WILL BE
// DISPLAYED TO USERS AFTER SUBMITTING THE FORM.
echo "Your e-mail has been sent! You should receive a reply within 24 hours!" ;}

else{
?>
<form method= "post" action= "<?php echo $_SERVER [ 'PHP_SELF' ] ;?>" />
  <table>
    <tr>
      <td>Your e-mail address: </td>
      <td><input name= "from" type= "text"/></td>
    </tr>
    <tr>
      <td>Your message: </td>
      <td><textarea name= "message" cols= "20" rows= "6"></textarea></td>
    </tr>
    <tr>
      <td></td>
      <td><input name= "buttonPressed" type= "submit" value= "Send E-mail!" /></td>
    </tr>
 </table>
</form>
<?php } ?>

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