เขียนโค้ด ส่งเมล แบบ Prestashop ด้วย Mail :: Send
ใน Prestashop มี library สำหรับช่วยให้ เราส่งเมล ผ่าน ทาง SMTP หรือจะใช้ฟังก์ชั่น mail ของ php ในการส่งก็ได้ขึ้นอยู่กับ ความพร้อมของเครื่อง หรือ Host ที่เราใช้งานอยู่ โดย Prestashop เลือกใช้ PHP Mail library swiftmailerเป็น library สำหรับส่งเมล
ตัวอย่างโค้ด Prestashop สำหรับส่งเมล
การส่งเมลจากร้านค้า Prestashop มีสอง methods ให้เลือกคือ
1. Mail :: Send() เป็นการส่งเมลโดยใช้ template คือเก็บไฟล์ข้อความหรือเนื้อหาไว้ในไฟล์ template
รูปแบบ
Mail::Send($id_lang, $template, $subject, $templateVars, $to, $toName = null, $from = null, $fromName = null, $fileAttachment = null, $modeSMTP = null, $templatePath = _PS_MAIL_DIR_, $die = false)
2. Mail :: sendMailTest() ส่งเมลโดยไม่ต้องใช้ template ของ Prestashop
รูปแบบ
Mail::sendMailTest($smtpChecked, $smtpServer, $content, $subject, $type, $to, $from, $smtpLogin, $smtpPassword, $smtpPort = 25, $smtpEncryption)
ตัวอย่าง
require(dirname(__FILE__).'/config/config.inc.php');
require_once(dirname(__FILE__).'/init.php');
$customer = new Customer();
$customer->getByEmail('[email protected]');
//Set the variables for the template:
$templateVars['{firstname}'] = $customer->firstname;
$templateVars['{lastname}'] = $customer->lastname;
$templateVars['{src_img}'] = _PS_BASE_URL_.__PS_BASE_URI__.'download/blog_belvg.png'; //Image to be displayed in the message
$id_land = Language::getIdByIso('en'); //Set the English mail template
$template_name = 'template'; //Specify the template file name
$title = Mail::l('Test Mail'); //Mail subject with translation
$from = Configuration::get('PS_SHOP_EMAIL'); //Sender's email
$fromName = Configuration::get('PS_SHOP_NAME'); //Sender's name
$mailDir = dirname(__FILE__).'/mails/'; //Directory with message templates
$toName = $customer->firstname.' '.$customer->lastname; //Customer name
$fileAttachment['content'] = file_get_contents(_PS_BASE_URL_.__PS_BASE_URI__.'download/fb.zip'); //File path
$fileAttachment['name'] = 'fileAttachment'; //Attachment filename
$fileAttachment['mime'] = 'application/zip'; //mime file type
$send = Mail::Send($id_land, $template_name, $title, $templateVars, $customer->email, $toName, $from, $fromName, $fileAttachment, NULL, $mailDir);
if ($send)
echo 'Done';
else
echo 'Error';โดยที่ $template_name = 'template_name'; คือชื่อไฟล์ template ที่ต้องทำไว้สองรูปบบคือ HTML และ TXT แล้วเก็บไว้ ใน folder
mails/{lang}/template_name.html
mails/{lang}/template_name.text
- 14 มีนาคม 2560
เขียนโดย Mindphp