【问题标题】:How can I get an attachment in a PHP e-mail form? [duplicate]如何在 PHP 电子邮件表单中获取附件? [复制]
【发布时间】:2017-07-20 17:18:04
【问题描述】:

我在网页上有一个简单的表单。我希望填写表格的人能够上传文件。然后我希望将消息和附件通过电子邮件发送给我。几天来我一直在玩很多代码,但没有找到任何适合我的东西。

我当前的代码:

<?php

    if ($_SERVER['REQUEST_METHOD'] == 'POST'){

        $flags = 'style="display:none;"';
        $message = $_POST['message'];
      $email="me@me.com";
        $to = "someone@somewhere.com";
        $subject = "test Message with attachment";

        $attachment = chunk_split(base64_encode(file_get_contents($_FILES['file']['uploaded_file'])));
        $filename = $_FILES['file']['uploaded_file'];

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

$headers  = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=utf-8" . "\r\n";
$headers .= "From: ".$email."\r\n";  
$headers .= "Reply-To: ".$email."\r\n" .

 $mail_sent = @mail($to, $subject, $message, $headers);  
 //if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
 echo $mail_sent ? "Mail sent" : "Mail failed";
 exit();

    } // end of what happens if form submitted
else{
?>
<!-- end of form processing-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test e-mail attachment form</title>
</head>
<body>

<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" <?php echo $flags;?>>
    <input type="hidden" name="status" value="sent">
    <table >
    <tr>
        <td>
            Message:
        </td>
        <td>
             <input type="text" name="message" size="20" required>
        </td>
    </tr>


    <tr>
        <td>
            Attach a file: 
        </td>
        <td>

<input type="file" name="uploaded_file">
                </td>
    </tr>
        <tr>
        <td colspan="2">
            <input type="submit" value="Send message">
        </td>
    </tr>
</table>

</form>

</body>
</html>
<?php
}
?>

消息通过正常,但没有附件。我尝试了很多不同的方法,包括在此处找到的几种方法,例如 [Simple PHP form: Attachment to email (code golf),但无法得到我想要的。

我还尝试了在 [http://form.guide/email-form/php-email-form-attachment.html][1] 上找到的东西,但这涉及安装 PEAR。我确实尝试过,但后来发现它覆盖了我的主页,所以我不得不卸载它,因为它似乎对我的需要来说太复杂和过度杀伤了。

我确信我一定在某个地方附近可以让它工作,但看不出有什么问题。

【问题讨论】:

标签: php forms email email-attachments


【解决方案1】:

我建议你使用PHPMailer 库。
您可能想使用send_file_upload 示例。
此外,还列出了很多示例here.

因此,您的代码可能会这样重写:

<?php
if( $_SERVER['REQUEST_METHOD'] == 'POST' )
{
    $flags = 'style="display:none;"';
    $message = $_POST['message'];
    $email="me@me.com";
    $to = "someone@somewhere.com";
    $subject = "test Message with attachment";
    $uploadfile = tempnam(sys_get_temp_dir(), sha1($_FILES['file']['name']));
    $msg = '';

    if( move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile) )
    {
        // Upload handled successfully
        // Now create a message
        // This should be somewhere in your include_path
        require '../PHPMailerAutoload.php';
        $mail = new PHPMailer;
        $mail->setFrom($email);
        $mail->addAddress($to);
        $mail->Subject = $subject;
        $mail->Body = $message;

        // Attach the uploaded file
        $mail->addAttachment($uploadfile, 'My uploaded file');
        if (!$mail->send()) {
            $msg .= "Mailer Error: " . $mail->ErrorInfo;
        } else {
            $msg .= "Message sent!";
        }
    } else
    {
        $msg .= 'Failed to move file to ' . $uploadfile;
    }

    exit( $msg );
} else { ?>
<!-- end of form processing-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test e-mail attachment form</title>
</head>
<body>

<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" <?php echo $flags;?>>
    <input type="hidden" name="status" value="sent">
    <table >
    <tr>
        <td>
            Message:
        </td>
        <td>
             <input type="text" name="message" size="20" required>
        </td>
    </tr>


    <tr>
        <td>
            Attach a file: 
        </td>
        <td>

<input type="file" name="uploaded_file">
                </td>
    </tr>
        <tr>
        <td colspan="2">
            <input type="submit" value="Send message">
        </td>
    </tr>
</table>

</form>

</body>
</html>
<?php }?>

附言使用此类库将为您提供更灵活和更有效的代码。
仅仅因为它们形成了所有必需的标头并提供了简单的 OOP 接口。

【讨论】:

    猜你喜欢
    • 2012-08-23
    • 1970-01-01
    • 1970-01-01
    • 2010-09-12
    • 1970-01-01
    • 2017-10-09
    • 1970-01-01
    • 2020-11-06
    • 2014-03-19
    相关资源
    最近更新 更多