我有一个抓取两个字符串变量并发送一个的页面,另一个是它存储在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";
}
不确定将图像附加到电子邮件的哪个部分。感谢。
答案 0 :(得分:0)
尝试使用这个函数,注意我手动获取$ 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 );
}
答案 1 :(得分:0)
我使用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);
?>