在电子邮件脚本中添加图像

时间:2014-12-11 09:40:20

标签: php image email

我有一个效果很好的电子邮件脚本,但无法在包含内容的电子邮件中显示图像。如何让图像显示在电子邮件中。我需要将电子邮件显示在$ template区域中。

<?php
//Fetching Values from URL
$name = $_POST['name1'];
$email = $_POST['email1'];
$message = $_POST['message1'];
$contact = $_POST['contact1'];
//sanitizing email
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
//After sanitization Validation is performed
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
if (!preg_match("/^[0-9]{10}$/", $contact)) {
echo "<span>* Please Fill Valid Contact No. *</span>";
} else {
$subject = $name;
// 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";
$headers .= 'From:' . $email. "\r\n"; // Sender's Email
$headers .= 'Cc:' . $email. "\r\n"; // Carbon copy to Sender
$template = '<div style="padding:50px; color:white;">Hello ' . $name . ',<br/>'
. '<br/>Thank you...! For leaving your information with us.<br/><br/>'
. 'Name: ' . $name . '<br/>'
. 'Email: ' . $email . '<br/>'
. 'Contact No: ' . $contact . '<br/>'
. 'Message: ' . $message . '<br/><br/>'
. 'This is a Contact Confirmation mail.'
. '<br/>'
. 'We Will contact You as soon as possible . <br><br> Sincerely,<br>Prive Care Team.</div>';
$sendmessage = "<div style=\"background-color:#7E7E7E; color:white;\">" . $template . "</div>";
// message lines should not exceed 70 characters (PHP rule), so wrap it
$sendmessage = wordwrap($sendmessage, 70);

// Send mail by PHP Mail Function
mail("cc-wp@hotmail.com", $subject, $sendmessage, $headers);
echo "Your Query has been received, We will contact you soon.";
}
} else {
echo "<span>* invalid email *</span>";
}

?>

1 个答案:

答案 0 :(得分:0)

在电子邮件中(与任何HTML页面一样)有两种显示图像的方式,可以使用互联网上托管的图像进行显示<img src="src from the internet" /> 或者您可以在base64中对图像数据进行编码并将其嵌入到<img src="data:image/png;base64,iVBORw0KGgoAAAANS..." />中,这样电子邮件就是自我(不依赖于互联网上的其他来源),但会占用更多空间。