ถ้าต้องการจะส่งเมล เเล้วแนบไฟล์ไปด้วยใน phpbb ทำยังไงคะ

สำหรับผู้ที่ เริ่มต้น Programming - PHP มีอะไร แนะนำ หรือข้อสงสัยต้องบอร์ด นี้ คนที่มีความรู้ แบ่งปันคนอื่นบ้างนะ ปัญหาการเขียนโปรแกรม แบบ OOP Session Cookies php network

Moderator: mindphp, ผู้ดูแลกระดาน

ภาพประจำตัวสมาชิก
thatsawan
PHP VIP Members
PHP VIP Members
โพสต์: 28508
ลงทะเบียนเมื่อ: 31/03/2014 10:02 am
ติดต่อ:

ถ้าต้องการจะส่งเมล เเล้วแนบไฟล์ไปด้วยใน phpbb ทำยังไงคะ

โพสต์ที่ยังไม่ได้อ่าน โดย thatsawan »

โค้ด: เลือกทั้งหมด

 include_once($this->phpbb_root_path . 'includes/functions_messenger.' . $this->phpEx);

            $messenger = new \messenger(FALSE);
            $messenger->template($email_template, $data['user_lang']);
            $messenger->to($data['reg_email'], $data['username']);
            $messenger->anti_abuse_headers($this->config, $this->user);
            $messenger->assign_vars(array(
                'SITENAME' => htmlspecialchars_decode($this->config['sitename']),
                'USERNAME' => htmlspecialchars_decode($data['username']),
                'PACK_NAME' => htmlspecialchars_decode($package_name),
                'DD_MM_YY' => htmlspecialchars_decode(date("d", $date_end_new) . "/" . date("m", $date_end_new) . "/" . (date("Y", $date_end_new))),
            ));

            $messenger->send(NOTIFY_EMAIL);
ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41127
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

Re: ถ้าต้องการจะส่งเมล เเล้วแนบไฟล์ไปด้วยใน phpbb ทำยังไงคะ

โพสต์ที่ยังไม่ได้อ่าน โดย mindphp »

ทำเหมือนกับ pm
ACP->general tab->private message ตั้งค่าให้แนปไฟล์ ได้ แล้วเขียนแนบไฟล์ เข้าไป

หรือ ลองหา จาก การกำหนดส่วนของ headers เข้าไป
https://area51.phpbb.com/docs/code/3.1. ... od_headers
หรือต้อง override method สำหรับ ส่งเมล

หรือใช้ class อื่นที่กดปุ่มแล้วส่งเมล เลยเหมือน php ทั่วไป
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41127
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

Re: ถ้าต้องการจะส่งเมล เเล้วแนบไฟล์ไปด้วยใน phpbb ทำยังไงคะ

โพสต์ที่ยังไม่ได้อ่าน โดย mindphp »

ตัวอย่าง ฟังก์ชั่นเมลแนบไฟล์ และ การกำหนด headers เองเป็นไฟล์แนป

โค้ด: เลือกทั้งหมด

<?php
function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
 $file = $path.$filename;
 $file_size = filesize($file);
 $handle = fopen($file, "r");
 $content = fread($handle, $file_size);
 fclose($handle);
 $content = chunk_split(base64_encode($content));
 $uid = md5(uniqid(time()));
 $header = "From: ".$from_name." <".$from_mail.">\r\n";
 $header .= "Reply-To: ".$replyto."\r\n";
 $header .= "MIME-Version: 1.0\r\n";
 $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
 $header .= "This is a multi-part message in MIME format.\r\n";
 $header .= "--".$uid."\r\n";
 $header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
 $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
 $header .= $message."\r\n\r\n";
 $header .= "--".$uid."\r\n";
 $header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here
 $header .= "Content-Transfer-Encoding: base64\r\n";
 $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
 $header .= $content."\r\n\r\n";
 $header .= "--".$uid."--";
 if (mail($mailto, $subject, "", $header)) {
 echo "mail send ... OK"; // or use booleans here
 } else {
 echo "mail send ... ERROR!";
 }
}
การเอาไปใช้

