[英]send attachments and data in form in an email with php
这是我的PHP代码
我的html表单位于下面,我要从名称,电子邮件,电话号码字段和文件附件中获取数据。
现在,我尝试在实时服务器环境中获取附件文件(未输入其他信息),我也以不同的格式获取邮件附件(例如我发送了word文档,但word文件为dat格式,但未显示) Word文件中的任何数据)
<?php
if(isset($_POST['submit']))
{
//The form has been submitted, prep a nice thank you message
$output = '<h1>Thanks for your file and message!</h1>';
//Set the form flag to no display (cheap way!)
$flags = 'style="display:none;"';
//Deal with the email
$to = 'xxxx@gmail.com';
$subject = 'Details and file attachments ';
$message = strip_tags($_POST['message']);
$attachment = (file_get_contents($_FILES['file']['tmp_name']));
$filename = $_FILES['file']['name'];
$boundary =md5(date('r', time()));
$headers = "From: xxxxxx@gmail.com\r\nReply-To: xxxxxxx@gmail.com";
$headers .= "\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"_1_$boundary\"";
$message="This is a multi-part message in MIME format.
--_1_$boundary
Content-Type: multipart/alternative; boundary=\"_2_$boundary\"
--_2_$boundary
Content-Type: text/plain; charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit
$message
--_2_$boundary--
--_1_$boundary
Content-Type: application/octet-stream; name=\"$filename\"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
$attachment
--_1_$boundary--";
mail($to, $subject, $message, $headers);
}
?>
这是我的html文件,建议我如何修改代码以获取其他字段的值以及通过电子邮件发送
<td height="12"> <?php echo $output; ?>
<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" <?php echo $flags;?>>
<fieldset>
<legend>Submit your interest</legend>
<p><label class="title" for="name">Your name:</label>
<input type="text" name="name" id="name"><br /></p>
<p> <label class="title" for="email">Your email:</label>
<input type="text" name="email" id="email"></p>
<p> <label class="title" for="phone">Your phone:</label>
<input type="number" name="phone" id="phone"></p>
<p><label for="location" class="title">Total Experience :</label>
<select name="location" id="location">
<option value="ny">1</option>
<option value="il">2</option>
<option value="ca">3+</option>
</select></p>
<span class="title">Upload your resume</span>
<input type="file" name="resume" />
</p>
</fieldset>
<div><input type="submit" name="submit" id="submit" value="send"></div>
</form></td>
Content-Transfer-Encoding: base64
您的标头说的是数据为base64
格式,但实际内容为原始二进制。 在包含正文之前,请尝试使用base64_encode($attachment)
。
也就是说,请不要手动执行此操作。 正如评论中提到的,图书馆会为您处理所有这一切。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.