Sending html or Text email in PHP

Sending email in php is a very common feature used by php developers. Below code will help out to send email.

Send text email in php:

$to = 'maryjane@clinkshub.com';
$subject = 'Marriage Proposal';

$message = 'Hi Jane, will you marry me?';
$from = 'peterparker@clinkshub.com';
// Sending email
if(mail($to, $subject, $message)){
    echo 'Your mail has been sent successfully.';
} else{
    echo 'Unable to send email. Please try again.';
}

Send html email in php:

$to = 'maryjane@clinkshub.com';
$subject = 'Marriage Proposal';
$from = 'peterparker@clinkshub.com';
 
// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
 
// Create email headers
$headers .= 'From: '.$from."\r\n".
    'Reply-To: '.$from."\r\n" .
    'X-Mailer: PHP/' . phpversion();
 
// Compose a simple HTML email message
$message .= '';

// Sending email
if(mail($to, $subject, $message, $headers)){
    echo 'Your mail has been sent successfully.';
} else{
    echo 'Unable to send email. Please try again.';
}

Hope the above topic will helpfull to the php developers looking for the code to send email in php.

Leave a Reply

Your email address will not be published. Required fields are marked *