繁体   English   中英

发送带有附件的HTML电子邮件PHP

[英]Send HTML Email With Attachments PHP

我遇到了需要发送带有附件的HTML电子邮件的情况。 过去我曾发送过HTML电子邮件,但是发送带有附件的HTML电子邮件是我不容易找到的。 所以这就是我解决的方法。

首先让我告诉我我的表格。 用户可以在此表单上填写其详细信息,并可以附加简历

这是我的表单的代码(使用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> 

用户单击“ 立即申请”后 ,我希望将数据连同附件一起邮寄到电子邮件地址。

现在,您可以在<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.'); } } ?> 

现在,当用户单击“ 立即应用”按钮时,在$recipient_email指定的预定参与$recipient_email将获得如下电子邮件: 在此处输入图片说明

实际上,我想与社区共享此代码段,我想如果单击“回答您的问题”,该问题将作为主题发布。 随它去吧

我希望其他用户可能会觉得有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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