เขียน custom page ส่งเมลด้วย messenger ผ่าน SMTP หรือ ฟังก์ชั่นเมลตามแบบ phpBB

PHP Knowledge เป็น บอร์ดรวามความรู้ php เน้นบทความ แนวทางการเขียนโปรแกรม บันทึกกันลืม เพื่อให้สมาชิกได้เขียนความรู้ที่ตัวเองมีให้สมาชิกท่านอื่นๆ ได้ เข้ามาอ่าน และ ไว้อ่านเองกันลืมด้วย

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

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

เขียน custom page ส่งเมลด้วย messenger ผ่าน SMTP หรือ ฟังก์ชั่นเมลตามแบบ phpBB

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

เขียน custom page ส่งเมลด้วย messenger ผ่าน SMTP หรือ ฟังก์ชั่นเมลตามแบบ phpBB
สำหรับใครที่ยังไม่รู้จัก phpBB custom page สามารถ ศึกษาได้ก่อนจาก บทความนี้
สำหรับใครที่พอเขียนเป็นอยู่แล้ว ไปกันต่อเลย
เริ่มจาก include ไฟล์ที่จำเป็นต้องใช้ แบบ custom page

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

define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);

require_once($phpbb_root_path . 'common.' . $phpEx);
require_once($phpbb_root_path . 'includes/startup.' . $phpEx);
require_once($phpbb_root_path . 'includes/functions.' . $phpEx);
require_once($phpbb_root_path . 'includes/utf/utf_tools.' . $phpEx);
require_once($phpbb_root_path . 'includes/functions_messenger.' . $phpEx);
// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup();

// สร้าง object messenger  กำหนด false เป็นค่าเริ่มต้นเพื่อไม่ต้องให้เก็บเมลไว้ใน ระบบ queue 
$messenger = new messenger(false);
// set ชื่อ template ของเมลที่จะใช้ ซึ่งไฟล์นี้ จะเก็บไว้ที่ language/en/email/email_notify.txt โดยที่ email_notify คือชื่อ template ไฟล์ข้อความ 
$messenger->template('email_notify');
// กำหนด รายละเอียดของเมล 
$to = '[email protected]';
$subject = 'Test Email by mindphp';
$message = 'This is a email. From ThaiVI';
$headers = 'From: [email protected]';

$messenger->headers($headers);
$messenger->to($to);
$messenger->from('[email protected]', 'mindphp');
$messenger->subject(html_entity_decode($subject));
$messenger->message = $message;
$messenger->send($message);

try {
    $messenger->send();
    echo 'Email sent successfully.';
} catch (\Exception $e) {
    echo 'Failed to send email: ' . $e->getMessage();
}

เท่านี้เราก็ใช้ phpBB ช่วยในการส่งเมลได้แล้ว ไม่ต้องติดตั้ง smtp หรือใช้ library อื่นเข้ามาช่วย
ติดตาม 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
โพสต์: 41245
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

Re: เขียน custom page ส่งเมลด้วย messenger ผ่าน SMTP หรือ ฟังก์ชั่นเมลตามแบบ phpBB

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

ตัวอย่างการดึงค่าและแทนที่ค่า ลงใน template email

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

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

						$subject = sprintf($user->lang['IM_JABBER_SUBJECT'], $user->data['username'], $config['server_name']);
						$message = $request->variable('message', '', true);

						if (empty($message))
						{
							trigger_error('EMPTY_MESSAGE_IM');
						}

						$messenger = new messenger(false);

						$messenger->template('profile_send_im', $row['user_lang']);
						$messenger->subject(html_entity_decode($subject, ENT_COMPAT));

						$messenger->replyto($user->data['user_email']);
						$messenger->set_addresses($row);

						$messenger->assign_vars(array(
							'BOARD_CONTACT'	=> phpbb_get_board_contact($config, $phpEx),
							'FROM_USERNAME'	=> html_entity_decode($user->data['username'], ENT_COMPAT),
							'TO_USERNAME'	=> html_entity_decode($row['username'], ENT_COMPAT),
							'MESSAGE'		=> html_entity_decode($message, ENT_COMPAT))
						);

						$messenger->send(NOTIFY_IM);
ศึกษา class messenger เพิ่มเติมได้ที่นี่
https://area51.phpbb.com/docs/code/3.3.x/messenger.html
ติดตาม 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
โพสต์: 41245
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

Re: เขียน custom page ส่งเมลด้วย messenger ผ่าน SMTP หรือ ฟังก์ชั่นเมลตามแบบ phpBB

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

การส่งเมลผ่าน SMTP 1 เมล เวลารวมเฉพาะการส่งเมลจะอยู่ที่
จากประสบการ การส่งเมล 1 เมลผ่าน SMTP จะใช้เวลาต่อเมลประมาณ
0.01 - 0.2xs
ทั้งนี้ขึ้นอยู่กับ ความเร็วของ smtp, network , การเข้ารหัส, และ ข้อมูลในการส่งเมลด้วย
ติดตาม 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
ตอบกลับโพส
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

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

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