如何使用php发送附件(.doc文件)的电子邮件

时间:2013-05-02 10:54:53

标签: php file-upload

我一直在努力使用thank you.php为邮件添加附件。它通常是简历/简历下单表格。 这是我的 job application.php 代码:

<form name="frm" action="ThankYou.php" method="post" style="padding-top:10px;">
<table style="text-align: left; width: 100%; color: rgb(255, 0, 0);
font-family: Arial; font-size: 11px; font-weight: bold;"
rules="all" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td>*mandatory fields</td>
</tr>

                            <tr>
                                <td align="left" valign="top"><p><table id="Table2"  cellSpacing="0" cellPadding="3" width="100%" align="center"
                                            border="0">
                                            <tr>
                                                <td style="WIDTH: 152px; HEIGHT: 31px" vAlign="top" align="left"><span id="Label1" class="text" class="text" style="background-color:Transparent;"><span style="font-size:13px; font-family:Arial; font-weight: normal;
color:#000000"><span id="Label15" class="asterix" style="color:Red;"></span>Form 
              of address</span></span></td>
                                                <td style="HEIGHT: 31px"><select name="DDtitle" id="DDtitle" class="formtext">


</select></td>
                                            </tr><br>
                                            <tr>
        <td height="35" valign="top"><span style="font-size:13px; font-family:Arial; font-weight: normal;
color:#000000"><font color="#FF0000"><sup>*</sup></font>First Name:<font color="#FF0000"></font></span></td><br>
        <td valign="top"><input type="text" size="35" name="name" id="name" value=""></td></tr>
                                            <tr>
        <td height="35" valign="top"><span style="font-size:13px; font-family:Arial; font-weight: normal;
color:#000000"><font color="#FF0000"><sup>*</sup></font>Last Name:<font color="#FF0000"></font></span></td>
        <td valign="top"><input type="text" size="35" name="lname" id="lname" value=""></td></tr><br>
                                            <tr>
    <td height="35" valign="top"><span style="font-size:13px; font-family:Arial; font-weight: normal;
color:#000000">Address: <font color="#FF0000"></font></span></td>
                        <td valign="top"><textarea cols="28" rows="2" name="address"  id="address"></textarea><br><br>
        <span id="txt_mobile" style="font-size:11px;"></span></td>
    </tr>
                                        <tr>
        <td height="35" valign="top"><span style="font-size:13px; font-family:Arial; font-weight: normal;
color:#000000"><font color="#FF0000"><sup>*</sup></font>City:<font color="#FF0000"></font></span></td>
        <td valign="top"><input type="text" size="35" name="city" id="city" value=""></td></tr><br>

    <tr>
        <td height="35" valign="top"><span style="font-size:13px; font-family:Arial; font-weight: normal;
color:#000000"><font color="#FF0000"><sup>*</sup></font>Contact No:<font color="#FF0000"></font></span></td>
        <td valign="top"><input type="text" size="35" name="number" id="number" value=""></td></tr><br>
                                                <tr>
        <td height="35" valign="top"><span style="font-size:13px; font-family:Arial; font-weight: normal;
color:#000000"><font color="#FF0000"><sup>*</sup></font>Email Id:<font color="#FF0000"></font></span></td>
        <td valign="top"><input type="text" size="35" name="email" id="email" value=""></td></tr><br>

                                            <tr>
                                                <td height="35" valign="top"><span style="font-size:13px; font-family:Arial; font-weight: normal;
color:#000000"><font color="#FF0000"><sup>*</sup></font>Position Applied for:<font color="#FF0000"></font></span></td>
                                                <td style="HEIGHT: 21px"><select name="position" id="position" class="formtext">


    </select>

</td></tr><br>

                                            <tr>
                                                <td style="WIDTH: 152px" vAlign="top"><span id="Label4" class="text" class="text"> 
              <span style="font-size:13px; font-family:Arial; font-weight: normal;
color:#000000">Current organisation</span></span></td>
                                                <td><input type="text" size="35" name="organization" id="organization" value=""></td>
                                            </tr><br>
                                            <tr>
                                                <td style="WIDTH: 152px; HEIGHT: 43px" vAlign="top"><span id="Label5" class="text" class="text"> 
              <span style="font-size:13px; font-family:Arial; font-weight: normal;
