PHP manages mail styles using templates

Source: Internet
Author: User
PHP uses a template to manage mail styles when the younger brother contacts php on November 3rd, the company wants to create an email module. The php framework used in the project is zendframework. However, in the example displayed on the Internet, to send an html-style email, the html code must all be included in the string. In this way, it is difficult to preview the Mail layout. If you can directly write an email template and call the template when sending the template, the system rendering template will get the PHP template to manage the Mail style.

My younger brother came into contact with php on November 3, 3rd. The company wants to create an email module. The php framework used in the project is zend framework. However, in the example displayed on the Internet, to send an html-style email, the html code must all be included in the string. In this way, it is difficult to preview the Mail layout. If you can directly write an email template and call the template when sending the template, the system renders the template to get the final email. So I tried to write this function by myself.

First write the Mail sending function

Class SendMailTool extends Zend_Mail_Transport_Smtp {protected $ mail; protected $ transport; public function _ construct ($ smtpserver = 'smtp .hgsamerica.com ', $ username, $ password) {$ config = array ('auth' => 'login', 'username' => $ username, 'password' => $ password ); $ this-> transport = new Zend_Mail_Transport_Smtp ($ smtpserver, $ config);}/*** is used to send basic emails */public function sendMail ($ to, $ toname, $ From, $ fromname, $ title, $ contant) {$ this-> mail = new Zend_Mail ("UTF-8 "); $ this-> mail-> setDefaultTransport ($ this-> transport); $ this-> mail-> addTo ($ to, $ toname ); $ this-> mail-> setBodyHtml ($ contant); $ this-> mail-> setFrom ($ from, $ fromname ); $ this-> mail-> setSubject ("=? UTF-8? B? ". Base64_encode ($ title )."? = "); $ This-> mail-> send () ;}// read the template and replace the variable private function loadVM ($ vmname, $ config = Array () in the template ()) {$ contant = ''; $ file = fopen (" mailtemplates /". $ vmname, "r"); while (! Feof ($ file) {$ line = fgets ($ file); while (strpos ($ line, "{\ $")> 0) {$ counts = strpos ($ line, "{\$"); $ counte = strpos ($ line, "}"); $ substr = substr ($ line, $ counts + 2, $ counte-$ counts-2); $ line = str_replace ("{\$ ". $ substr. "}", $ config [$ substr], $ line);} $ contant. = $ line;} return $ contant;}/*** send template email */public function sendVMMail ($ to, $ toname, $ from, $ fromname, $ title, $ config, $ vm) {$ this-> sendMail ($ to, $ toname, $ from, $ fromname, $ title, $ this-> loadVM ($ vm, $ config ));}
?



? Test Template

 
 
please input your inf
name:{$user}
password:{$password}
?



? Test Code

        $config = array (            'user' => 'huling',            'password' => '123456'        );        $mail = new SendMailTool('smtp.xxxx.com', 'huling', 'aqsdsadsad');        $mail->sendVMMail('sagahl@126.com', 'live', 'huling@xxxxx.com', 'work', 'askjdkas',   $config,'test.html');    }
?



? The above code completes sending emails through the template. To modify the Mail style, you only need to modify the mail template.

Since I was new to php, I don't know if php has implemented the above function code? Or is there a better solution? I hope you will be able to inspire others.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.