【问题标题】:Submit a form with a file and mail that as an attachment using PHP使用 PHP 提交带有文件和邮件作为附件的表单
【发布时间】:2015-03-04 12:22:48
【问题描述】:

我是 php 新手,我尝试设置一个提交表单。我希望能够上传文件并附加文件并将其与我的消息一起发送。我可以接收所有消息,但没有收到文件。我对消息使用 nl2br 函数。我会很感激任何帮助,谢谢。

【问题讨论】:

    标签: php email message attachment


    【解决方案1】:

    您是否定义了enctype 的表单?您需要这样做才能发送带有表单的文件。

    你的某些代码对理解问题有很大帮助,但我猜你没有指定enctype。它应该看起来像这样:

    <form action="file.php" method="post" enctype="multipart/form-data">
    

    【讨论】:

    • 感谢您的回复,不,我没有定义 enctype。我试试看:)
    【解决方案2】:

    您是否在表单中添加了 enctype="multipart/form-data":

    <form action="action_page.php" method="POST" enctype="multipart/form-data">
    </form>
    

    【讨论】:

    • 感谢您抽出宝贵时间,我现在就添加它
    【解决方案3】:

    在 HTML 中添加 &lt;form&gt; 属性 enctype="multipart/form-data"。

    然后,上传文件:&lt;input type="file" name="myfile" /&gt;

    然后在 PHP 中:

    不要使用 PHP mail() 函数直接邮寄,而是使用 PHPMailer。它会让你很容易,自己处理所有这些。您只需调用几个函数就可以了。

    这是一个示例:

    <?php
    require("class.phpmailer.php");
    
    $mail = new PHPMailer();
    
    $mail->IsSMTP();  // telling the class to use SMTP
    $mail->Host     = "smtp.example.com"; // SMTP server
    
    $mail->From     = "from@example.com";
    $mail->AddAddress("myfriend@example.net");
    
    $mail->Subject  = "An HTML Message";
    $mail->Body     = "Hello, <b>my friend</b>! \n\n This message uses HTML entities!";
    
    ?>
    

    如需完整教程,请访问http://phpmailer.worxware.com/?pg=tutorial

    编辑:看这里,查看视频系列:http://bit.ly/1Ekgzel

    希望对你有帮助。

    【讨论】:

    • 这有什么关系?
    • 感谢您的回复。本教程似乎是我需要的。我可以发送任何消息,但问题在于我的附件
    • @Forien 实际上我误解了这个问题,因为 OP 提到“对 PHP 来说是新手”。我认为他无法将附件与电子邮件一起发送。我已经更正了,希望如此。
    • @user3451793 很高兴我能帮上忙。
    • 我的意思是我不想让某人给我原始代码,我需要教程,因为首先我想学习然后做我的工作
    猜你喜欢
    • 2012-03-01
    • 2015-03-13
    • 2013-06-25
    • 1970-01-01
    • 2013-01-28
    • 1970-01-01
    • 1970-01-01
    • 2017-09-29
    • 1970-01-01
    相关资源
    最近更新 更多