Legacy


  1. Home
  2. Legacy
  3. Scripting
  4. Sending PHP Email
Click here for full details

Sending PHP Email

In response to a number of PHP email sending related support queries we’ve put together this post outlining the most basic sending method and referencing some of the more advanced PHP email topics. Additional information on PHP mail related functions can be found in the PHP mail function documentation.


Sending a PHP email

A very simple script to get your message out there. Create a file called something.php with the contents below, upload it into a domain directory via FTP, then visit http://domain.ext/something.php and your email should be sent and arrive at its destination within seconds.

<?php $domain = 'domain.ext'; $to     = 'website@'.$domain; $from     = 'website@'.$domain; $subject = 'Website Email From '.$domain; $message = 'your message'; $message = wordwrap($message); $headers = 'From: '.$from."\r\n".'Reply-To: '.$from."\r\n"; mail($to, $subject, $message, $headers); ?>

 


Sending a PHP email via specific SMTP relay server

To send PHP email via a remote SMTP server where relaying has been allowed and optionally setting the SMTP port, add the following code above your email script.

Please note that all Pipe Ten managed web servers are already configured by default to relay email via the appropriate server.

ini_set("SMTP", "dwebform.pipeten.co.uk");
ini_set("smtp_port", "25");

 


Advanced PHP Email Topics

Preventing Form Variable Injection

When passing form/visitor input to a script which sends an email via PHP one must ensure that the submitted data is protected against malicious or unintentional activity by the visitor. The function below protects against simple injection attempts against forms.

function heal($str) {
	$injections = array('/(\n+)/i',
	'/(\r+)/i',
	'/(\t+)/i',
	'/(%0A+)/i',
	'/(%0D+)/i',
	'/(%08+)/i',
	'/(%09+)/i'
	);
	$str= preg_replace($injections,'',$str);
	return $str;
}
$from = heal($_POST['customer_email']);

Preventing Automated Form Input (CAPTCHA)

CAPTCHA or Completely Automated Public Turing test to tell Computers and Humans Apart is now a standard component of many mail forms and authentication systems to prevent the automated submission of forms and spam.

Many CAPTCHA systems exist but recaptcha is by far the most popular and easily implemented.

Sending Mass Emails with PHP

There are many different methods available for sending multiple emails from PHP. These range from looping the PHP mail(), to using PEARS Mail and Mail::Queue libraries (coding required), to open source feature rich packages such as PHPList. All of these methods are supported by Pipe Ten however for details on good email etiquette and mass mailing restrictions with Pipe Ten’s services please see the Mass Mail Policy.

Click here for full details

Classification: Public
Last saved: 2023/07/05 at 13:50 by Jamie

How can we help?