Php mail send html email
To send an HTML email using PHP, you can use the mail()
function and specify the Content-Type
header as text/html
. Here's an example:
<?php
$to = '[email protected]';
$subject = 'HTML Email Example';
$message = '<html>
<head>
<title>HTML Email</title>
</head>
<body>
<h1>Hello, World!</h1>
<p>This is an HTML email.</p>
</body>
</html>';
$headers = 'MIME-Version: 1.0'. "\r\n";
$headers.= 'Content-type: text/html; charset=UTF-8'. "\r\n";
$headers.= 'From: Your Name <[email protected]>'. "\r\n";
$headers.= 'Reply-To: Your Name <[email protected]>'. "\r\n";
mail($to, $subject, $message, $headers);
?>
In this example, the $message
variable contains the HTML content of the email, and the $headers
variable specifies the Content-Type
header as text/html
. The mail()
function is then called with the recipient's email address, subject, message, and headers.
You can also use the SwiftMailer
library, which is a popular PHP library for sending emails. Here's an example:
<?php
require_once 'swiftmailer/lib/swift_required.php';
$transport = Swift_SmtpTransport::newInstance('smtp.example.com', 587)
->setUsername('[email protected]')
->setPassword('your_password');
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance('HTML Email Example')
->setFrom(array('[email protected]' => 'Your Name'))
->setTo(array('[email protected]'))
->setBody('<html><body>Hello, World!</body></html>', 'text/html');
$mailer->send($message);
?>
In this example, the SwiftMailer
library is used to create a Swift_Message
object, which contains the HTML content of the email. The setBody()
method is used to specify the HTML content, and the setContentType()
method is used to specify the Content-Type
header as text/html
. The Swift_Mailer
object is then used to send the email.
Note that in both examples, you'll need to replace the placeholders ([email protected]
, your_password
, etc.) with your actual email credentials and settings.