首页 > 解决方案 > 未定义索引 - 尝试将 base64 编码图像发布到 PHP

问题描述

我试图找出我的错误在哪里,而且我对 PHP 很陌生。我正在尝试发送由 js 函数生成的图像。我目前正在本地主机上运行它,直到我可以解决问题。以下是一些代码片段。

这是我将图像发布到 php 文件的 js 代码:

$('#send-image-mail-php').click(function() {
                yourDesigner.getProductDataURL(function(dataURL) {
                    $.post("php/send_image_via_mail.php", { base64_image: yourDesigner.getProductDataURL() }, function(data) {
                        if(data) {
                            alert("Mail successfully sent!");
                        }
                        else {            
                        alert("Mail could not be sent!");
                        }
            });

这是php代码:

<?php

$base64_str = substr($_POST['base64_image'], strpos($_POST['base64_image'], ",")+1);
$to =   'myemail@domain.com';//set here your receiving mail address
$subject =  'Fancy Product Designer Test Email'; //set here the mail subject
$message = '';
$bound_text =   md5(date('r', time()));
$bound =    "--".$bound_text."\r\n";
$bound_last =   "--".$bound_text."--\r\n";

$headers =  "From: myemail@domain.com\r\n";//set here the sending mail address
$headers .=     "MIME-Version: 1.0\r\n"
    ."Content-Type: multipart/mixed; boundary=\"$bound_text\"";

$message .=     "If you can see this MIME than your client doesn't accept MIME types!\r\n";
    .$bound;

$message .=     "Content-Type: text/html; charset=\"iso-8859-1\"\r\n"
    ."Content-Transfer-Encoding: 7bit\r\n\r\n"
    ."Email Sent Successfully\r\n" //set here the mail text
    .$bound;

$message .=     "Content-Type: image/png; name=\"mail_product.png\"\r\n"
    ."Content-Transfer-Encoding: base64\r\n"
    ."Content-disposition: attachment; file=\"mail_product.png\"\r\n"
    ."\r\n"
    .chunk_split($base64_str)
    .$bound_last;

if(mail($to, $subject, $message, $headers))
{
     echo json_encode(1);
} else {
     echo json_encode(0);
}

?>

这是来自我的 Apache 服务器的错误日志,通过 XAMPP 在本地运行:

[Sat Sep 12 00:32:37.790441 2020] [php7:notice] [pid 10748:tid 1920] [client ::1:61515] PHP Notice:  Undefined index: base64_image in C:\\xampp\\htdocs\\php\\send_image_via_mail.php on line 6, referer: http://localhost/product-designer.html
[Sat Sep 12 00:32:37.790441 2020] [php7:notice] [pid 10748:tid 1920] [client ::1:61515] PHP Notice:  Undefined index: base64_image in C:\\xampp\\htdocs\\php\\send_image_via_mail.php on line 6, referer: http://localhost/product-designer.html
[Sat Sep 12 00:32:37.856441 2020] [php7:notice] [pid 10748:tid 1924] [client ::1:61517] PHP Notice:  Undefined index: base64_image in C:\\xampp\\htdocs\\php\\send_image_via_mail.php on line 6, referer: http://localhost/product-designer.html
[Sat Sep 12 00:32:37.856441 2020] [php7:notice] [pid 10748:tid 1924] [client ::1:61517] PHP Notice:  Undefined index: base64_image in C:\\xampp\\htdocs\\php\\send_image_via_mail.php on line 6, referer: http://localhost/product-designer.html
sendmail: Error writing to C:\xampp\sendmail\debug.log: --- MESSAGE BEGIN ---
sendmail: Error writing to C:\xampp\sendmail\debug.log: To: myemail@domain.com
sendmail: Error writing to C:\xampp\sendmail\debug.log: Subject: Fancy Product Designer Test Email
sendmail: Error writing to C:\xampp\sendmail\debug.log: From: myemail@domain.com
sendmail: Error writing to C:\xampp\sendmail\debug.log: MIME-Version: 1.0
sendmail: Error writing to C:\xampp\sendmail\debug.log: Content-Type: multipart/mixed; boundary="f6e9b319c244b41ebc6650ef9c316270"
sendmail: Error writing to C:\xampp\sendmail\debug.log: 
sendmail: Error writing to C:\xampp\sendmail\debug.log: If you can see this MIME than your client doesn't accept MIME types!
sendmail: Error writing to C:\xampp\sendmail\debug.log: --f6e9b319c244b41ebc6650ef9c316270
sendmail: Error writing to C:\xampp\sendmail\debug.log: Content-Type: text/html; charset="iso-8859-1"
sendmail: Error writing to C:\xampp\sendmail\debug.log: Content-Transfer-Encoding: 7bit
sendmail: Error writing to C:\xampp\sendmail\debug.log: 
sendmail: Error writing to C:\xampp\sendmail\debug.log: Email sent successfully
sendmail: Error writing to C:\xampp\sendmail\debug.log: --f6e9b319c244b41ebc6650ef9c316270
sendmail: Error writing to C:\xampp\sendmail\debug.log: Content-Type: image/png; name="mail_product.png"
sendmail: Error writing to C:\xampp\sendmail\debug.log: Content-Transfer-Encoding: base64
sendmail: Error writing to C:\xampp\sendmail\debug.log: Content-disposition: attachment; file="mail_product.png"
sendmail: Error writing to C:\xampp\sendmail\debug.log: 
sendmail: Error writing to C:\xampp\sendmail\debug.log: 
sendmail: Error writing to C:\xampp\sendmail\debug.log: --f6e9b319c244b41ebc6650ef9c316270--
sendmail: Error writing to C:\xampp\sendmail\debug.log: 
sendmail: Error writing to C:\xampp\sendmail\debug.log: --- MESSAGE END ---
sendmail: Error writing to C:\xampp\sendmail\debug.log: Authenticating with POP3 server
sendmail: Error during delivery: Connect tim

不知道为什么错误日志会在最后一行中断。我关闭并重新打开日志以确保我没有在写中间字时发现它。

此外,我也收到关于 $message 未定义的错误,但我似乎已经通过将其声明为 ''; 来修复此错误。与其余的声明。

无论如何,我已经配置了我的 php.ini 文件以及 sendmail.ini 文件。我正在使用 gmail 发送和接收电子邮件。我输入了 SMTP 和 POP……我希望这不是我出错的地方。

对不起,如果这已经发布过。我真的可以使用一些洞察力。谢谢阅读。

标签: javascriptphpjquerypostbase64

解决方案


推荐阅读