color:#000000">Designation</span></span></td>
                                                <td style="HEIGHT: 43px"><input type="text" size="35" name="designation" id="designation" value=""></td>
                                            </tr><br>
                                            <tr>
                                                <td style="WIDTH: 152px; HEIGHT: 23px" vAlign="top"><span lang="EN-GB" style="FONT-FAMILY: 'Times New Roman'; FONT-SIZE: 11pt; mso-fareast-font-family: 'Times New Roman'; mso-ansi-language: EN-GB; mso-fareast-language: EN-US; mso-bidi-language: AR-SA"><span id="Label32" class="text" class="text"><span style="font-size:13px; font-family:Arial; font-weight: normal;
color:#000000">Total 
              work experience </span></span></span></td>
                                                <td><select name="ExperienceYears" id="ExperienceYears" class="formtext">

</select><select name="ExperienceYears" id="ExperienceMonths" class="formtext">

    </select></td>
                                            </tr><br>
                                            <tr>
                                                <td vAlign="top" width="100%" colSpan="2">
                                                    <table id="Table11" cellSpacing="0" cellPadding="1" width="100%" border="0">
                                                        <tr>
                                                            <td class="text" style="HEIGHT: 15px" vAlign="top"><span style="font-size:13px; font-family:Arial; font-weight: normal;
color:#000000">Resume</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                                            </td>

                                                            <td vAlign="top"><input name="file" type="file" id="file" class="FormText" />
                                                            </td>
                                                        </tr><br>
                                                    </table>
                                                </td>
                                            </tr>

                                            <!<tr>
                                                <td colSpan="2"><span id="Label13" class="text">Enter the text as it is shown in the box below</span></td>
                                            </tr>
                                            <tr>
                                                <td colSpan="2"><img id="Image1" src="../CreateCaptcha.aspx" style="border-width:0px;" /><input name="strCAPTCHA" type="text" maxlength="6" id="strCAPTCHA" class="formtext" style="width:112px;" /></td>
                                            </tr>
                                            <tr>
                                                <td align="center" colSpan="2"><input style="margin-left:50px;" type="submit" value="Submit" class="submit" onclick="return reguser_validation(frm);"/></td>
                                            </tr>
        <!-- InstanceEndEditable -->
                                        </table>
                                                        </TR>
                                                    </TABLE>
                                                </td>
                                            </tr>
<tbody>
</table>



</td>
</tr></td></tr></table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>

</table>
</td>
</tr>



</table>
</td>
</tr>
/* Validation for fields*/<br>
<script type="text/javascript" src="email_validate.js"></script><br>
    <script type="text/javascript" src="signup.js"></script><br>
            <script language="javascript"><br>
function reguser_validation(field) <br>
            {<br>   
                var name = document.frm.name;<br>
                var lname = document.frm.lname;<br>
                var city = document.frm.city;<br>
                var number = document.frm.number;<br>
                var email = document.frm.email;<br>
                var position = document.frm.position;<br>

    if ((name.value == null) || (name.value == ''))<br>
                {
                    alert("Please enter your First name");   <br>  
                    name.focus();<br>
                    return false;<br>
                }<br>

if ((lname.value == null) || (lname.value == ''))<br>
                {
                    alert("Please enter your Last name"); <br>   
                    lname.focus();<br>
                    return false;<br>
                }

if ((city.value == null) || (city.value == ''))<br>
                {
                    alert("Please enter your City name"); <br>    
                    city.focus();<br>
                    return false;<br>
                }<br>

if ((number.value == null) || (number.value == ''))<br>
                {
                    alert("Please enter your Contact Number"); <br>    
                    number.focus();<br>
                    return false;<br>
                }
if(Mobile_Validate(number) == false)<br>
                {
                    return false;<br>
                }<br>

            if ((email.value == null) || (email.value == '')){<br>
                    alert("Please enter your Email ID.");<br>
                    email.focus();<br>
                    return false;<br>
                }<br>

                if (emailcheck(email.value) == false){<br>
                    alert("Please Enter Valid Email ID.");<br>
                    email.focus();<br>
                    return false;<br>
                }<br>


                if (position.value ==  '')<br>
                {                   
                    alert('Please Enter the position that your applying for');<br>
                    position.focus();<br>
                    return false;<br>
                }
                <br>
                return true;<br>
            }
            <br>

