duannuan0074 2013-01-08 20:13
浏览 76
已采纳

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

I am fairly new to php, and am unfamiliar with email headers. This is a function based on some code I came across / updated to use a file object. The result is a 'File Sent Successfully gets echoed, but the email never comes. I can only assume this has something to do with the email headers that this thing is generating. Hopefully somebody see's what's wrong here:

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 .= "
MIME-Version: 1.0
" . 
                "Content-Type: multipart/mixed;
" . 
                "boundary=\"{$mime_boundary}\"";
    $email_message .= "This is a multi-part message in MIME format.

" . "--{$mime_boundary}
" . 
                "Content-Type:text/html; charset=\"iso-8859-1\"
" . 
                "Content-Transfer-Encoding: 7bit

" . $message . "

"; 
    $data = chunk_split(base64_encode($data)); 
    $email_message .= "--{$mime_boundary}
" . 
                "Content-Type: {$fileatt_type};
" . 
                "name=\"{$fileatt_name}\"
" . 
                "Content-Transfer-Encoding: base64

" . $data . "

" 
                . "--{$mime_boundary}--
";   
    $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!"); } 
}
  • 写回答

1条回答 默认 最新

  • dtf1111 2013-01-08 20:19
    关注

    Use phpMailer()

    <?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>
    ";
        } 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
    
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题