How can I send an email using php then add a template design in the email? I'm using this:
如何使用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.";
And it works fine! The problem is just how to add a template.
和它工作好!问题是如何添加模板。
8 个解决方案
#1
61
Why not try something as simple as this :
为什么不试试这么简单的方法:
$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;
Your template file being something like :
您的模板文件如下:
<html>
<p>My name is {{ name }} and I am {{ age }} !</p>
</html>
#2
41
Lets have a small crack at this :)
让我们在这个问题上有一个小裂缝:)
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.
}
}
Notice the function SetTemplate()
...
注意函数SetTemplate()…
Heres a a small template class
这是一个小的模板类。
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;
}
}
Here's a small example, you still need to do the core of the script but this will provide you with a nice layout to get started with.
这里有一个小例子,您仍然需要执行脚本的核心,但是这将为您提供一个良好的布局。
$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://*.com/questions/3706855/send-email-with-a-template-using-php';
//...
$Emailer->SetTemplate($Template); //Email runs the compile
$Emailer->send();
Thats really all there is to it, just have to know how to use objects and its pretty simple from there, ooh and the template would look a little something like this:
这就是它的全部,只要知道如何使用对象,它很简单,哦,模板看起来就像这样:
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.
#3
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 );
#4
0
Try this....
试试这个....
$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);
#5
0
First you have to make a HTML template.
首先,您必须创建一个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>
Below code is mail functional code with your template.
下面的代码是带有模板的邮件功能代码。
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: noreply@uandblog.com";
mail($to, $subject, $message, $headers);
}
You can also download full code with template from www.uandblog.com
你也可以从www.uandblog.com下载完整的代码
The link is http://www.uandblog.com/How-to-Send-Mail-with-Email-or-HTMLTemplate-using-php
这个链接是http://www.uandblog.com/How-to-Send-Mail-with-Email-or-HTMLTemplate-using-php
#6
0
You can use $this
in the template as you are in the calling file.
您可以在模板中使用$this,就像在调用文件中一样。
You only need to include the template after the ob_start command and retrieve its content:
只需在ob_start命令之后包含模板并检索其内容:
$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>
#7
0
My simple example
我的简单的例子
template.php
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
contact-us.tpl
Name: {{ name }}
Email: {{ email }}
subject: {{ subject }}
------messages------
{{ messages }}
---------------------
main.php
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>";
}
?>
#8
0
Create your template file, e.g,
创建模板文件,例如,
/path/to/templates/template.twig:
/ /模板/ template.twig:/路径
Dear {{name}},
亲爱的{ {名称} },
Thank you for writing to us about {{subject}}.
谢谢您写信给我们关于{{}{{{{}}}。
Then follow the instructions at https://twig.symfony.com/doc/2.x/api.html to install and use the twig templating engine with Composer
然后按照https://twig.symfony.com/doc/2.x/api.html的说明与Composer一起安装并使用twig模板引擎
require_once '/path/to/vendor/autoload.php';
$loader = new Twig_Loader_Filesystem('/path/to/templates');
$twig = new Twig_Environment($loader, array());
Then render and send your email:
然后渲染和发送你的电子邮件:
$to = "someone@example.com";
$subject = "Test mail";
$message = $twig->render('template.twig', array(
'name' => 'Fred',
'subject' => 'philately',
));
$from = "someonelse@example.com";
$headers = "From: $from";
mail($to,$subject,$message,$headers);
#1
61
Why not try something as simple as this :
为什么不试试这么简单的方法:
$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;
Your template file being something like :
您的模板文件如下:
<html>
<p>My name is {{ name }} and I am {{ age }} !</p>
</html>
#2
41
Lets have a small crack at this :)
让我们在这个问题上有一个小裂缝:)
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.
}
}
Notice the function SetTemplate()
...
注意函数SetTemplate()…
Heres a a small template class
这是一个小的模板类。
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;
}
}
Here's a small example, you still need to do the core of the script but this will provide you with a nice layout to get started with.
这里有一个小例子,您仍然需要执行脚本的核心,但是这将为您提供一个良好的布局。
$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://*.com/questions/3706855/send-email-with-a-template-using-php';
//...
$Emailer->SetTemplate($Template); //Email runs the compile
$Emailer->send();
Thats really all there is to it, just have to know how to use objects and its pretty simple from there, ooh and the template would look a little something like this:
这就是它的全部,只要知道如何使用对象,它很简单,哦,模板看起来就像这样:
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.
#3
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 );
#4
0
Try this....
试试这个....
$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);
#5
0
First you have to make a HTML template.
首先,您必须创建一个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>
Below code is mail functional code with your template.
下面的代码是带有模板的邮件功能代码。
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: noreply@uandblog.com";
mail($to, $subject, $message, $headers);
}
You can also download full code with template from www.uandblog.com
你也可以从www.uandblog.com下载完整的代码
The link is http://www.uandblog.com/How-to-Send-Mail-with-Email-or-HTMLTemplate-using-php
这个链接是http://www.uandblog.com/How-to-Send-Mail-with-Email-or-HTMLTemplate-using-php
#6
0
You can use $this
in the template as you are in the calling file.
您可以在模板中使用$this,就像在调用文件中一样。
You only need to include the template after the ob_start command and retrieve its content:
只需在ob_start命令之后包含模板并检索其内容:
$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>
#7
0
My simple example
我的简单的例子
template.php
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
contact-us.tpl
Name: {{ name }}
Email: {{ email }}
subject: {{ subject }}
------messages------
{{ messages }}
---------------------
main.php
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>";
}
?>
#8
0
Create your template file, e.g,
创建模板文件,例如,
/path/to/templates/template.twig:
/ /模板/ template.twig:/路径
Dear {{name}},
亲爱的{ {名称} },
Thank you for writing to us about {{subject}}.
谢谢您写信给我们关于{{}{{{{}}}。
Then follow the instructions at https://twig.symfony.com/doc/2.x/api.html to install and use the twig templating engine with Composer
然后按照https://twig.symfony.com/doc/2.x/api.html的说明与Composer一起安装并使用twig模板引擎
require_once '/path/to/vendor/autoload.php';
$loader = new Twig_Loader_Filesystem('/path/to/templates');
$twig = new Twig_Environment($loader, array());
Then render and send your email:
然后渲染和发送你的电子邮件:
$to = "someone@example.com";
$subject = "Test mail";
$message = $twig->render('template.twig', array(
'name' => 'Fred',
'subject' => 'philately',
));
$from = "someonelse@example.com";
$headers = "From: $from";
mail($to,$subject,$message,$headers);