php 发送邮件 带图片,在PHP邮件中附加上传的图片

这是我提出的解决方案。根据上传代码和附加文件信息花了一点时间。另外我遇到了标题的问题,但我设法让它工作,它运作良好。

if ((($_FILES["file"]["type"] == "image/jpeg")

|| ($_FILES["file"]["type"] == "image/pjpeg"))

&& ($_FILES["file"]["size"] < 2000000))

{

if ($_FILES["file"]["error"] > 0)

{

echo "Error: " . $_FILES["file"]["error"] . "
";

}

else

{

echo "Upload: " . $_FILES["file"]["name"] . "
";

echo "Type: " . $_FILES["file"]["type"] . "
";

echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb
";

if (file_exists("upload/" . $_FILES["file"]["name"]))

{

echo $_FILES["file"]["name"] . " already exists. ";

}

else

{

move_uploaded_file($_FILES["file"]["tmp_name"],

"upload/" . $_FILES["file"]["name"]);

echo "Stored in: " . "upload/" . $_FILES["file"]["name"];

echo "
File submitted.";

}

}

}

else

{

echo "Invalid file";

}

// DO THE MAILING HERE

$filename = "upload/" . $_FILES["file"]["name"];

;

$to3 =

$to4 =

$subject = 'Sustainability Photo Contest';

$bound_text = "dacc1231";

$bound = "--".$bound_text."\r\n";

$bound_last = "--".$bound_text."--\r\n";

$headers = "MIME-Version: 1.0\r\n"

."Content-Type: multipart/mixed; boundary=\"$bound_text\"". PHP_EOL;

$headers .= "From: noreply@dacc.edu\r\n";

$fields = array('name','address','city','state','zip','phone','photo','location');

$greet = "The following was submitted on " . date("F j, Y, g:i a") . "

";

$body = $greet;

//$cn = 1;

foreach($fields as $efield) {

if(isset($_POST[$efield])) {

if($efield == "name") {

$body.= "Name: " . $_POST[$efield] . "

";

} elseif($efield == "address") {

$body.= "Address: " . $_POST[$efield] . "

";

} elseif($efield == "city") {

$body.= "City: " . $_POST[$efield] . "

";

} elseif ($efield == "state") {

$body.= "State: " . $_POST[$efield] . "

";

} elseif ($efield == "phone") {

$body.= "Daytime Phone Number: " . $_POST[$efield] . "

";

} elseif ($efield == "photo") {

$body.= "Name of Photo: " . $_POST[$efield] . "

";

} elseif ($efield == "location") {

$body.= "Location where Photo was taken: " . $_POST[$efield] . "

";

}

}

}

$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"

."Here is a Submission for the Photo Contest\r\n" .$body ."\r\n"

.$bound;

$file = file_get_contents($filename);

$message .= "Content-Type: image/jpg; name=\"".$filename."\"\r\n"

."Content-Transfer-Encoding: base64\r\n"

."Content-disposition: attachment; file=\"".$filename."\"\r\n"

."\r\n"

.chunk_split(base64_encode($file))

.$bound_last;

if(mail($to3, $subject, $message, $headers)

&&

mail($to4, $subject, $message,$headers ))

{

echo '
MAIL SENT';

} else {

echo '
MAIL FAILED';

}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值