Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/248.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 电子邮件post数据和base 64编码冗余?_Php_Email_Base64 - Fatal编程技术网

Php 电子邮件post数据和base 64编码冗余?

Php 电子邮件post数据和base 64编码冗余?,php,email,base64,Php,Email,Base64,我有一个页面,它获取两个字符串变量并发送一个,另一个是它存储在web服务器上的base64编码字符串。我还需要通过电子邮件发送带有标题的照片。我有另一个页面可以发送电子邮件…我很好奇如何将图片附在上面 目标PHP页面: <?php $con = mysql_connect("localhost","root","root"); if (!$con) { die('Could not connect: ' . mysql_error()); } $base = $_POST['

我有一个页面,它获取两个字符串变量并发送一个,另一个是它存储在web服务器上的base64编码字符串。我还需要通过电子邮件发送带有标题的照片。我有另一个页面可以发送电子邮件…我很好奇如何将图片附在上面

目标PHP页面:

<?php
$con = mysql_connect("localhost","root","root");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

$base = $_POST['image'];


$date = date("Y-m-d");
$name  = "" . $date.rand(0, 999).".jpg";
$path  = "http://192.168.1.5/trendypieces/site/ios/" . $name;

if (file_exists($name)) {
    echo "File already exists";
} else {
    $binary = base64_decode($base);
    $file = fopen($name, 'wb');
    fwrite($file, $binary);
    fclose($file);
    echo "Image uploaded";
}


mysql_select_db("tpwebdata", $con);
$sql="INSERT INTO latest (caption, image)
VALUES
('$_POST[caption]','$path')"; 
echo "OK";
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());

  }
mysql_close($con)

?>

不确定我应该使用哪一部分将图像附加到电子邮件。谢谢。

尝试使用此函数,注意我手动获取$mime类型,这是因为getmimetype函数不适用于其他php版本

public static $mime_type = array(
                "doc"       =>    "application/msword",
                "docx"      =>    "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
                "pdf"       =>    "application/pdf",
                "xlsx"      =>    "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                "gif"       =>    'image/gif',
                "jpg"       =>    'image/jpeg',
                "png"       =>    'image/png',
                "txt"       =>    'text/plain',
                "rtf"       =>    'application/rtf'
);

/*
 * @des    - return mime type base on file extension
 * @notes  - I use this because some of the php function that get the mimetype
 *           is not working on other php version
 * @params string $filename  - full filename of the file e.g. (imat.png)      
 * @return - mime type of the file
 * @requiremetns array $mime_type - an array of file extension with equivalent mime type        
 */

private static function getMimeType($filename)
{
    $filename = basename($filename);
    $ext = explode(".", $filename);
    $ext = $ext[count($ext) - 1];

    return self::$mime_type[$ext];
}


/*
 * @params string $from - this will be the sender of an email
 * @params string $subject - subject of the email
 * @params array $attachments - an array of attachment e.g. array([0] => array("path" => "public_html/imat.com/data/imat.jpg", "filename" => "imat.jpg"))
 * #the attachments["path"] must be an ABSOLUTE PATH
 * @params string $email_message - email message message of the email(can be an html format)
 * @params string $to - this will be the receiver of an email
 * @params string $cc - this will be the carbon copy of the email 
 * @return boolean - return true/false
 */

public static function mail_with_attachment($from, $subject, $attachments, $email_message, $to , $cc = NULL)
{

    $random_hash = sha1(md5(date('r', time()))); 

    //define the headers we want passed. Note that they are separated with \r\n 
    $headers = array();
    $headers[] = "MIME-Version: 1.0";
    $headers[] = "From: $from";
    $headers[] = "Content-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; 
    $headers[] = "X-Mailer: PHP/".phpversion();
    $headers   = implode("\r\n", $headers);

    //define the message
    $message = array();
    $message[] = "--PHP-mixed-$random_hash";        
    $message[] = "Content-Type: multipart/alternative; boundary=\"PHP-alt-{$random_hash}\"";
    $message[] = "--PHP-alt-{$random_hash}";
    $message[] = "Content-Type: text/html; charset=\"iso-8859-1\"";
    $message[] = "Content-Transfer-Encoding: 7bit";
    $message[] = "\r\n".$email_message."\r\n\r\n";

    foreach($attachments as $attachment)
    {
        $mime_type = self::getMimeType($attachment['filename']);
        $chunk_attachment = chunk_split(base64_encode(file_get_contents($attachment['path'])));

        $message[] = " \r\n--PHP-mixed-$random_hash";
        $message[] = "Content-Type: $mime_type; name=\"{$attachment['filename']}\"";
        $message[] = "Content-Transfer-Encoding: base64";
        $message[] = "Content-Disposition: attachment";
        $message[] = "\r\n".$chunk_attachment."\r\n";
    }

    $message[] = "--PHP-mixed-$random_hash";
    $message   = implode("\r\n", $message);

    //send the email 
    return @mail( $to, $subject, $message, $headers ); 

}

我和PHPMailer一起工作

require '/blahblah/ios/PHPMailer/PHPMailerAutoload.php';

$email = new PHPMailer();
$email->From      = 'limitedwave@gmail.com';
$email->FromName  = 'Store Agent';
$email->Subject   = $_POST['caption'];
$email->Body      = 'the image';
$email->AddAddress( 'limitedwave@gmail.com' );

$file_to_attach = '/blahblah/ios/photos/'.$name.'jpg';

$email->AddAttachment( $file_to_attach , '2014-03-31221.jpg' );

return $email->Send();
然而,当与我的其他代码集成时,它似乎不起作用。我怀疑这是因为电子邮件内容在进程有机会解码并将图像保存到服务器之前运行