โค้ด: เลือกทั้งหมด

$my_file = "somefile.zip";
$my_path = "/your_path/to_the_attachment/";
$my_name = "Olaf Lederer";
$my_mail = "[email protected]";
$my_replyto = "[email protected]";
$my_subject = "This is a mail with attachment.";
$my_message = "Hallo,rndo you like this script? I hope it will help.rnrngr. Olaf";
mail_attachment($my_file, $my_path, "[email protected]", $my_mail, $my_name, $my_replyto, $my_subject, $my_message);
ส่วนนี้ สามารถถอด มาใช้ร่วมกับ phpBB ได้ กำหนดรายละเอียด header เพิ่มเติม

โค้ด: เลือกทั้งหมด

 $header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here
 $header .= "Content-Transfer-Encoding: base64\r\n";
 $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
โดยที่ไฟล์ $filename หรือไฟล์ ไฟล์แนบที่เราอ่านมาจากเนื้อหาไฟล์
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
ภาพประจำตัวสมาชิก
thatsawan
PHP VIP Members
PHP VIP Members
โพสต์: 28508
ลงทะเบียนเมื่อ: 31/03/2014 10:02 am
ติดต่อ:

Re: ถ้าต้องการจะส่งเมล เเล้วแนบไฟล์ไปด้วยใน phpbb ทำยังไงคะ

โพสต์ที่ยังไม่ได้อ่าน โดย thatsawan »

โค้ด: เลือกทั้งหมด

 public function modify_email_headers($event) {

        $headers = $event['headers'];
        $filename = 'receipt.pdf';
        $file = '../images/receipt/' . $filename;
        $file_size = filesize($file);
        $handle = fopen($file, "r");
        $content = fread($handle, $file_size);
        fclose($handle);
        $content = chunk_split(base64_encode($content));
        
        $uid = md5(uniqid(time()));
        $headers[] = "Content-Type: application/octet-stream; name=\"" . $filename . "\"\r\n"; // use different content types here
        $headers[] = "Content-Transfer-Encoding: base64\r\n";
        $headers[] = "Content-Disposition: attachment; filename=\"" . $filename . "\"\r\n\r\n";
        $headers[] = $content . "\r\n\r\n";
        $headers[] = "--" . $uid . "--";
        $event['headers'] = $headers;

//        $mimetype = 'application/pdf';
//        $filetype = 'PDF';
//        $headers[] = "Cache-Control: private, no-cache";
//        $headers[] = "Content-Type: $mimetype; name=\"data.$filetype\"";
//        $headers[] = "Content-disposition: attachment; filename=data.$filetype";
//        $headers[] = $content . "\r\n\r\n";
//        $event['headers'] = $headers;
    }
ลองใส่ header ใน event มีการส่ง PDF ลงไปในเมล เเล้ว เเต่ไฟล์เปิดไม่ได้
ภาพประจำตัวสมาชิก
thatsawan
PHP VIP Members
PHP VIP Members
โพสต์: 28508
ลงทะเบียนเมื่อ: 31/03/2014 10:02 am
ติดต่อ:

Re: ถ้าต้องการจะส่งเมล เเล้วแนบไฟล์ไปด้วยใน phpbb ทำยังไงคะ

โพสต์ที่ยังไม่ได้อ่าน โดย thatsawan »

ลอง วิธีนี้

