Php html email imginn
A classic combination!
Here's a basic example of how to send an HTML email with an embedded image using PHP:
HTML Email Template
<html>
<head>
<title>Email with Image</title>
</head>
<body>
<h1>Email with Image</h1>
<img src="cid:image1" alt="Embedded Image">
</body>
</html>
PHP Code
<?php
// Set the email headers
$headers = array(
'MIME-Version: 1.0',
'Content-Type: text/html; charset=UTF-8'
);
// Set the email body
$body = '<html><head><title>Email with Image</title></head><body><h1>Email with Image</h1><img src="cid:image1" alt="Embedded Image"></body></html>';
// Set the image file
$image_file = 'path/to/image.jpg';
// Set the image content type
$image_type = 'image/jpeg';
// Set the image ID
$image_id = 'image1';
// Create the email message
$message = 'To: '. $_POST['email']. "\r\n";
$message.= 'Subject: Email with Image'. "\r\n";
$message.= 'MIME-Version: 1.0'. "\r\n";
$message.= 'Content-Type: multipart/mixed; boundary="boundary"'. "\r\n";
$message.= '--boundary'. "\r\n";
$message.= 'Content-Type: text/html; charset=UTF-8'. "\r\n";
$message.= $body. "\r\n";
$message.= '--boundary'. "\r\n";
$message.= 'Content-Type: '. $image_type. '; name="'. $image_id. '"'. "\r\n";
$message.= 'Content-Transfer-Encoding: base64'. "\r\n";
$message.= 'Content-Disposition: inline; filename="'. $image_id. '"'. "\r\n";
$message.= chunk_split(base64_encode(file_get_contents($image_file))). "\r\n";
$message.= '--boundary--'. "\r\n";
// Send the email
mail($_POST['email'], 'Email with Image', $message, $headers);
?>
How it works
- The HTML email template is created with an
<img>
tag that references the image using acid
attribute. - The PHP code sets the email headers, body, and image file.
- The email body is created using the HTML template.
- The image file is read and encoded in base64.
- The email message is created using the
multipart/mixed
content type, with the HTML body and image attachment. - The email is sent using the
mail()
function.
Note: Make sure to replace path/to/image.jpg
with the actual path to your image file. Also, this is just a basic example, and you may want to add more functionality, such as error handling and email validation.