这是我的谢谢你.php:

<?php

$to = "web@adisysonline.com";<br>
$subject = "Uploaded file";<br>

$fileatt = $_FILES['file']['tmp_name'];<br>
$fileatt_type = $_FILES['file']['type'];<br>
$fileatt_name = $_FILES['file']['name'];<br>
$headers = "From: $from";<br>

if (is_uploaded_file($fileatt))<br> 
{
    // Read the file to be attached ('rb' = read binary)<br>
    $file = fopen($fileatt, 'rb');<br>
    $data = fread($file, filesize($fileatt));<br>
    $semi_rand = md5(time()); <br>
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; <br>
    $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";<br>
    $mail_body = "This is a multi-part message in MIME format.\n\n" . "--<br>{$mime_boundary}\n" . "Content-Type: text/html; charset=\"iso-8859-1\"\n" .<br> 
    "Content-Transfer-Encoding: 7bit\n\n" . $mail_body . "\n\n";<br>
    $data = chunk_split( base64_encode($data));<br>
    $mail_body .= "--{$mime_boundary}\n" . "Content-Type: {$fileatt_type};\n" . " <br>name=\"{$fileatt_name}\"\n" . "Content-Disposition: attachment;\n" . 
    " filename=\"{$fileatt_name}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" . "--{$mime_boundary}--\n"; <br>

if ($_SERVER['REQUEST_METHOD'] == "POST") {<br>
    foreach ($_POST as $key => $value) { <br>
        $msg .= ucfirst ($key) ." : ". $value . "\n"; <br>
    }<br>
}<br>
else {<br>
    foreach ($_GET as $key => $value) { <br>
        $msg .= ucfirst ($key) ." : ". $value . "\n"; <br>
    }<br>
}



    $ok = @mail($to, $subject, $msg, $mail_body, $headers);<br>

    if($ok)<br>
        echo "<b>Thank you for submitting Your Resume.<br> We will get back to you as soon as possible.</b>";<br>
    else<br>
        echo "Server Busy please try after few minutes";<br>

    fclose($file);<br>
}<br>
?><br>

任何帮助都是有效的......谢谢......等待专业知识回复......

1 个答案:

答案 0 :(得分:3)

  $file1=$_FILES['filenm']['name'];
    $to = "example@gmail.com";
                        $subject = "your subject";
                        $base = basename($file1);
    $file = fopen('your folder name/'.$file1,'rb');
    $size = filesize('your folder name/'.$file1);
    $data = fread($file,$size);
    fclose($file);
    $data = chunk_split(base64_encode($data));
    $message="<html><body>";
    $message="<table border='1'>";
    $message.="<tr><td colspan='3' align='center' style='color:#FFFFFF;font-size:large; background:#000000'>Information</td></tr>";
    $message.="</table>";
    $message.="</body></html>";
    //boundary
    $div = "==Multipart_Boundary_x".md5(time())."x";
    //headers
    $head = "From: $email\n".
        "MIME-Version: 1.0\n".
        "Content-Type: multipart/mixed;\n".
        " boundary=\"$div\"";
    //message
    $mess = "--$div\n".
        "Content-Type: text/html; charset=\"iso-8859-1\"\n".
        "Content-Transfer-Encoding: 7bit\n\n".
        "$message\n\n".
        "--$div\n".
        "Content-Type: application/octet-stream; name=\"$base\"\n".
        "Content-Description: $base\n".
        "Content-Disposition: attachment;\n".
        " filename=\"$base\"; size=$size;\n".
        "Content-Transfer-Encoding: base64\n\n".
        "$data\n\n".
        "--$div\n";
    $return = "-f$email";
    mail($to,$subject,$mess,$head,$return);

试试这段代码 我使用此代码发送任何类型附件的邮件