使用php发送带有模板的电子邮件

ano*_*123 38 php email templates

如何使用php发送电子邮件然后在电子邮件中添加模板设计?我正在使用这个:

$to = "someone@example.com";  
$subject = "Test mail";  
$message = "Hello! This is a simple email message.";  
$from = "someonelse@example.com";  
$headers = "From: $from";  
mail($to,$subject,$message,$headers);  
echo "Mail Sent.";  
Run Code Online (Sandbox Code Playgroud)

它工作正常!问题是如何添加模板.

Jiv*_*van 86

为什么不尝试这样简单的事情:

$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;
Run Code Online (Sandbox Code Playgroud)

您的模板文件类似于:

<html>

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

</html>
Run Code Online (Sandbox Code Playgroud)

  • 我更喜欢你的解决方案.非常简单. (3认同)
  • 这正是我想要的! (2认同)

Rob*_*itt 46

让我们有一个小裂缝:)

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.
    }
}
Run Code Online (Sandbox Code Playgroud)

注意功能SetTemplate()......

这是一个小模板类

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是一个小例子,你仍然需要做脚本的核心,但这将为你提供一个很好的布局来开始.

$emails = array(
    'bob@bobsite.com',
    'you@yoursite.com'
);

$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();
Run Code Online (Sandbox Code Playgroud)

多数民众赞成就是这样,只需要知道如何使用对象,从那里开始就非常简单,哦,模板会看起来像这样:

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.
Run Code Online (Sandbox Code Playgroud)

  • 嗨,罗伯特,很棒的片段,但我不确定我是如何发送邮件的.您可以轻松地将模板编译到SetTemplate中,但之后没有发送函数,并且类Emailer不会扩展任何Mailer类(使用send或mail()函数)...我在这里遗漏了什么? (7认同)

And*_*ski 7

我的简单例子

模板.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;
  }
}
?>
Run Code Online (Sandbox Code Playgroud)

联系我们.tpl

Name: {{ name }}
Email:  {{ email }}
subject:  {{ subject }}
------messages------
{{ messages }}
---------------------
Run Code Online (Sandbox Code Playgroud)

main.php

<?php
include_once 'template.php';

$name = "Your name";
$to = "someone@example.com";  
$subject = "Test mail";  
$message = "Hello! This is a simple email message.";  
$from = "someonelse@example.com";  
$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>"; 
}
?>
Run Code Online (Sandbox Code Playgroud)