Email Function

Sending Emails

PHP has a built-in function called mail() that allows you to send emails directly from a script.

Here's a basic example:

$to = "";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From:" . "\r\n" .


In the above example, the mail() function takes four arguments: the recipient's email, the email subject, the email body text, and additional headers like 'From' or 'CC'.

Here's a more advanced example that sends an HTML email:

$to = "";
$subject = "My HTML email";
$message = "
<title>HTML email</title>
<p>This email contains HTML Tags!</p>

$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";


In this example, the Content-type header is set to text/html to indicate that the email content should be interpreted as HTML.

Please note that the PHP mail() function requires your server to be configured to send mail. This usually involves setting up an SMTP server, and the configuration details can vary depending on your hosting environment.

Last updated