简体   繁体   English

通过PHP PEAR发送电子邮件附件

[英]Send Email Attachment through PHP PEAR

I really need your help. 我真的需要你的帮助。 Please find my code below : 请在下面找到我的代码:

if (isset($_FILES['file']) and $_FILES['file'] <> '') { // Jika email disertai attachment
        $crlf = "\n";

        $headers['MIME-Version'] =  '1.0';
        $headers['Content-Type'] =  'text/html; charset="UTF-8';
        $headers['Content-Transfer-Encoding'] =  'base64\r\n';
        $headers['Date'] =  date('r', $_SERVER['REQUEST_TIME']);
        $headers['Message-ID'] =  $_SERVER['REQUEST_TIME'] . md5($_SERVER['REQUEST_TIME']) . '@' . $_SERVER['SERVER_NAME'];
        $headers['From']    = "do-not-reply@sbm-itb.ac.id";
        $headers['To']      = $to;
        $headers['Subject'] = $subject;

        $mime = new Mail_mime(array('eol' => $crlf));

        $mime->setHTMLBody($body);
        foreach($_FILES['file']['tmp_name'] as $key => $value){
            $file_name = $_FILES['file']['name'][$key]; //nama file (tanpa path)
            $tmp_name  = $_FILES['file']['tmp_name'][$key]; //nama local temp file di server
            $file_type = $_FILES['file']['type'][$key]; //tipe filenya (langsung detect MIMEnya)

            $fp      = fopen($tmp_name, 'r');
            $content = fread($fp, filesize($tmp_name));
            $content = addslashes($content);
            fclose($fp);
            $data = chunk_split(base64_encode($content));

            //$mime->addAttachment($tmp_name, $file_type);
            $mime->addAttachment($data, $file_type);
        }
            $body = $mime->get();
            $headers = $mime->headers($headers);

I'm success to send my HTML text mail. 我成功发送了HTML文本邮件。 But, without my uploaded attachments. 但是,没有我上传的附件。 What's wrong with my code above? 我上面的代码有什么问题?

Thank you. 谢谢。

I don't see your mail function on here? 我在这里看不到您的邮件功能吗? Try something like this: 尝试这样的事情:

Uploading the attachment: 上载附件:

$max_allowed_file_size = 1250; // size in KB 
$allowed_extensions = array("jpg", "jpeg", "gif", "bmp", "png");
$upload_folder = './uploads/'; //<-- this folder must be writeable by the script
$path_of_uploaded_file = $upload_folder . $name_of_uploaded_file;

$tmp_path = $_FILES["uploaded_file"]["tmp_name"];
//Get the uploaded file information
$name_of_uploaded_file =  basename($_FILES['uploaded_file']['name']);

//get the file extension of the file
$type_of_uploaded_file = substr($name_of_uploaded_file, 
                        strrpos($name_of_uploaded_file, '.') + 1);

$size_of_uploaded_file = $_FILES["uploaded_file"]["size"]/2048;

The mailing of the attachments, headers, etc. 附件,标题等的邮寄

 $message = new Mail_mime(); 
    $message->setHTMLBody($text); 
    $message->addAttachment($path_of_uploaded_file);
    $body = $message->get();
    $extraheaders = array("From"=>$from, "Subject"=>$subject,"Reply-To"=>$visitor_email);
    $headers = $message->headers($extraheaders);
    $mail = Mail::factory("mail");
    $mail->send($to, $headers, $body);

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

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