I am doing simple PHP form validation with email template. I am having simple form called "index.html" and having submit button with form method "post" and action as "sendmail.php". In my sendmail.php, am having smtp-mailer.php with email template called as "email-template.html". How can i pass a variable from index.html to mail-template.php via sendmail.php... Catch my point??? I know about using SESSION. But i don't know where should i call this and how to fetch this??? Any idea...???
我正在使用电子邮件模板进行简单的PHP表单验证。我有一个名为“index.html”的简单形式,并且具有表单方法“post”的提交按钮和作为“sendmail.php”的动作。在我的sendmail.php中,我有smtp-mailer.php,电子邮件模板名为“email-template.html”。我如何通过sendmail.php将一个变量从index.html传递给mail-template.php ...赶上我的观点???我知道使用SESSION。但我不知道我应该在哪里打电话给这个以及如何取这个?任何想法...???
index.html
<form method="post" action="sendemail.php">
Email: <input name="email" id="email" type="text" /><br />
Message:<br />
<textarea name="message" id="message" rows="15" cols="40"></textarea><br />
<input type="submit" value="Submit" />
</form>
sendmail.php
<?php
include "class.smtp.php";
include "class.phpmailer.php";
session_start();
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
$_SESSION['message'] = $message;
$Body = file_get_contents('email-template.php');
...
...
?>
email-template.php (send it to email not in browser)
<table border="1">
<tr>
<td colspan="2">
<h3>
<?php
session_start();
$message = $_SESSION['message'];
echo "Your registration is: ".$message.".";
?>
</h3>
</td>
</tr>
</table>
Updates : I did the same... But no response... Did i miss something in sendmail.php
更新:我做了同样的...但没有回复...我在missmail.php中遗漏了什么
5 个解决方案
#1
7
You're trying to put your email in its own template file, which is great, but you need to actually execute the PHP code within it to fill out the variables.
您正试图将您的电子邮件放在自己的模板文件中,这很棒,但您需要在其中实际执行PHP代码以填写变量。
Rename email-template.html to email-template.phtml. That's a fairly standard extension for php templates. It's worth noting that using $_REQUEST is not recommended in PHP as it takes both POST and GET parameters, and you'd decided that the user needs to POST the form.
将email-template.html重命名为email-template.phtml。这是php模板的一个相当标准的扩展。值得注意的是,在PHP中不推荐使用$ _REQUEST,因为它同时使用POST和GET参数,并且您决定用户需要POST表单。
Include and execute the template file in sendmail.php:
在sendmail.php中包含并执行模板文件:
<?php
include "class.smtp.php";
include "class.phpmailer.php";
function render_email($email, $message) {
ob_start();
include "email-template.phtml";
return ob_get_contents();
}
$email = $_POST['email'] ;
$message = $_POST['message'];
$body = render_email($email, $message);
...
...
render_email is including the template file, exposing the two variables $email and $message to that template file and then returning the output of including the template.
render_email包含模板文件,将两个变量$ email和$ message暴露给该模板文件,然后返回包含模板的输出。
You use these variables in template.phtml as you were trying to do before. email-template.phtml:
您可以像之前尝试的那样在template.phtml中使用这些变量。电子邮件template.phtml:
<table border="1">
<tr>
<td colspan="2">
<h3>
Your registration is:<?= htmlspecialchars($message) ?>
</h3>
</td>
</tr>
</table>
#2
1
You can do that in 3 ways. Injecting a var in the link, using a session or using a cookie. Personally I suggest sessions because users can't modify them and they expire when the user close his browser.
你可以用3种方式做到这一点。使用会话或使用cookie在链接中注入var。我个人建议会话,因为用户无法修改它们,当用户关闭浏览器时它们会过期。
You need to rename index.html to index.php so you can insert php code into this file, then you'll need to write something like this:
你需要将index.html重命名为index.php,这样你就可以将php代码插入到这个文件中了,然后你需要写这样的东西:
<?php
session_start();
$_SESSION['name'] = "value";
?>
Doing so you setted the session, now each time you need to call the session use this code:
这样设置了会话,现在每次需要调用会话时都使用以下代码:
<?php
session_start();
$var = $_SESSION['name'];
?>
Now you use
现在你用
echo "$var";
Where you wanna print the var
你想要打印var的地方
Don't forget session_start();
or you won't be able to call or set sessions
不要忘记session_start();或者你将无法打电话或设置会话
If you wanna do this using a cookie or injecting the var into the url feel free to ask and I'll explain you how ;)
如果你想使用cookie或者将var注入url,请随意询问,我会向你解释如何;)
Your code in sendmail.php is a little bit messed, here the fixed one:
你在sendmail.php中的代码有点混乱,这里是固定的:
<?php
include "class.smtp.php";
include "class.phpmailer.php";
session_start();
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
$_SESSION['message'] = $message;
$Body = file_get_contents('email-template.php');
...
...
?>
Then in email-template.php you'll use this code:
然后在email-template.php中,您将使用以下代码:
<table border="1">
<tr>
<td colspan="2">
<h3>
<?php
session_start();
$message = $_SESSION['message'];
echo "Your registration is: ".$message.".";
?>
</h3>
</td>
</tr>
</table>
#3
1
For those who are still facing this problem, I think this is what it's all about.
对于那些仍然面临这个问题的人,我认为这就是它的全部意义所在。
In the template the string(s) to be displayed should be specified (with special characters) instead of printing by php.
在模板中,应指定要显示的字符串(使用特殊字符),而不是通过php打印。
email_template.php
<table border="1">
<tr>
<td colspan="2">
<h3>
Your Registration number is: #registration_number#
</h3>
</td>
</tr>
Back in sendmail.php the template should be opened and read (using fopen and fread) and the specified string should be replaced using str_replace before mailing
回到sendmail.php,应该打开并读取模板(使用fopen和fread),并且在邮寄之前应该使用str_replace替换指定的字符串
sendmail.php
<?php
include "class.smtp.php";
include "class.phpmailer.php";
session_start();
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
$_SESSION['message'] = $message;
$template_loc = fopen("../path/to/email_template.php");
$template_read = fread($template_loc, filesize("../path/to/email_template.php"));
$template_read = str_replace("#registration_number#", $message, $template_read);
$Body = $template_read; #or directly use $template_read as body
...
...
?>
Suggestions: 1) CSS in email templates should always be inline. 2)For sending emails 3rd party APIs offer more functionalities, such as tracking the sent emails, etc.
建议:1)电子邮件模板中的CSS应始终内联。 2)对于发送电子邮件,第三方API提供更多功能,例如跟踪已发送的电子邮件等。
#4
0
Try something like this
尝试这样的事情
$name = 'John Doe';
$vars = ('NAME' => $name);
$message = file_get_contents('email_template.html');
foreach($vars as $search => $replace){
$message = str_ireplace('%' . $search . '%', $replace, $message);
}
mail('john@doe.com', 'Subject', $message, $headers);
So email_template.html
will have %NAME%
inside of it, str_ireplace
will replace it by John Doe
and you will send the variable $message
as the email html.
因此,email_template.html将包含%NAME%,str_ireplace将由John Doe替换它,您将发送变量$ message作为电子邮件html。
I think this is the best practice.
我认为这是最好的做法。
I think it will help you.
我认为它会对你有所帮助。
#5
0
Why don't you try to put some message placeholder in email-template.html and later replace it with the real message in sendmail.php
为什么不尝试在email-template.html中放置一些消息占位符,然后将其替换为sendmail.php中的真实消息
For example: email-template.html
例如:email-template.html
<table border="1">
<tr>
<td colspan="2">
<h3>{{message-placeholder}}</h3>
</td>
</tr>
</table>
And then in sendmail.php
然后在sendmail.php中
<?php
include "class.smtp.php";
include "class.phpmailer.php";
session_start();
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
$_SESSION['message'] = $message;
$Body = file_get_contents('email-template.php');
$Body = str_replace('{{message-placeholder}}', $message, $Body);
...
...
?>
That way you'll have the html template and you might also have some very basic templating. You'll also get rid of that session stuff
这样你就可以拥有html模板了,你可能也会有一些非常基本的模板。你也会摆脱那些会话的东西
#1
7
You're trying to put your email in its own template file, which is great, but you need to actually execute the PHP code within it to fill out the variables.
您正试图将您的电子邮件放在自己的模板文件中,这很棒,但您需要在其中实际执行PHP代码以填写变量。
Rename email-template.html to email-template.phtml. That's a fairly standard extension for php templates. It's worth noting that using $_REQUEST is not recommended in PHP as it takes both POST and GET parameters, and you'd decided that the user needs to POST the form.
将email-template.html重命名为email-template.phtml。这是php模板的一个相当标准的扩展。值得注意的是,在PHP中不推荐使用$ _REQUEST,因为它同时使用POST和GET参数,并且您决定用户需要POST表单。
Include and execute the template file in sendmail.php:
在sendmail.php中包含并执行模板文件:
<?php
include "class.smtp.php";
include "class.phpmailer.php";
function render_email($email, $message) {
ob_start();
include "email-template.phtml";
return ob_get_contents();
}
$email = $_POST['email'] ;
$message = $_POST['message'];
$body = render_email($email, $message);
...
...
render_email is including the template file, exposing the two variables $email and $message to that template file and then returning the output of including the template.
render_email包含模板文件,将两个变量$ email和$ message暴露给该模板文件,然后返回包含模板的输出。
You use these variables in template.phtml as you were trying to do before. email-template.phtml:
您可以像之前尝试的那样在template.phtml中使用这些变量。电子邮件template.phtml:
<table border="1">
<tr>
<td colspan="2">
<h3>
Your registration is:<?= htmlspecialchars($message) ?>
</h3>
</td>
</tr>
</table>
#2
1
You can do that in 3 ways. Injecting a var in the link, using a session or using a cookie. Personally I suggest sessions because users can't modify them and they expire when the user close his browser.
你可以用3种方式做到这一点。使用会话或使用cookie在链接中注入var。我个人建议会话,因为用户无法修改它们,当用户关闭浏览器时它们会过期。
You need to rename index.html to index.php so you can insert php code into this file, then you'll need to write something like this:
你需要将index.html重命名为index.php,这样你就可以将php代码插入到这个文件中了,然后你需要写这样的东西:
<?php
session_start();
$_SESSION['name'] = "value";
?>
Doing so you setted the session, now each time you need to call the session use this code:
这样设置了会话,现在每次需要调用会话时都使用以下代码:
<?php
session_start();
$var = $_SESSION['name'];
?>
Now you use
现在你用
echo "$var";
Where you wanna print the var
你想要打印var的地方
Don't forget session_start();
or you won't be able to call or set sessions
不要忘记session_start();或者你将无法打电话或设置会话
If you wanna do this using a cookie or injecting the var into the url feel free to ask and I'll explain you how ;)
如果你想使用cookie或者将var注入url,请随意询问,我会向你解释如何;)
Your code in sendmail.php is a little bit messed, here the fixed one:
你在sendmail.php中的代码有点混乱,这里是固定的:
<?php
include "class.smtp.php";
include "class.phpmailer.php";
session_start();
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
$_SESSION['message'] = $message;
$Body = file_get_contents('email-template.php');
...
...
?>
Then in email-template.php you'll use this code:
然后在email-template.php中,您将使用以下代码:
<table border="1">
<tr>
<td colspan="2">
<h3>
<?php
session_start();
$message = $_SESSION['message'];
echo "Your registration is: ".$message.".";
?>
</h3>
</td>
</tr>
</table>
#3
1
For those who are still facing this problem, I think this is what it's all about.
对于那些仍然面临这个问题的人,我认为这就是它的全部意义所在。
In the template the string(s) to be displayed should be specified (with special characters) instead of printing by php.
在模板中,应指定要显示的字符串(使用特殊字符),而不是通过php打印。
email_template.php
<table border="1">
<tr>
<td colspan="2">
<h3>
Your Registration number is: #registration_number#
</h3>
</td>
</tr>
Back in sendmail.php the template should be opened and read (using fopen and fread) and the specified string should be replaced using str_replace before mailing
回到sendmail.php,应该打开并读取模板(使用fopen和fread),并且在邮寄之前应该使用str_replace替换指定的字符串
sendmail.php
<?php
include "class.smtp.php";
include "class.phpmailer.php";
session_start();
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
$_SESSION['message'] = $message;
$template_loc = fopen("../path/to/email_template.php");
$template_read = fread($template_loc, filesize("../path/to/email_template.php"));
$template_read = str_replace("#registration_number#", $message, $template_read);
$Body = $template_read; #or directly use $template_read as body
...
...
?>
Suggestions: 1) CSS in email templates should always be inline. 2)For sending emails 3rd party APIs offer more functionalities, such as tracking the sent emails, etc.
建议:1)电子邮件模板中的CSS应始终内联。 2)对于发送电子邮件,第三方API提供更多功能,例如跟踪已发送的电子邮件等。
#4
0
Try something like this
尝试这样的事情
$name = 'John Doe';
$vars = ('NAME' => $name);
$message = file_get_contents('email_template.html');
foreach($vars as $search => $replace){
$message = str_ireplace('%' . $search . '%', $replace, $message);
}
mail('john@doe.com', 'Subject', $message, $headers);
So email_template.html
will have %NAME%
inside of it, str_ireplace
will replace it by John Doe
and you will send the variable $message
as the email html.
因此,email_template.html将包含%NAME%,str_ireplace将由John Doe替换它,您将发送变量$ message作为电子邮件html。
I think this is the best practice.
我认为这是最好的做法。
I think it will help you.
我认为它会对你有所帮助。
#5
0
Why don't you try to put some message placeholder in email-template.html and later replace it with the real message in sendmail.php
为什么不尝试在email-template.html中放置一些消息占位符,然后将其替换为sendmail.php中的真实消息
For example: email-template.html
例如:email-template.html
<table border="1">
<tr>
<td colspan="2">
<h3>{{message-placeholder}}</h3>
</td>
</tr>
</table>
And then in sendmail.php
然后在sendmail.php中
<?php
include "class.smtp.php";
include "class.phpmailer.php";
session_start();
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
$_SESSION['message'] = $message;
$Body = file_get_contents('email-template.php');
$Body = str_replace('{{message-placeholder}}', $message, $Body);
...
...
?>
That way you'll have the html template and you might also have some very basic templating. You'll also get rid of that session stuff
这样你就可以拥有html模板了,你可能也会有一些非常基本的模板。你也会摆脱那些会话的东西