• Sales: 1-800-961-2888
  • Support: 1-800-961-4454

How can I test PHP mail functionality?


You can use the following code saved into a file with a .php extension to test PHP mail functionality:

<?
$headers = 'From: webmaster@example.com';
mail('nobody@example.com', 'Test email using PHP', 'This is a test email message', $headers, '-fwebmaster@example.com');
?>
<?php
$to = 'nobody@example.com';
$subject = 'Test email using PHP';
$message = 'This is a test email message';
$headers = 'From: webmaster@example.com' . "\r\n" .
           'Reply-To: webmaster@example.com' . "\r\n" .
           'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers, '-fwebmaster@example.com');
?>


© 2011-2013 Rackspace US, Inc.

Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License


See license specifics and DISCLAIMER

6 Comments

The submitted email address is not shown in the body of email. It's shown as "$email" instead. This same php script work fine on other server, however it doesn't work on the rackspace server. Is there php script to show the submitted email address in the body of email?

<?php
$to = 'nobody@example.com';
$subject = 'Test email using PHP';
$email = $_POST['email'];
$message = 'This is a test email message $email';
$headers = 'From: webmaster@example.com' . "\r\n" .
'Reply-To: webmaster@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);

?>

The "$_POST" variable would be an array of values in the posted data, as I understand it. If the submitted address isn't in a field named "email" then it wouldn't be extracted from the $_POST variable. You might, then, check the name associated with that field in the form's POST to see if it's something other than "email".

It should be noted that you require headers to be passed. I just migrated a script that stopped sending mail because headers were not passed as default.

Use Test Mail Server tool : http://www.toolheap.com/test-mail-server-tool/

Add new comment