有人知道我可能会延迟运行电子邮件吗?我想我当然会把它放在一个函数中,但是我怎么可能等到图像保存后才调用该函数呢

完整代码(不附加图像):


无法让它工作。这不是我真正需要的全部:$chunk\u attachment=chunk\u split(base64\u encode(file\u get\u contents($attachment['path')));--但我的问题是-页面已经收到一个base64编码的POST变量。我可以说$chunk\u attachment=$\u POST[image64encoded]吗?是的,它接收一个base64编码的字符串。我知道你想发一封带有附件的电子邮件。那就行了。希望能有帮助。谢谢
public static $mime_type = array(
                "doc"       =>    "application/msword",
                "docx"      =>    "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
                "pdf"       =>    "application/pdf",
                "xlsx"      =>    "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                "gif"       =>    'image/gif',
                "jpg"       =>    'image/jpeg',
                "png"       =>    'image/png',
                "txt"       =>    'text/plain',
                "rtf"       =>    'application/rtf'
);

/*
 * @des    - return mime type base on file extension
 * @notes  - I use this because some of the php function that get the mimetype
 *           is not working on other php version
 * @params string $filename  - full filename of the file e.g. (imat.png)      
 * @return - mime type of the file
 * @requiremetns array $mime_type - an array of file extension with equivalent mime type        
 */

private static function getMimeType($filename)
{
    $filename = basename($filename);
    $ext = explode(".", $filename);
    $ext = $ext[count($ext) - 1];

    return self::$mime_type[$ext];
}


/*
 * @params string $from - this will be the sender of an email
 * @params string $subject - subject of the email
 * @params array $attachments - an array of attachment e.g. array([0] => array("path" => "public_html/imat.com/data/imat.jpg", "filename" => "imat.jpg"))
 * #the attachments["path"] must be an ABSOLUTE PATH
 * @params string $email_message - email message message of the email(can be an html format)
 * @params string $to - this will be the receiver of an email
 * @params string $cc - this will be the carbon copy of the email 
 * @return boolean - return true/false
 */

public static function mail_with_attachment($from, $subject, $attachments, $email_message, $to , $cc = NULL)
{

    $random_hash = sha1(md5(date('r', time()))); 

    //define the headers we want passed. Note that they are separated with \r\n 
    $headers = array();
    $headers[] = "MIME-Version: 1.0";
    $headers[] = "From: $from";
    $headers[] = "Content-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; 
    $headers[] = "X-Mailer: PHP/".phpversion();
    $headers   = implode("\r\n", $headers);

    //define the message
    $message = array();
    $message[] = "--PHP-mixed-$random_hash";        
    $message[] = "Content-Type: multipart/alternative; boundary=\"PHP-alt-{$random_hash}\"";
    $message[] = "--PHP-alt-{$random_hash}";
    $message[] = "Content-Type: text/html; charset=\"iso-8859-1\"";
    $message[] = "Content-Transfer-Encoding: 7bit";
    $message[] = "\r\n".$email_message."\r\n\r\n";

    foreach($attachments as $attachment)
    {
        $mime_type = self::getMimeType($attachment['filename']);
        $chunk_attachment = chunk_split(base64_encode(file_get_contents($attachment['path'])));

        $message[] = " \r\n--PHP-mixed-$random_hash";
        $message[] = "Content-Type: $mime_type; name=\"{$attachment['filename']}\"";
        $message[] = "Content-Transfer-Encoding: base64";
        $message[] = "Content-Disposition: attachment";
        $message[] = "\r\n".$chunk_attachment."\r\n";
    }

    $message[] = "--PHP-mixed-$random_hash";
    $message   = implode("\r\n", $message);

    //send the email 
    return @mail( $to, $subject, $message, $headers ); 

}
require '/blahblah/ios/PHPMailer/PHPMailerAutoload.php';

$email = new PHPMailer();
$email->From      = 'limitedwave@gmail.com';
$email->FromName  = 'Store Agent';
$email->Subject   = $_POST['caption'];
$email->Body      = 'the image';
$email->AddAddress( 'limitedwave@gmail.com' );

$file_to_attach = '/blahblah/ios/photos/'.$name.'jpg';

$email->AddAttachment( $file_to_attach , '2014-03-31221.jpg' );

return $email->Send();
<?php
$con = mysql_connect("blahblah","tpwebdata","blahblah");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
 // get base64 encoded image from iOS pass vai POST 
$base = $_POST['image'];

// set final URL ($path) for image
$date = date("Y-m-d");
$name  = "" . $date.rand(0, 999).".jpg";
$path  = "http://trendypieces.net/ios/photos/" . $name;

 // save image to location this file resides
if (file_exists($name)) {
    echo "File already exists";
} else {
    $binary = base64_decode($base);
    $file = fopen($name, 'wb');
    fwrite($file, $binary);
    fclose($file);
    echo "Image uploaded";
}

// perform database insert for web site use
mysql_select_db("tpwebdata", $con);
$sql="INSERT INTO latest (caption, image)
VALUES
('$_POST[caption]','$path')"; 
echo "OK";
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());

  }

// mail it --------------------

require '/blahblah/ios/PHPMailer/PHPMailerAutoload.php';

$email = new PHPMailer();
$email->From      = 'limitedwave@gmail.com';
$email->FromName  = 'Store Agent';
$email->Subject   = $_POST['caption'];
$email->Body      = 'Trendy images.';
$email->AddAddress( 'limitedwave@gmail.com' );

$file_to_attach = '/blahblah/ios/photos/'.$name.'jpg';


$email->AddAttachment( $file_to_attach , '2014-03-31221.jpg' );

return $email->Send();


mysql_close($con);
?>