ADVERTISEMENT
ADVERTISEMENT

PHP program to send an HTML email message

Here, we are going to learn how to send an HTML email message in PHP?
Submitted by Nidhi, on November 22, 2020

Here, we will demonstrate how we can send an HTML email message using the PHP program?

Program/Source Code:

The source code to send an HTML e-mail message is given below. The given program is compiled and executed successfully.

<?php
//PHP program to send a HTML email message.
$ToEmail = "[email protected]";
$sub = "Subject of the email";
$msg = "<h2>We are sending HTML message</h2>";

$header = "From:[email protected] \r\n";
$header .= "MIME-Version: 1.0 \r\n";
$header .= "Content-type: text/html;charset=UTF-8 \r\n";

$isSuccess = mail($ToEmail, $sub, $msg, $header);

if ($isSuccess == true)
{
    printf("Email send successfully");
}
else
{
    printf("Email sending failed");
}
?>

Explanation:

Here, we created local variables that are initialized with the following values:

  1. From email address
  2. To email address
  3. Header
  4. Subject
  5. Message
$msg = "<h2>We are sending HTML message</h2>"; 

Here, we prepared a message with an HTML heading tag <h2>.

$header = "From:[email protected] \r\n";  
$header .= "MIME-Version: 1.0 \r\n";  
$header .= "Content-type: text/html;charset=UTF-8 \r\n";  

And, we prepared the header to send HTML message.

$isSuccess = mail ($to,$sub,$msg,$header);

We used the mail() method, which is used to send an email message and return the boolean value true or false. The mail() message returns true if the email sends successfully otherwise it returns false.

More PHP Programs »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.