Ever find yourself typing the headers into the php mail() function over and over again?

Try using a standard function and calling it when you need to send mail. I got tired of copy-pasting the same header block across different scripts, so wrapping it in a reusable function saved a lot of repetition. The function below handles the content-type and from headers in one place.

Keep in mind that PHP’s built-in mail() relies on your server’s sendmail configuration, so it won’t work out of the box on every setup. For production use, a library like PHPMailer or SwiftMailer gives you SMTP support, better error handling, and attachment support. But for quick scripts or internal tools, this gets the job done.

function sendEmail($subject,$content,$from,$to){
  $header = "Content-Type: text/html; charset=iso-8859-1\nFrom:$from";
  if( mail($to, $subject, $content, $header) );
}