How to send email from a php script

EQ Admin

EQ Forum Admin
Staff member
To send email from a php script use the following code that requires an email recipient, subject line, the message body, and what address the email should be sent From:
PHP:
$to = "contact@example.com"; 
$subject = "The subject of the email"; 
$body = "This is an example email sent from a php script";
$from = "yourAddress@example.com";

if (mail($to, $subject, $body, $from)) {   
  echo("<p>Success - Email sent"); 
} else {     
    echo("<p>Fail - Error sending email");   
  }
To help prevent your email being marked as spam be sure to add a valid From: address so the email is not sent as your web server address.
 
Top