如何在php中通过电子邮件发送文件附件(我的代码有什么问题)

如何在php中通过电子邮件发送文件附件(我的代码有什么问题),php,email-attachments,Php,Email Attachments,我对php相当陌生,不熟悉电子邮件标题。这是一个基于我遇到/更新的一些代码的函数,以使用文件对象。结果是“成功发送的文件得到回音,但电子邮件从未出现。”。我只能假设这与它生成的电子邮件头有关。希望有人看到这里出了什么问题: function mail_attachment ($from , $to, $subject, $message, $attachment){ $fileatt = $attachment["tmp_name"]; $fileatt_type = "app

我对php相当陌生,不熟悉电子邮件标题。这是一个基于我遇到/更新的一些代码的函数,以使用文件对象。结果是“成功发送的文件得到回音,但电子邮件从未出现。”。我只能假设这与它生成的电子邮件头有关。希望有人看到这里出了什么问题:

function mail_attachment ($from , $to, $subject, $message, $attachment){ 
    $fileatt = $attachment["tmp_name"];
    $fileatt_type = "application/octet-stream";
    $fileatt_name = $attachment["name"]; 
    $email_from = $from;
    $subject = "New Attachment Message"; 
    $email_subject = $subject;
    $email_to = $to;
    $headers = "From: ".$email_from;   
    $file = fopen($fileatt,'rb'); 
    $data = fread($file,filesize($fileatt)); 
    fclose($file);
    $semi_rand = md5(time()); 
    $mime_boundary = "Multipart_Boundary_x{$semi_rand}x"; 
    $headers .= "\nMIME-Version: 1.0\n" . 
                "Content-Type: multipart/mixed;\n" . 
                "boundary=\"{$mime_boundary}\"";
    $email_message .= "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . 
                "Content-Type:text/html; charset=\"iso-8859-1\"\n" . 
                "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; 
    $data = chunk_split(base64_encode($data)); 
    $email_message .= "--{$mime_boundary}\n" . 
                "Content-Type: {$fileatt_type};\n" . 
                "name=\"{$fileatt_name}\"\n" . 
                "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" 
                . "--{$mime_boundary}--\n";   
    $ok = mail($email_to, $email_subject, $email_message, $headers);   
    if($ok) { echo "File Sent Successfully.";} 
    else { die("Sorry but the email could not be sent. Please go back and try again!"); } 
}



窃取代码然后请求SO为您修复?您应该使用@Sean Why<代码>邮件工作得很好。它被大量修改,我不是傻瓜,我只是不知道标题有什么问题。这是你的决定,使用这样繁琐的代码。所以请自己调试。其他人都应该使用。
<?php 
require_once('phpmailer.php');

$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
$mail->IsSendmail(); // telling the class to use SendMail transport

try {
    $mail->AddReplyTo('email@example.com', 'First Last');
    $mail->AddAddress('John@example.com', 'John Doe');

    $mail->SetFrom('email@example.com', 'First Last');
    $mail->Subject  =  "Subject Line";
    $mail->AltBody    = "Alternate Text"; // optional, comment out and test

    $mail->WordWrap     =   50; // set word wrap    
    $mail->Body = "This is the body of the email";
    $mail->IsHTML(true); // send as HTML

    // Single or Multiple File Attachments
    $mail->AddAttachment('../path-to-file.pdf', 'File-Name.pdf');
    $mail->Send(); // Try to send email
    //echo "Message Sent OK<p></p>\n";
    } catch (phpmailerException $e) {
        echo $e->errorMessage(); //Pretty error messages from PHPMailer
    } catch (Exception $e) {
        echo $e->getMessage(); //Boring error messages from anything else!
}
// end try


?>