如何在电子邮件中粘贴图像

时间:2014-12-19 20:30:19

标签: php html forms email messages

哦,这是一个棘手的问题。 (我认为)。 您是否知道何时收到消息,并且电子邮件在页面顶部或底部有图像。 我一直试图做到这一点,但无法弄明白。 我希望你的家伙可以帮我解决这个问题。

这是我到目前为止所得到的;

    <?php 
    $to = 'info@gmail.com';

    if(isset($_POST['submit']))
    {

    $name = $_POST['name'];
    $gast = $_POST['email'];
    $tmpName = $_FILES['attachment']['tmp_name']; 
    $fileType = $_FILES['attachment']['type']; 
    $fileName = $_FILES['attachment']['name']; 



    if(empty($errors))
    {

    $to = $to;
    $from = $gast;
    $file = fopen($tmpName,'rb'); 
    $data = fread($file,filesize($tmpName)); 
    fclose($file);

    $mimeBoundary = "==Multipart_Boundary_x{$randomVal}x";

    $body = "E-mail".
    "Name: $name\n".
    "Email: $gast \n".
    "Content-Type: {$fileType};\n". 
    "Content-Transfer-Encoding: base64\n\n". 
    $data;  

    $headers = "From: $from \r\n";
    $headers .= "Email: $gast \r\n";
    $headers .= "\nMIME-Version: 1.0\n"; 
    $headers .= "Content-Type: multipart/alternative;\n";
    $headers .= " boundary=\"{$mimeBoundary}\""; 
    $headers .= "img src='http://cache.estivant.nl/image/1399025430_12_banners-bestemmingen-single-1680x460-extra2-06-kos_1399025430.jpg' alt='image'";

    $data = chunk_split(base64_encode($data));

    mail($to, $body, $headers);

    }
    }

    ?>


    <html>
    <head></head>
    <body>
    <form method="post" action="">

    <label for="name">name</label>
    <input type="name" name="name" value="" />

    <label for="email">email</label>
    <input type="email" name="email" value="" />

    <button id="submit" name="submit">send</button>
    </form>
    </body>
    </html>

2 个答案:

答案 0 :(得分:1)

只需编写一个带有CSS样式的html页面并相应地格式化您的电子邮件,并使用html作为您的电子邮件正文。这将在邮件中创建一个图像

<img src="image.jpg" alt="image"></img>

请注意,只要您不在可信任的发件人列表或联系人列表中,大多数(如果不是全部)电子邮件客户端都会阻止您的图片显示给您的接收者。

当然,您的电子邮件必须以html格式发送

答案 1 :(得分:0)

以下是带有图片的格式良好的电子邮件示例。

注意图像语法:<img src="http://css-tricks.com/examples/WebsiteChangeRequestForm/images/wcrf-header.png" alt="Website Change Request" />'

$to = 'bob@example.com';

$subject = 'Website Change Request';

$headers = "From: " . strip_tags($_POST['req-email']) . "\r\n";
$headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\r\n";
$headers .= "CC: susan@example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

$message = '<html><body>';
$message .= '<img src="http://css-tricks.com/examples/WebsiteChangeRequestForm/images/wcrf-header.png" alt="Website Change Request" />';
$message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
$message .= "<tr style='background: #eee;'><td><strong>Name:</strong> </td><td>" . strip_tags($_POST['req-name']) . "</td></tr>";
$message .= "<tr><td><strong>Email:</strong> </td><td>" . strip_tags($_POST['req-email']) . "</td></tr>";
$message .= "<tr><td><strong>Type of Change:</strong> </td><td>" . strip_tags($_POST['typeOfChange']) . "</td></tr>";
$message .= "<tr><td><strong>Urgency:</strong> </td><td>" . strip_tags($_POST['urgency']) . "</td></tr>";
$message .= "<tr><td><strong>URL To Change (main):</strong> </td><td>" . $_POST['URL-main'] . "</td></tr>";
$addURLS = $_POST['addURLS'];
if (($addURLS) != '') {
    $message .= "<tr><td><strong>URL To Change (additional):</strong> </td><td>" . strip_tags($addURLS) . "</td></tr>";
}
$curText = htmlentities($_POST['curText']);           
if (($curText) != '') {
    $message .= "<tr><td><strong>CURRENT Content:</strong> </td><td>" . $curText . "</td></tr>";
}
$message .= "<tr><td><strong>NEW Content:</strong> </td><td>" . htmlentities($_POST['newText']) . "</td></tr>";
$message .= "</table>";
$message .= "</body></html>";

mail($to,$subject,$message,$headers);

代码来源和完整说明:http://css-tricks.com/sending-nice-html-email-with-php/