简体   繁体   中英

Send HTML Email With Attachments PHP

I came across a situation where I needed to send HTML email with attachments. In the past I had sent HTML email, but sending HTML email with attachments was something I couldn't find easily. So this is how I tackled it.

Let me show you my form first. 用户可以在此表单上填写其详细信息,并可以附加简历

This is the Code for my Form (its using Bootstrap)

 <form enctype="multipart/form-data" action="career.php" method="post" data-parsley-validate=""> <div class="row mt-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 mt-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 mt-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 mt-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 ml-15" for="validatedCustomFile"> Attach CV...</label> </div> </div> <!-- <div class="row mt-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 pl-30 marginttop"> <button type="button" class="btn btn-secondary">S0Frt</button> </div> </div> --> <div class="row mt-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> 

After user clicks on Apply Now , I wanted the data to be mailed to an email address along with attached file.

Now, as you can see at <form enctype="multipart/form-data" action="career.php" method="post" data-parsley-validate=""> that the form is submitting to itself. So I wrote the following code on the top of my . PHP file

 <?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.'); } } ?> 

Now, when the user clicks on Apply Now Button, the intended party specified in $recipient_email gets the email as follows: 在此处输入图片说明

Actually I wanted to share this code snippet with community, I thought if I click on Answer your question, this question will get posted just as a topic. But let it be,

I hope other users might find it useful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM