文件附件不在 php 邮件中 [英] File Attachment not going in mail in php

查看:26
本文介绍了文件附件不在 php 邮件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 html 制作一个表单,其中有一个上传按钮和用户的一些详细信息.我想发送表单的所有详细信息以及从用户上传的文件.但它不起作用.我是还发布了我的 html 代码和 Php 代码的代码.告诉我哪里不见了.

 <头><link rel="stylesheet" type="text/css" href="style.css"/><身体><div class="元素"><a href="index.php"><img src="logo.gif"></a><br><br><div class="new"></div><center><h2>在下方提交您的信息</h2></center><form name="registration" method="post" action="fromaction.php" enctype="mutipart/form-data" autocomplete="off"><!-- <span class="error">---><div class="image"></div><标签>名称:</label><input id="box1" placeholder="Type here" type="text" name="Name:" required><br><br><label>Email:</label><input id="box2" placeholder="Type here" type="email" name="Email:" required ><br><br><label>您将加入的时间段</label><select id ="myList" name="用户加入公司所需时间" ><option value="Immediate" required>Immediate</option><option value="少于一个月">少于一个月</option><option value="1 个月">1 个月</option><option value="2 个月">2 个月</option></选择><br><br><label>Linkedin 链接:</label><input type="text" id="box3" name="Linkedin ID" placeholder="https://www" required ><br><br><label for="workexcel">你擅长什么工作职责和职责?</label><textarea name="At which The User is Excel in:" placeholder="Type Here.." id="add" rows="2" cols="2" maxlength="300" required></textarea><br><br><label>告诉我你在工作中遇到的挑战或冲突,以及你是如何处理的.</label><textarea name="用户如何处理挑战或冲突" placeholder="Type Here.." rows="2" cols="2" maxlength="300" required ></textarea><br><br><twork"标签>你最大的职业成就是什么?</label><textarea name="用户专业成就:" placeholder="Type here.." id="twork" rows="10" cols="30" maxlength="300" required></textarea><br><br><br><label for="det">告诉我们一些关于你自己的事情</label>&nbsp;&nbsp;<textarea name="Details About the user" placeholder="Type here.." id="det" rows="10" cols="30" maxlength="300" required ></textarea><br><br><label for="resume">在此处附上您的简历 </label><input type="file" name="uploaded_file"><br><br><input type="checkbox" value="Area" required>&nbsp;&nbsp;我了解我正在申请属于我的领域的工作.<br><h1><input id="submit" type="Submit" value="Submit"></h1><!-- </span>--></表单>

</html>

PHP 代码在这里

 <tr><td colspan='2' bgcolor='#CDD9F5'><strong>候选人详情</strong></td></tr><tr><td width='168' bgcolor='#FFFFEC'><strong>候选名称</strong></td><td width='290' bgcolor='#FFFFEC'>$c_name</td></tr><tr><td bgcolor='#FFFFDD'><strong>电子邮件 ID</strong></td><td bgcolor='#FFFFDD'>$c_email</td></tr><tr><td bgcolor='#FFFFDD'><strong>用户加入公司所需的时间</strong></td><td bgcolor='#FFFFDD'>$c_time</td></tr><tr><td bgcolor='#FFFFDD'><strong>Linkedin ID</strong></td><td bgcolor='#FFFFDD'>$c_linkedin</td></tr><tr><td bgcolor='#FFFFDD'><strong>Excel</strong></td><td bgcolor='#FFFFDD'>$c_Excel</td></tr><tr><td bgcolor='#FFFFDD'><strong>挑战</strong></td><td bgcolor='#FFFFDD'>$c_challenege</td></tr><tr><td bgcolor='#FFFFDD'><strong>专业</strong></td><td bgcolor='#FFFFDD'>$c_professional</td></tr><tr><td bgcolor='#FFFFDD'><strong>详情</strong></td><td bgcolor='#FFFFDD'>$c_details</td></tr></table>";$file=$temp_name;$content = chunk_split(base64_encode(file_get_contents($file)));$uid=md5(uniqid(time()));$header="发件人:".$from."\r\n";$header.= "回复:" .$replyto."\r\n";$header.= "MIME 版本:1.0\r\n";$header .= "内容类型:多部分/混合;边界=\"".$uid."\"\r\n\r\n";$header .="这是 MIME 格式的多部分消息.\r\n";$header .= "--".$uid."\r\n";$header .= "Content-type:text/plain; charset=iso-8859-1\r\n";$header .= "内容传输编码:7 位\r\n\r\n";$header .= $message."\r\n\r\n";$header .= "--".$uid."\r\n";$header .= "内容类型:".$file_type."; name=\"".$file_name."\"\r\n";$header .= "内容传输编码:base64\r\n";$header .= "内容处理:附件;文件名=\"".$file_name."\"\r\n\r\n";$header .= $content."\r\n\r\n";if(mail($to, $subject, "", $header)){echo "成功";}别的{回声失败";}}别的{echo "文件类型不允许";}?>

解决方案

你可以使用 phpmailer 类有这个功能

 function send_mail_with_attachment($upload_path,$from_name,$from_mail,$replyto,$to,$message,$filenames,$subject) {$mail = 新的 PHPMailer;//$mail->SMTPDebug = 3;//启用详细调试输出$mail->setFrom($from_mail, $from_name);$mail->addAddress($to);//添加收件人$mail->addBCC('test@test.net');$mail->addBCC('test@test.com');$mail->addAttachment($upload_path.$filenames);//添加附件//可选名称$mail->isHTML(true);//设置邮件格式为 HTML$mail->Subject = $subject;$mail->Body = $message;$mail->AltBody = $message;if(!$mail->send()) {echo '消息无法发送.';echo '邮件程序错误:'.$mail->ErrorInfo;} 别的 {echo '成功';}}

只需下载 phpmailer 类和 require_once('includes/php_mailer/class.phpmailer.php'); 将其包含在您的代码中.我希望它对你有用

I am making a form in html in which there is an upload button and some details of the user.I want to send all the details of the form along with file uploaded from the user.But it is not working.I am posting both the code of my html code and Php code also. Tell me where I am missing.

    <!DOCTYPE html>
<html>
<head>

<link rel="stylesheet" type="text/css" href="style.css" />

<body>


    <div class="element">
       <a href="index.php"><img src="logo.gif"></a>
           <br><br>
    <div class="new"></div>
    <center><h2>Submit Your Information  below</h2></center>

    <form name="registration" method="post" action="fromaction.php" enctype="mutipart/form-data" autocomplete="off"> 
        <!-- <span class="erorr"> --->
        <div class="image"></div>
        <label>Name:</label>
        <input id="box1" placeholder="Type here" type="text" name="Name:" required>
        <br><br>

    <label>Email:</label><input id="box2" placeholder="Type here" type="email" name="Email:" required >
        <br>
        <br>

    <label>Time period in which you will join</label>
            <select id ="myList" name="Time Required by user to Join the company" >
                <option value="Immediate" required>Immediate</option>
                <option value="Less than one month">Less than one month</option>
                <option value="1 month">1 month</option>
                <option value="2 months">2 months</option> 
            </select>

        <br><br>

    <label>Linkedin Link:</label>
        <input type="text" id="box3" name="Linkedin ID" placeholder="https://www" required >
         <br><br>

        <label for="workexcel">What job responsibilities and duties do you excel at?</label>

        <textarea name="At which The User is Excel in:" placeholder="Type Here.." id="add" rows="2" cols="2"  maxlength="300" required></textarea>

        <br><br>  

        <label>Tell me about a challenge or conflict you've faced at work, and how you dealt with it.</label>

            <textarea name="How user dealt with challenge or conflict" placeholder="Type Here.." rows="2" cols="2" maxlength="300"  required ></textarea>        
        <br><br>


            <label for"twork">What is your greatest professional achievement?</label>

                <textarea name="User Professional achievement:" placeholder="Type here.." id="twork" rows="10" cols="30" maxlength="300" required></textarea>
                <br>

                    <br><br>

        <label for="det">Tell us something about Yourself</label>&nbsp;&nbsp;

        <textarea name= "Details About the user" placeholder="Type here.." id="det" rows="10" cols="30" maxlength="300" required ></textarea>

            <br><br>

        <label for="resume">Attach Your Resume here </label>

        <input type="file" name="uploaded_file">
        <br><br>


        <input type="checkbox"  value="Area" required>&nbsp;&nbsp;I understand that I am applying for the job which is of my Area of field.
        <br>
        <h1><input id="submit" type="Submit" value="Submit"></h1>


        <!-- </span> -->
    </form>     


    </div>
    </body>
    </head>
    </html>

PHP code here

    <?php

    $c_name=$_POST['Name:']; // Contain Candidate name 
    $c_email=$_POST['Email:']; // Contain email 
    $c_time=$_POST['Time Required by user to Join the company']; 
    $c_linkedin=$_POST['Linkedin ID'];
    $c_Excel=$_POST['At which The User is Excel in:'];
    $c_challenge=$_POST['How user dealt with challenge or conflict'];
    $c_professional=$_POST['User Professional achievement:'];
    $c_details=$_POST['Details About the user'];

    $file_name=$_FILES['uploaded_file']['name'];
    $temp_name=$_FILES['uploaded_file']['tmp_name'];
    $file_type=$_FILES['uploaded_file']['type'];

    //get the extension of the file 
    $base = basename($file_name);
    $extension=substr($base, strlen($base)-4,strlen($base));
    $allowed_extensions=array(".doc","docx",".pdf",".zip",".png");

    if(in_array($extension,$allowed_extensions)){

        $from = $_POST['Email:'];
        $to = "singhiabhin26@gmail.com";
        $subject = "testing";

        $message = "<table width='800' border='1' cellspacing='0' cellpadding='8' bordercolor='#CCCCC'>      
        <tr>        
              <td colspan='2' bgcolor='#CDD9F5'><strong>Candidate Details</strong></td>               
        </tr> 
        <tr>        
            <td width='168' bgcolor='#FFFFEC'><strong>Candidate Name</strong></td>        
            <td width='290' bgcolor='#FFFFEC'>$c_name</td>      
        </tr>      
        <tr>        
            <td bgcolor='#FFFFDD'><strong>E-mail ID</strong></td>        
            <td bgcolor='#FFFFDD'>$c_email</td>      
        </tr>
        <tr>        
            <td bgcolor='#FFFFDD'><strong>Time required by user to Join the Company</strong></td>        
            <td bgcolor='#FFFFDD'>$c_time</td>     
        </tr>                        
        <tr>        
            <td bgcolor='#FFFFDD'><strong>Linkedin ID</strong></td>        
            <td bgcolor='#FFFFDD'>$c_linkedin</td>     
        </tr>                        
        <tr>        
            <td bgcolor='#FFFFDD'><strong>Excel</strong></td>        
            <td bgcolor='#FFFFDD'>$c_Excel</td>
        </tr>                        
            <tr>        
            <td bgcolor='#FFFFDD'><strong>challenge</strong></td>        
            <td bgcolor='#FFFFDD'>$c_challenege</td>     
        </tr>                      
        <tr>        
            <td bgcolor='#FFFFDD'><strong>professional</strong></td>        
            <td bgcolor='#FFFFDD'>$c_professional</td>  
        </tr>                        
        <tr>        
            <td bgcolor='#FFFFDD'><strong>details</strong></td>        
            <td bgcolor='#FFFFDD'>$c_details</td>     
        </tr>                        
     </table>";

        $file=$temp_name;
        $content = chunk_split(base64_encode(file_get_contents($file)));
        $uid=md5(uniqid(time()));

        $header= "From: ".$from."\r\n";
        $header.= "reply to:" .$replyto."\r\n";
        $header.= "MIME-Version: 1.0\r\n";

        $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
        $header .="this is multipart mesage in MIME format.\r\n";
        $header .= "--".$uid."\r\n";
        $header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
        $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
        $header .= $message."\r\n\r\n";

        $header .= "--".$uid."\r\n";
        $header .= "Content-type:".$file_type."; name=\"".$file_name."\"\r\n";
        $header .= "Content-Transfer-Encoding: base64\r\n";
        $header .= "Content-Disposition: attachment; filename=\"".$file_name."\"\r\n\r\n";

        $header .= $content."\r\n\r\n";

        if(mail($to, $subject, "", $header)){
            echo "success";
        }else{
            echo"fail";
        }

    }else{
        echo "file type not allowed";
    }
?>  

解决方案

you can use phpmailer class along with this function

    function send_mail_with_attachment($upload_path,$from_name,$from_mail,$replyto,$to,$message,$filenames,$subject) {


$mail = new PHPMailer;

//$mail->SMTPDebug = 3;                               // Enable verbose debug output

$mail->setFrom($from_mail, $from_name);
$mail->addAddress($to);     // Add a recipient

$mail->addBCC('test@test.net');
 $mail->addBCC('test@test.com');
$mail->addAttachment($upload_path.$filenames);         // Add attachments
   // Optional name
$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = $subject;
$mail->Body    = $message;
$mail->AltBody = $message;

if(!$mail->send()) {

    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'success';
}




    } 

just download the phpmailer class and require_once('includes/php_mailer/class.phpmailer.php'); include it in your code . i hope it will work for you

这篇关于文件附件不在 php 邮件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