• 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.

I found that after trying to be "helpful" and putting the senders email in the "from" and "reply-to". Gmail (and some others) consider this spam and will block them. The address you put in MUST match the domain hosting your mail form.

eg: if your site is www.omgmyspacebarisbroken.com then use noreply@omgmyspacebarisbroken.com and you won't have any issues.

With receivers like Gmail you can use a different "from" domain so long as you have an SPF record set up on the "from" domain that specifies where mail will come from for the domain. So you can send email with the "from" of "sender@example.com" from an SMTP server that identifies as "mail.otherserver.com" so long as the SPF record set for example.com lists mail.otherserver.com as a potential sending server.

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

Add new comment