โค้ด: เลือกทั้งหมด

  include_once($this->phpbb_root_path . 'includes/functions_messenger.' . $this->phpEx);
        $filename = 'receipt.pdf';
        $file = '../images/receipt/' . $filename;
        $file_size = filesize($file);
        $handle = fopen($file, "r");
        $content = fread($handle, $file_size);
        fclose($handle);
        $content = chunk_split(base64_encode($content));

        $uid = md5(uniqid(time()));

        $file = '../images/receipt.pdf';


        if ($this->config['email_enable']) {

            $email_template = 'notification_member_package_email';
            include_once($this->phpbb_root_path . 'includes/functions_messenger.' . $this->phpEx);
            $messenger = new \messenger(FALSE);

            $messenger->template($email_template, $data['user_lang']);

            $messenger->to('[email protected]', $data['username']);
            $messenger->anti_abuse_headers($this->config, $this->user);


            $messenger->headers("Content-Type: application/octet-stream; name=\"" . $filename . "\"\r\n");
            $messenger->headers("Content-Transfer-Encoding: base64\r\n");
           // $messenger->headers("Content-Disposition: attachment; filename=\"" . $filename . "\"\r\n\r\n");


            $messenger->assign_vars(array(
                'SITENAME' => htmlspecialchars_decode($this->config['sitename']),

                'PACK_NAME' => $content,#htmlspecialchars_decode($content),

            ));

            $messenger->send(NOTIFY_EMAIL);
            return True;
        }

โค้ด: เลือกทั้งหมด

  $messenger->headers("Content-Type: application/octet-stream; name=\"" . $filename . "\"\r\n");
            $messenger->headers("Content-Transfer-Encoding: base64\r\n");
ผลที่ได้
Programming - PHP-1.png
Programming - PHP-1.png (28.63 KiB) Viewed 1016 times

โค้ด: เลือกทั้งหมด

  $messenger->headers("Content-Type: application/octet-stream; name=\"" . $filename . "\"\r\n");
            $messenger->headers("Content-Transfer-Encoding: base64\r\n");
             $messenger->headers("Content-Disposition: attachment; filename=\"" . $filename . "\"\r\n\r\n");
ผลที่ได้
Programming - PHP-2.png
Programming - PHP-2.png (14.28 KiB) Viewed 1016 times
ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41127
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

Re: ถ้าต้องการจะส่งเมล เเล้วแนบไฟล์ไปด้วยใน phpbb ทำยังไงคะ

โพสต์ที่ยังไม่ได้อ่าน โดย mindphp »

การอ่านและแปลงข้อมูลในไฟล์ เป็น base64 php บางเวอร์ชั่น จะมีส่วน header หรือ text เพิ่มมาก่อน content ต้องตรวจเช็คในส่วนนี้ด้วย
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
ภาพประจำตัวสมาชิก
thatsawan
PHP VIP Members
PHP VIP Members
โพสต์: 28508
ลงทะเบียนเมื่อ: 31/03/2014 10:02 am
ติดต่อ:

Re: ถ้าต้องการจะส่งเมล เเล้วแนบไฟล์ไปด้วยใน phpbb ทำยังไงคะ

โพสต์ที่ยังไม่ได้อ่าน โดย thatsawan »

มันจะเหมือนเรา เอา file มาเเปลงเป็น base64 เเละ เปิดกลับ ใน HTML เป็น PDF ใช่มั้ย
ถ้าเราเอา file PDF มา ใส่ base64 แล้วเอากลับมา ผลได้เป็น php file เเสดงว่ามีปัญหาใช่มั้ย

โค้ด: เลือกทั้งหมด

  $encoded_file = chunk_split(base64_encode(file_get_contents($file)));
        echo header('Content-Type: application/pdf');
        echo base64_decode($encoded_file);
        exit;
Programming - PHP-1.png
Programming - PHP-1.png (11.74 KiB) Viewed 990 times
ภาพประจำตัวสมาชิก
thatsawan
PHP VIP Members
PHP VIP Members
โพสต์: 28508
ลงทะเบียนเมื่อ: 31/03/2014 10:02 am
ติดต่อ:

Re: ถ้าต้องการจะส่งเมล เเล้วแนบไฟล์ไปด้วยใน phpbb ทำยังไงคะ

โพสต์ที่ยังไม่ได้อ่าน โดย thatsawan »

ส่วนของ Header
Programming - PHP-1.png
Programming - PHP-1.png (31.44 KiB) Viewed 982 times
ลองเข้าไปแก้ไข header ของ phpbb มาตราฐาน

