2010-09-14 55 views
23

Tôi làm cách nào để gửi email bằng php sau đó thêm mẫu thiết kế vào email? Tôi đang sử dụng:Gửi email bằng mẫu sử dụng php

$to = "[email protected]"; 
$subject = "Test mail"; 
$message = "Hello! This is a simple email message."; 
$from = "[email protected]"; 
$headers = "From: $from"; 
mail($to,$subject,$message,$headers); 
echo "Mail Sent."; 

Và nó hoạt động tốt! Vấn đề là làm thế nào để thêm một mẫu.

Trả lời

39

Cho phép có một vết nứt nhỏ ở đây :)

class Emailer 
{ 
    var $recipients = array(); 
    var $EmailTemplate; 
    var $EmailContents; 

    public function __construct($to = false) 
    { 
     if($to !== false) 
     { 
      if(is_array($to)) 
      { 
       foreach($to as $_to){ $this->recipients[$_to] = $_to; } 
      }else 
      { 
       $this->recipients[$to] = $to; //1 Recip 
      } 
     } 
    } 

    function SetTemplate(EmailTemplate $EmailTemplate) 
    { 
     $this->EmailTemplate = $EmailTemplate;    
    } 

    function send() 
    { 
     $this->EmailTemplate->compile(); 
     //your email send code. 
    } 
} 

Thông báo chức năng SetTemplate() ...

Heres aa lớp mẫu nhỏ

class EmailTemplate 
{ 
    var $variables = array(); 
    var $path_to_file= array(); 
    function __construct($path_to_file) 
    { 
     if(!file_exists($path_to_file)) 
     { 
      trigger_error('Template File not found!',E_USER_ERROR); 
      return; 
     } 
     $this->path_to_file = $path_to_file; 
    } 

    public function __set($key,$val) 
    { 
     $this->variables[$key] = $val 
    } 


    public function compile() 
    { 
     ob_start(); 

     extract($this->variables); 
     include $this->path_to_file; 


     $content = ob_get_contents(); 
     ob_end_clean(); 

     return $content; 
    } 
} 

Dưới đây là một ví dụ nhỏ, bạn vẫn cần phải làm cốt lõi của tập lệnh nhưng điều này sẽ cung cấp cho bạn bố cục đẹp để bắt đầu.

$emails = array(
    '[email protected]', 
    '[email protected]' 
); 

$Emailer = new Emailer($emails); 
//More code here 

$Template = new EmailTemplate('path/to/my/email/template'); 
    $Template->Firstname = 'Robert'; 
    $Template->Lastname = 'Pitt'; 
    $Template->LoginUrl= 'http://stackoverflow.com/questions/3706855/send-email-with-a-template-using-php'; 
    //... 

$Emailer->SetTemplate($Template); //Email runs the compile 
$Emailer->send(); 

Thats thực sự tất cả những gì được với nó, chỉ cần biết cách sử dụng các đối tượng và nó khá đơn giản từ đó, ooh và mẫu sẽ trông một chút gì đó như thế này:

Welcome to my site, 

Dear <?php echo $Firstname ?>, You have been registered on our site. 

Please visit <a href="<?php echo $LoginUrl ?>">This Link</a> to view your upvotes 

Regards. 
+0

Chỉ cần rực rỡ! – Jeff

+0

Làm tốt lắm. Câu lệnh trích xuất trong phương thức biên dịch có cần đặt trước câu lệnh include không? –

+0

Không cần phải gọi trước khi được tải, điều này cho phép xác định các biến mẫu và trong phạm vi cho nội dung mẫu. – RobertPitt

55

Tại sao không thử một cái gì đó đơn giản như này:

$variables = array(); 

$variables['name'] = "Robert"; 
$variables['age'] = "30"; 

$template = file_get_contents("template.html"); 

foreach($variables as $key => $value) 
{ 
    $template = str_replace('{{ '.$key.' }}', $value, $template); 
} 

echo $template; 

bạn mẫu tập tin là một cái gì đó như:

<html> 

<p>My name is {{ name }} and I am {{ age }} !</p> 

</html> 
+2

Đây chỉ là những gì tôi đang tìm kiếm! – CMH

+2

Tôi thích giải pháp của bạn tốt hơn. Rất đơn giản. –

+1

Rực rỡ Rực Rỡ Rực Rỡ Rực Rỡ Rực Rỡ Rực Rỡ Rực Rỡ Rực Rỡi Rực Rỡ Rực Rỡ Rực Rỡ Rực Rỡ! – kta

3
 $message_to_client = file_get_contents("client_email.html"); 
     //$message_to_client = "bla bla {{ EMAIL }} bla bla"; 


     $variables = array(
      'SITE_TITLE' => $SITE_TITLE, 
      'SITE_LOGO' => $SITE_LOGO, 
      'SITE_URL' => $SITE_URL, 
      'CLIENT_NAME' => strip_tags($data->clientname), 
      'PHONE' => strip_tags($data->phone), 
      'EMAIL' => strip_tags($data->email), 
      'CITY' => strip_tags($data->city), 
      'REGION' => strip_tags($data->region), 
      'COMMENT' => htmlentities($data->comment)     
     ); 

     $message_to_client = preg_replace_callback('/{{([a-zA-Z0-9\_\-]*?)}}/i', 
      function($match) use ($variables) { 
       return $variables[$match[1]]; 
     }, $message_to_client); 
