通过PHP邮件发送Base64

时间:2015-04-17 03:05:20

标签: javascript php html canvas base64

我尝试使用PHP邮件将base64编码的图像作为附件发送,但它似乎无法正常工作。

我的脚本如下:

 <?php

 if(!empty($_POST['email'])){

    $name = $_POST['name'];
    $email = $_POST['email'];
    $company = $_POST['company']; 
    $size = $_POST['size']; 
    $colour = $_POST['colour'];
    $lid = $_POST['lid']; 
    $quantity = $_POST['quantity'];
    $image = $_POST['image'];

    $file_name = "bottle-design.png";
    $file_contents = chunk_split($image); 
    $uid = md5(time());
    $headers = array();

    $subject = "Bottle Design Quote";
    $message = "Customer Name: $name<br/> Customer Email: $email<br/> Company Name: $company<br/> Bottle Size: $size<br/> Bottle Color: $colour<br/> Bottle Lid: $lid<br/> Quantity: $quantity<br/> Design: $image";
    $to = "alex@givemetap.com,alex@saidani.co.uk";

    $headers[] = "MIME-Version: 1.0";
    $headers[] = "From: {$name}<{$email}>";
    $headers[] = "Reply-To: {$email}";
    $headers[] = "Content-Type: multipart/mixed; boundary=\"{$uid}\"";
    $headers[] = "This is a multi-part message in MIME format.";
    $headers[] = "--{$uid}";
    $headers[] = "Content-type:text/plain; charset=iso-8859-1";
    $headers[] = "Content-Transfer-Encoding: 7bit";
    $headers[] = $message;
    $headers[] = "--{$uid}";
    $headers[] = "Content-Type: application/octet-stream; name=\"{$file_name}\"";
    $headers[] = "Content-Transfer-Encoding: base64";
    $headers[] = "Content-Disposition: attachment; filename=\"{$file_name}\"";
    $headers[] = $file_contents;
    $headers[] = "--{$uid}--";


    mail($to,$subject,$message,implode("\r\n", $headers));

    header('Location: ../success.php'); 

   }
   else 
   {
       header('Location: builder.php');
   }
?>

我的base64值是使用javascript创建的,如下所示:

function convertImage() {
    var bottleCanvas = document.getElementById('bottleCanvas');
    var designCanvas = document.getElementById('editorCanvas');
    var bottleContext = bottleCanvas.getContext('2d');

    if($('.colourCanvas500').is(':visible')) {
        bottleContext.drawImage(designCanvas, 153, 250);
    }
    else if($('.colourCanvas600').is(':visible')) {
        bottleContext.drawImage(designCanvas, 155, 238);
    }
    else if($('.whiteCanvas500').is(':visible')) { 
        bottleContext.drawImage(designCanvas, 132, 235);
    }
    else if($('.whiteCanvas600').is(':visible')) {
        bottleContext.drawImage(designCanvas, 132, 235);
    }

    var data = bottleCanvas.toDataURL("image/png");
    var link = document.createElement('a');
    link.download = "bottle-design.png";
    var dataLink = bottleCanvas.toDataURL("image/png").replace("image/png", "image/octet-stream");
    //document.write(dataLink);
    link.href = dataLink;
    link.click();

    $('#imageInput').val(data);

    $('form[name="bottleDetails"]').submit();
}

我如何才能实现这一目标,或者是否有更好的方式通过电子邮件发送base64图像?


我已使用以下代码使其使用Apple Mail工作,而图像确实向所有其他邮件客户端发送文件似乎已损坏。

$message = "

        Content-Type: multipart/related; boundary='boundary-example'; type='text/html'

--boundary-example
Content-Type: text/html; charset='US-ASCII'

Customer Name: $name<br/> Customer Email: $email<br/> Company Name: $company<br/> Bottle Size: $size<br/> Bottle Color: $colour<br/> Bottle Lid: $lid<br/> Quantity: $quantity<br/> Design:

<IMG SRC='cid:bottle-design' ALT='bottle design'>

--boundary-example
Content-Location: CID:somethingatelse ; this header is disregarded
Content-ID: <bottle-design>
Content-Type: IMAGE/PNG
Content-Transfer-Encoding: BASE64

$img

--boundary-example--";

0 个答案:

没有答案