我遇到了需要发送带附件的HTML电子邮件的情况。 在过去,我发送了HTML电子邮件,但发送带附件的HTML电子邮件是我无法轻易找到的。 所以这就是我解决它的方式。
这是我的表格代码(使用Bootstrap)
<form enctype="multipart/form-data" action="career.php" method="post" data-parsley-validate="">
<div class="row m-t-25">
<div class="col-lg-6 col-sm-6">
<input type="text" class="form-control" placeholder="First name" name="fname" required>
</div>
<div class="col-lg-6 col-sm-6 marginttop">
<input type="text" class="form-control" placeholder="Last name" name="lname" required>
</div>
</div>
<div class="row m-t-10">
<div class="col-lg-6 col-sm-6">
<input type="text" class="form-control" placeholder="Phone No" name="phone" required>
</div>
<div class="col-lg-6 col-sm-6 marginttop">
<input type="email" class="form-control" placeholder="Email Id" name="email" required>
</div>
</div>
<div class="row m-t-10">
<div class="col-lg-6 col-sm-6">
<input type="text" class="form-control" placeholder="Education" name="education" required>
</div>
<div class="col-lg-6 col-sm-6 marginttop">
<input type="text" class="form-control" placeholder="Work Experience" name="experience" required>
</div>
</div>
<div class="row m-t-10">
<div class="col-lg-6 col-sm-6 ">
<input type="text" class="form-control" placeholder="Department Applying For " name="dept" required>
</div>
<div class="col-lg-6 col-sm-6 marginttop">
<input type="file" class="custom-file-input" id="validatedCustomFile" name="my_file" required>
<label class="custom-file-label m-l-15" for="validatedCustomFile"> Attach CV...</label>
</div>
</div>
<!--
<div class="row m-t-10">
<div class="col-lg-6 col-sm-6 ">
<input type="text" class="form-control" placeholder="Add Captcha Text ">
</div>
<div class="col-lg-6 col-sm-6 p-l-30 marginttop">
<button type="button" class="btn btn-secondary">S0Frt</button>
</div>
</div> -->
<div class="row m-t-10">
<div class="col-lg-6 col-sm-6 ">
<!--<a href="#" class="btn btn-primary btn-lg active blue-light-bg-color capital-text fontsize-16" role="button" aria-pressed="true">Apply Now</a>-->
<button type="submit" name="submit" class="btn btn-secondary">Apply Now1</button>
</div>
</div>
</form>
&#13;
用户点击立即申请后,我希望将数据与附件一起邮寄到电子邮件地址。
现在,您可以在<form enctype="multipart/form-data" action="career.php" method="post" data-parsley-validate="">
看到该表单正在提交给自己。所以我在我的。 PHP 文件
<?php
if(isset ($_POST['submit'])){
//var_dump($_POST);
//die; $
fname = $_POST['fname'];
$lname = $_POST['lname'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$education = $_POST['education'];
$experience = $_POST['experience'];
$dept = $_POST['dept'];
$message = '
<table width="70%" border="1" cellpadding="5" cellspacing="0" style="height: 129px;">
<tbody>
<tr style="height: 21px;">
<td colspan="2" bgcolor="#6b9e19" align="center" style="font-family: Verdana, Geneva, sans-serif; font-size: 14px; color: #ffffff; height: 21px;"><b>Resume From Website.</b></td>
</tr>
<tr style="height: 18px;">
<td width="30%" style="font-family: Verdana, Geneva, sans-serif; font-size: 12px; text-align: left; color: #7a7a7a; height: 18px;"><b>First Name</b></td>
<td width="70%" style="font-family: Verdana, Geneva, sans-serif; font-size: 12px; text-align: left; color: #404040; height: 18px;"><b>' . $fname . '</b></td>
</tr>
<tr style="height: 18px;">
<td width="30%" style="font-family: Verdana, Geneva, sans-serif; font-size: 12px; text-align: left; color: #7a7a7a; height: 18px;"><b>Last Name</b></td>
<td width="70%" style="font-family: Verdana, Geneva, sans-serif; font-size: 12px; text-align: left; color: #404040; height: 18px;"><b>' . $lname . '</b></td>
</tr>
<tr style="height: 18px;">
<td width="30%" style="font-family: Verdana, Geneva, sans-serif; font-size: 12px; text-align: left; color: #7a7a7a; height: 18px;"><b>Mobile </b></td>
<td width="70%" style="font-family: Verdana, Geneva, sans-serif; font-size: 12px; text-align: left; color: #404040; height: 18px;"><b>' . $phone . '</b></td>
</tr>
<tr style="height: 18px;">
<td width="30%" style="font-family: Verdana, Geneva, sans-serif; font-size: 12px; text-align: left; color: #7a7a7a; height: 18px;"><b>Email</b></td>
<td width="70%" style="font-family: Verdana, Geneva, sans-serif; font-size: 12px; text-align: left; color: #404040; height: 18px;"><b>' . $email . '</b></td>
</tr>
<tr style="height: 18px;">
<td width="30%" style="font-family: Verdana, Geneva, sans-serif; font-size: 12px; text-align: left; color: #7a7a7a; height: 18px;"><b>Education</b></td>
<td width="70%" style="font-family: Verdana, Geneva, sans-serif; font-size: 12px; text-align: left; color: #404040; height: 18px;"><b>' . $education . '</b></td>
</tr>
<tr style="height: 18px;">
<td width="30%" style="font-family: Verdana, Geneva, sans-serif; font-size: 12px; text-align: left; color: #7a7a7a; height: 18px;"><b>Work Experience </b></td>
<td width="70%" style="font-family: Verdana, Geneva, sans-serif; font-size: 12px; text-align: left; color: #404040; height: 18px;"><b>' . $experience . '</b></td>
</tr>
<tr style="height: 18px;">
<td width="30%" style="font-family: Verdana, Geneva, sans-serif; font-size: 12px; text-align: left; color: #7a7a7a; height: 18px;"><b>Dept. Applying For </b></td>
<td width="70%" style="font-family: Verdana, Geneva, sans-serif; font-size: 12px; text-align: left; color: #404040; height: 18px;"><b>' . $dept . '</b></td>
</tr>
</tbody>
</table>';
$from_email = 'career@onecompaany.com'; //from mail, it is mandatory with some hosts
$recipient_email = 'amit@othercompany.com'; //recipient email (most cases it is your personal email)
//Capture POST data from HTML form and Sanitize them,
$sender_name = filter_var($_POST["fname"], FILTER_SANITIZE_STRING); //sender name
$reply_to_email = filter_var($_POST["email"], FILTER_SANITIZE_STRING); //sender email used in "reply-to" header
//$subject = filter_var($_POST["subject"], FILTER_SANITIZE_STRING); //get subject from HTML form
$subject = "Resume From Website";
//Get uploaded file data
$file_tmp_name = $_FILES['my_file']['tmp_name'];
$file_name = $_FILES['my_file']['name'];
$file_size = $_FILES['my_file']['size'];
$file_type = $_FILES['my_file']['type'];
$file_error = $_FILES['my_file']['error'];
if($file_error > 0)
{
die('Upload error or No files uploaded');
}
//read from the uploaded file & base64_encode content for the mail
$handle = fopen($file_tmp_name, "r");
$content = fread($handle, $file_size);
fclose($handle);
$encoded_content = chunk_split(base64_encode($content));
$boundary = md5("sanwebe");
//header
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From:".$from_email."\r\n";
$headers .= "Reply-To: ".$reply_to_email."" . "\r\n";
$headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n";
//plain text
$body = "--$boundary\r\n";
$body .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n\r\n";
$body .= chunk_split(base64_encode($message));
//attachment
$body .= "--$boundary\r\n";
$body .="Content-Type: $file_type; name=".$file_name."\r\n";
$body .="Content-Disposition: attachment; filename=".$file_name."\r\n";
$body .="Content-Transfer-Encoding: base64\r\n";
$body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n";
$body .= $encoded_content;
$sentMail = mail($recipient_email, $subject, $body, $headers);
if($sentMail) //output success or failure messages
{
//die('Thank you for your email');
echo "<script langauge='javascript'>alert ('Your Resume sent successfully. We will contact soon'); </script>";
}else{
die('Could not send mail! Please check your PHP mail configuration.');
}
}
?>
&#13;
答案 0 :(得分:0)
其实我想与社区分享这个代码片段,我想如果我点击回答您的问题,这个问题将作为一个主题发布。但是,就这样吧,
我希望其他用户可能会发现它很有用。