簡體   English   中英

使用php發送電子郵件形式的附件和數據

[英]send attachments and data in form in an email with php

這是我的PHP代碼

我的html表單位於下面,我要從名稱,電子郵件,電話號碼字段和文件附件中獲取數據。

現在,我嘗試在實時服務器環境中獲取附件文件(未輸入其他信息),我也以不同的格式獲取郵件附件(例如我發送了word文檔,但word文件為dat格式,但未顯示) Word文件中的任何數據)

<?php
    if(isset($_POST['submit']))
    {

        //The form has been submitted, prep a nice thank you message
        $output = '<h1>Thanks for your file and message!</h1>';
        //Set the form flag to no display (cheap way!)
        $flags = 'style="display:none;"';

        //Deal with the email
        $to = 'xxxx@gmail.com';
        $subject = 'Details and file attachments ';

        $message = strip_tags($_POST['message']);
        $attachment = (file_get_contents($_FILES['file']['tmp_name']));
        $filename = $_FILES['file']['name'];

        $boundary =md5(date('r', time())); 

        $headers = "From: xxxxxx@gmail.com\r\nReply-To: xxxxxxx@gmail.com";
        $headers .= "\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"_1_$boundary\"";

        $message="This is a multi-part message in MIME format.

--_1_$boundary
Content-Type: multipart/alternative; boundary=\"_2_$boundary\"

--_2_$boundary
Content-Type: text/plain; charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit

$message

--_2_$boundary--
--_1_$boundary
Content-Type: application/octet-stream; name=\"$filename\" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 

$attachment
--_1_$boundary--";

        mail($to, $subject, $message, $headers);
    }
?>

這是我的html文件,建議我如何修改代碼以獲取其他字段的值以及通過電子郵件發送

    <td height="12"> <?php echo $output; ?>
<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" <?php echo $flags;?>>
            <fieldset>
                <legend>Submit your interest</legend>
                <p><label class="title" for="name">Your name:</label>
                     <input type="text" name="name" id="name"><br /></p>
                <p>  <label class="title" for="email">Your email:</label>
                     <input type="text" name="email" id="email"></p>
                <p>  <label class="title" for="phone">Your phone:</label>
                     <input type="number" name="phone" id="phone"></p>
                <p><label for="location" class="title">Total Experience :</label>
                     <select name="location" id="location">
                         <option value="ny">1</option>
                         <option value="il">2</option>
                         <option value="ca">3+</option>
                     </select></p>
                <span class="title">Upload your resume</span>
                <input type="file" name="resume" /> 
                </p>
            </fieldset>
        <div><input type="submit" name="submit" id="submit" value="send"></div>
        </form></td>
 Content-Transfer-Encoding: base64 

您的標頭說的是數據為base64格式,但實際內容為原始二進制。 在包含正文之前,請嘗試使用base64_encode($attachment)


也就是說,請不要手動執行此操作。 正如評論中提到的,圖書館會為您處理所有這一切。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM