【问题标题】:send attachments and data in form in an email with php使用 php 以电子邮件形式发送附件和数据
【发布时间】:2015-07-14 15:57:32
【问题描述】:

这是我的 PHP 代码

我的 html 表单在下面,我想从姓名、电子邮件、电话号码字段和文件附件中获取数据。

现在我尝试在实时服务器环境中获取附加文件(没有输入其他信息),我也在邮件中以不同格式获取附件(比如我发送了一个 word doc,但 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>

【问题讨论】:

  • 不要建立自己的 mime 电子邮件。使用 phpmailer 或 swiftmailer。特别是因为您将文件的 RAW 内容填充到电子邮件中,但声称它是 base64 编码的......

标签: php html email phpmailer


【解决方案1】:
Content-Transfer-Encoding: base64 

您的标题说数据是base64 格式,但实际内容是原始二进制文件。在包含正文之前尝试使用base64_encode($attachment)


也就是说,请不要手动执行此操作。正如 cmets 中提到的,图书馆会为您处理所有这些。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-16
    • 1970-01-01
    • 2012-04-29
    • 1970-01-01
    相关资源
    最近更新 更多