Php 将base64转换为电子邮件附件中的图像

Php 将base64转换为电子邮件附件中的图像,php,base64,Php,Base64,我有一个php脚本,可以给我发送电子邮件。在这个电子邮件数据中,我发送base64图像数据,但我希望base64转换成jpeg,然后通过电子邮件作为附件添加 <?php header('Access-Control-Allow-Origin: *'); if(isset($_POST['email'])){ $to = 'my@email.com'; $subject = "New submission from ".$_POST['emai

我有一个php脚本,可以给我发送电子邮件。在这个电子邮件数据中,我发送base64图像数据,但我希望base64转换成
jpeg
,然后通过电子邮件作为附件添加

<?php
header('Access-Control-Allow-Origin: *');

if(isset($_POST['email'])){
    $to      = 'my@email.com';
    $subject = "New submission from ".$_POST['email'];
    $message = $_POST['message'];
    $image = base64_to_jpeg( $_POST['image'], 'image.jpeg' ); //this is base64 image
    $subscribe = $_POST['subscribe'];
    $headers = "From: ".$_POST['email']." <".">\r\n"; $headers = "Reply-To: ".$_POST['email']."\r\n";
    $headers = "Content-type: text/html; charset=iso-8859-1\r\n";
    'X-Mailer: PHP/' . phpversion();
    if(mail($to, $subject, $image, $headers)) echo json_encode(['success'=>true]);
    else echo json_encode(['success'=>false]);
    exit;
 }

function base64_to_jpeg($base64_string, $output_file) {
    // open the output file for writing
    $ifp = fopen( $output_file, 'wb' );

    // split the string on commas
    // $data[ 0 ] == "data:image/png;base64"
    // $data[ 1 ] == <actual base64 string>
    $data = explode( ',', $base64_string );

    // we could add validation here with ensuring count( $data ) > 1
    fwrite( $ifp, base64_decode( $data[ 1 ] ) );

    // clean up the file resource
    fclose( $ifp );

    return $output_file;
}
?>

$image
数据:image/png;base64,iVBORw0KGg

我遇到的问题是,当发送电子邮件时,电子邮件只包含纯文本“image.jpeg”,而不是显示图像或附加图像

你知道我该怎么解决这个问题吗


谢谢。

$image
传递到邮件正文显然会发送名称,因为这是您从函数返回的内容

但是如果你用图像标签传递它

mail($to, $subject, '<img src="' . $image . '" alt="'. $image . '">', $headers); // I used PHP template strings to create the img tag
我现在理解了您的问题,当您将
$image
传递到邮件正文时,会显示
alt
文本,原因很简单,因为邮件不知道从何处下载图像

当您通过邮件发送图像时,图像链接必须是绝对路径,以便邮件知道从何处下载图像

因此,您可以将该图像上载到网站文件管理器,并使用这样的绝对路径,

不要将图像转换为
.jpeg
,只需将
base 64
字符串作为图像的src


我还在代码中看到,您没有将
$headers
连接在一起,而只是重置了值。(即,您使用的是
=
而不是
=

您好,谢谢您的回复。我已经尝试了你的建议,但它看起来像一个破碎的图像。。图像src似乎指向
http://doodle.jpeg
。。知道为什么吗?那么它必须是从函数返回的图像名。我用你的函数和代码在我的系统上试过了。当我移除
img
标签时,我得到了图像的名称。当我没有删除它时,我得到了我转换为
base 64
string的图像。你能显示你试图转换的图像的屏幕截图以及它在浏览器上的显示方式吗?我将base 64添加到了pastebin:,当图像src不正确时,它只显示默认图标。我发布了一个屏幕截图
<?php

function base64_to_jpeg($base64_string, $output_file)
{
    // open the output file for writing
    $ifp = fopen($output_file, 'wb');

    // split the string on commas
    // $data[ 0 ] == "data:image/png;base64"
    // $data[ 1 ] == <actual base64 string>
    $data = explode(',', $base64_string);

    // we could add validation here with ensuring count( $data ) > 1
    fwrite($ifp, base64_decode($data[1]));

    // clean up the file resource
    fclose($ifp);

    return $output_file;
}

    $image = base64_to_jpeg(base64_string_from_link, 'image.jpeg')
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Base 64</title>
</head>
<body>
    <?php echo '<img src="' . $image . '" alt="' . $image . '">'; ?>
</body>
</html>