0

Hãy thử điều này ....

$body='<table width="90%" border="0"> 
     <tr> 
     <td><b>Name:</b></td> <td>'.$name.'</td> 
     </tr> 
     <tr> 
     <td><b>Email:</b></td> <td>'.$email.'</td> 
     </tr> 
     <tr> 
     <td><b>Message:</b></td> <td>'.$message.'</td> 
     </tr> 
     <tr></table>'; 

    mail($to,$subject,$body,$headers); 
0

Trước tiên, bạn phải thực hiện một mẫu HTML.

<form action="#" id="ContactForm" method="post" enctype="multipart/form-data"> 
    <table border="0" cellspacing="5" cellpadding="5" style="background-color:#CCCCCC; text-align:center;"> 
     <tr> 
      <td width="15%">Name:</td> 
      <td width="85%"><input name="name" type="text" required></td> 
     </tr> 
      <tr> 
      <td>Email:</td> 
      <td><input name="email" type="email" required></td> 
     </tr> 

     <tr> 
      <td colspan="2"><input name="sub" type="submit" value="Submit"></td> 
     </tr> 

    </table> 

Dưới code đang email mã chức năng với mẫu của bạn.

if ($_SERVER["REQUEST_METHOD"] == "POST") { 
    $name=$_REQUEST['name']; 
    $email=$_REQUEST['email']; 
    $to=$email; //change to ur mail address 
    $subject="UandBlog - Send Email Template Demo"; 
    $message = file_get_contents('Your template path'); // Your Template   
    $headers = 'MIME-Version: 1.0'."\r\n"; 
    $headers .= 'Content-type: text/html; charset=iso-8859-1'."\r\n"; 
    $headers .= "From: [email protected]"; 

    mail($to, $subject, $message, $headers); 
    } 

Bạn cũng có thể tải về mã đầy đủ với mẫu từ www.uandblog.com

Liên kết là http://www.uandblog.com/How-to-Send-Mail-with-Email-or-HTMLTemplate-using-php

0

Bạn có thể sử dụng $this trong mẫu như bạn đang ở trong tập tin gọi.

Bạn chỉ cần include template sau khi lệnh ob_start và lấy nội dung của nó:

$this->customer = 1234; //* This variable is used in the template 
ob_start(); 
include 'template.php'; 
$template = ob_get_clean(); 
var_dump($template);  //* Outputs '<b>1234</b>' 

// template.php 
<b><? echo $this->customer ?></b> 
0

ví dụ đơn giản của tôi

template.php

<?php 
class Template 
{ 
    function get_contents($templateName, $variables) { 
    $template = file_get_contents($templateName); 

    foreach($variables as $key => $value) 
    { 
     $template = str_replace('{{ '.$key.' }}', $value, $template); 
    } 
    return $template; 
    } 
} 
?> 

contact-us.tpl

Name: {{ name }} 
Email: {{ email }} 
subject: {{ subject }} 
------messages------ 
{{ messages }} 
--------------------- 

chính.php

<?php 
include_once 'template.php'; 

$name = "Your name"; 
$to = "[email protected]"; 
$subject = "Test mail"; 
$message = "Hello! This is a simple email message."; 
$from = "[email protected]"; 
$headers = "From: $from"; 

$text = Template::get_contents("contact-us.tpl", array('name' => $name, 'email' => $from, 'subject' => $subject, 'messages' => $message)); 
echo '<pre>'; 
echo $text; 
echo '<pre>'; 

$mail = @mail($to, $subject, $text, $headers); 
if($mail) { 
    echo "<p>Mail Sent.</p>"; 
} 
else { 
    echo "<p>Mail Fault.</p>"; 
} 
?> 
0

Tạo file mẫu của bạn, ví dụ:

/path/to/templates/template.twig:

Kính gửi {{name}},

Cảm ơn bạn đã viết cho chúng tôi về {{subject}}.

Sau đó làm theo các hướng dẫn tại https://twig.symfony.com/doc/2.x/api.html để cài đặt và sử dụng công cụ khuôn mẫu cành lá với Composer

require_once '/path/to/vendor/autoload.php'; 

$loader = new Twig_Loader_Filesystem('/path/to/templates'); 
$twig = new Twig_Environment($loader, array()); 

Sau đó đưa ra và gửi email của bạn:

$to = "[email protected]"; 
$subject = "Test mail"; 
$message = $twig->render('template.twig', array(
    'name' => 'Fred', 
    'subject' => 'philately', 
)); 
$from = "[email protected]"; 
$headers = "From: $from"; 
mail($to,$subject,$message,$headers); 
Các vấn đề liên quan