带有上传图片并通过邮件发送的HTML表单()

时间:2017-03-26 07:41:36

标签: php html

我做了下一个表格:

<form action="test_img.php" method="POST" enctype="multipart/form-data">
    <p>Open images <input type="file" name="fl" multiple></p>
    <input type="submit" value="Send mail">
</form>

我想通过mail()函数发送上传的图片,我写了部分php:

<?php
$file = $_POST['fl'];
$to = "starovoyt107@yandex.ru";
$subject = "Mail with images";
?>

但我不知道接下来要做什么:(请帮我查看标题和附件,或者给我一个好教程的链接!

1 个答案:

答案 0 :(得分:0)

你可以这样做:

$from_mail = "{From Email}";
$message = "{Your message}";
$subject = "{Your subject}";
$mailto = "{To Email}";


$filename = $_POST['fl'];
$path = // You must specify the path;
$file = $path.$filename;
$content = file_get_contents( $file);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$name = basename($file);

// header
$header = "From: ".$from_name." <".$from_mail.">\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";

// message & attachment
$nmessage = "--".$uid."\r\n";
$nmessage .= "Content-type:text/plain; charset=iso-8859-1\r\n";
$nmessage .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$nmessage .= $message."\r\n\r\n";
$nmessage .= "--".$uid."\r\n";
$nmessage .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n";
$nmessage .= "Content-Transfer-Encoding: base64\r\n";
$nmessage .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$nmessage .= $content."\r\n\r\n";
$nmessage .= "--".$uid."--";

if (mail($mailto, $subject, $nmessage, $header)) {
    return true; // Or do something here
} else {
  return false;
}

我希望这会对你有所帮助。

来源:https://stackoverflow.com/a/31428803/3172176