โค้ด: เลือกทั้งหมด

function build_header($to, $cc, $bcc) {
        global $config, $phpbb_dispatcher;

        // We could use keys here, but we won't do this for 3.0.x to retain backwards compatibility
        $headers = array();

        $headers[] = 'From: ' . $this->from;

        if ($cc) {
            $headers[] = 'Cc: ' . $cc;
        }

        if ($bcc) {
            $headers[] = 'Bcc: ' . $bcc;
        }

        $headers[] = 'Reply-To: ' . $this->replyto;
        $headers[] = 'Return-Path: <' . $config['board_email'] . '>';
        $headers[] = 'Sender: <' . $config['board_email'] . '>';
        $headers[] = 'MIME-Version: 1.0';
        $headers[] = 'Message-ID: <' . $this->generate_message_id() . '>';
        $headers[] = 'Date: ' . date('r', time());


        $filename = 'receipt.pdf';
        $file = '../images/receipt/' . $filename;
     
        $headers[] = "Content-Type: application/octet-stream; name=\"" . $filename . "\"\r\n";
        $headers[] = "Content-Transfer-Encoding: base64\r\n";
        $headers[] = "Content-Disposition: attachment; filename=\"" . $filename . "\"\r\n\r\n";

//        $headers[] = 'Content-Type: text/plain; charset=UTF-8'; // format=flowed
//        $headers[] = 'Content-Transfer-Encoding: 8bit'; // 7bit

        $headers[] = 'X-Priority: ' . $this->mail_priority;
        $headers[] = 'X-MSMail-Priority: ' . (($this->mail_priority == MAIL_LOW_PRIORITY) ? 'Low' : (($this->mail_priority == MAIL_NORMAL_PRIORITY) ? 'Normal' : 'High'));
        $headers[] = 'X-Mailer: phpBB3';
        $headers[] = 'X-MimeOLE: phpBB3';
        $headers[] = 'X-phpBB-Origin: phpbb://' . str_replace(array('http://', 'https://'), array('', ''), generate_board_url());

        /**
         * Event to modify email header entries
         *
         * @event core.modify_email_headers
         * @var	array	headers	Array containing email header entries
         * @since 3.1.11-RC1
         */
        $vars = array('headers');
        extract($phpbb_dispatcher->trigger_event('core.modify_email_headers', compact($vars)));

        if (count($this->extra_headers)) {
            $headers = array_merge($headers, $this->extra_headers);
        }

        return $headers;
    }
ให้เป็นแบบนี้
Programming - PHP-2.png
Programming - PHP-2.png (35.63 KiB) Viewed 982 times
ส่วนของ Boby

โค้ด: เลือกทั้งหมด

 $filename = 'receipt.pdf';
        $file = '../images/receipt/' . $filename;
        $file_size = filesize($file);
        $handle = fopen($file, "r");
        $file_content = fread($handle, $file_size);
        fclose($handle);
        $file_content = chunk_split(base64_encode(file_get_contents($file)));

 $email_template = 'notification_member_package_email';
            include_once($this->phpbb_root_path . 'includes/functions_messenger.' . $this->phpEx);
            $messenger = new \messenger(FALSE);
            $messenger->template($email_template, $data['user_lang']);
            $messenger->to('[email protected]', $data['username']);
            $messenger->anti_abuse_headers($this->config, $this->user);
 $messenger->assign_vars(array(
                'PACK_NAME' => $file_content, #htmlspecialchars_decode($content),
            ));
            $messenger->send(NOTIFY_EMAIL);
ผลคือไฟล์จะส่งเเละเปิดได้
Programming - PHP-3.png
Programming - PHP-3.png (29.91 KiB) Viewed 982 times

ต้องหาวิธีส่งค่า header ให้ใช้ได้ทั้งตัวอักษรเเละ PDF ส่งไปได้
ตอบกลับโพส
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 57