简体   繁体   English

通过电子邮件发送附件

[英]Sending attachment(s) in email

I've never touched PHP, but was tasked with fixing an Intern's code.. 我从没接触过PHP,但是却要负责修复Intern的代码。

I am trying to attach a file being uploaded to an email that I am sending. 我正在尝试将要上传的文件附加到我发送的电子邮件中。 The email sends, but without the file. 电子邮件发送,但没有文件。 I am using PHPMailerAutoUpload.php (found on GitHub). 我正在使用PHPMailerAutoUpload.php(在GitHub上找到)。

Here's the code I am using. 这是我正在使用的代码。

The attachment is being saved via move_uploaded_file 附件通过move_uploaded_file保存

move_uploaded_file( $resume['tmp_name'] , $up_dir .basename( $random_var . '_wse_' . $resume['name'] ) )

Note: I have commented out the move_uploaded_file function to make sure I wasn't getting rid of the attachment. 注意:我已经注释掉了move_uploaded_file函数,以确保我没有摆脱附件。

        require_once('phpmailer/PHPMailerAutoload.php');
        $mail = new PHPMailer(true);
        $mail->IsSMTP();
        $mail->SMTPDebug = 2;
        $mail->SMTPAuth = false;
        $mail->Host = 'oursmtp';
        $mail->Port = 25;

        $mail->setFrom( $_POST['E-mail'] , $_POST['first_name'] . " " . $_POST['last_name'] );
        $mail->addAddress( 'test@test.com' );
        $mail->Subject = "Test" . @date('M/D/Y');
        $mail->msgHTML($msgDoc);

        if (isset($_FILES['uploaded_file']) &&
        $_FILES['uploaded_file']['error'] == UPLOAD_ERR_OK) {
        $mail->AddAttachment($_FILES['uploaded_file']['tmp_name'],
                             $_FILES['uploaded_file']['name']);
        }


        if (!$mail->send()) {
            $mailError = $mail->ErrorInfo;
            $outcomeArr = array(
                                'outcome'=>'failure',
                                'message'=>'Error' . $mailError
                            );              
            echo json_encode( $outcomeArr );
            exit();             
        } else {
            // success
            $outcomeArr = array(
                                'outcome'=>'success',
                                'message'=>'Thank you'
                            );      
            echo json_encode( $outcomeArr );
        }

From what I have read, $_FILES is a temporary storage for uploaded files in PHP. 据我了解, $_FILES _ $_FILES是PHP中上传文件的临时存储。 With this code, the email sends, but with no attachment (only a link to the uploaded file location). 使用此代码,电子邮件将发送,但没有附件(只有指向上载文件位置的链接)。

I tried following this , but it isn't working for me. 我尝试遵循此方法 ,但是它对我不起作用。

Your intern was apparently a rockstar that had no need to check for or indicate error conditions, and then mail will send even if there's no attachment or if there was an error during the upload. 您的实习生显然是摇滚明星,不需要检查或指出错误情况,即使没有附件或在上传过程中出现错误,邮件也会发送出去。 Change these bits to the code to tell you why the file wasn't attached: 将这些位更改为代码,以告诉您为什么未附加文件:

if (isset($_FILES['uploaded_file']) && $_FILES['uploaded_file']['error'] == UPLOAD_ERR_OK) {
    if( ! $mail->AddAttachment($_FILES['uploaded_file']['tmp_name'], $_FILES['uploaded_file']['name']) ) {
        echo 'Error adding attachment: ' . $mail->ErrorInfo;
    }
} else if( !isset($_FILES['uploaded_file']) ) {
    echo 'No uploaded file found';
} else {
    echo 'Uploaded file error: ' . $_FILES['uploaded_file']['error'];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM