电子邮件发布数据和 Base 64 编码冗余


email post data and base 64 encoded redundant?

我有一个页面,它抓取两个字符串变量并发送一个,另一个是存储在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)
?>

电子邮件页面:

<?php
$to = "limitedwave@gmail.com"; 
$subject = $_GET['caption'];
$message = "Image: ".$_GET['image'];
$headers = "From: $email"; $sent = mail($to, $subject, $message, $headers) ; 
if($sent) {
    $response = 'Thank you.';
    print 'thanks';
    } 
else {
    $response = 'There has been an error.';
    print 'fail';
    } 
?>

我有这个来发送 base64 字符串,但我认为它可能会先编码,在这种情况下我已经对其进行了解码......所以我有点迷茫如何整合这些...

function myMail($to, $subject, $mail_msg, $filename, $contentType){
    $random_hash = md5(date('r', time()));
    $headers = "From: webmaster@example.com'r'nReply-To: ".$to;
    $headers .= "'r'nContent-Type: ".$contentType.
        "; boundary='"PHP-mixed-".$random_hash."'"";
    $attachment = chunk_split(base64_encode(file_get_contents($filename)));
    ob_start();
    echo "
--PHP-mixed-$random_hash
Content-Type: multipart/alternative; boundary='"PHP-alt-$random_hash'"
--PHP-alt-$random_hash
Content-Type: text/plain; charset='"utf-8'"
Content-Transfer-Encoding: 7bit
$mail_msg
--PHP-alt-$random_hash
--PHP-mixed-$random_hash--
Content-Type: text/plain; name='"$filename'" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 
$attachment
--PHP-mixed-$random_hash--
";
    $message = ob_get_clean();
    $mail_sent = @mail( $to, $subject, $message, $headers );
    return $mail_sent ? "Mail sent" : "Mail failed";
}

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

尝试使用此函数,请注意我手动获取$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();

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

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

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

<?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);
?>