获取PHP Form Mail()函数上的文件内容 [英] Get Contents of a File on PHP Form Mail() Function

查看:98
本文介绍了获取PHP Form Mail()函数上的文件内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用一些类型的邮件合并,其中PHP读取文本文件,但在该文本中,我试图替换一些关键字,以便如果我添加#FIRSNAME#,#LASTNAME# ,#EMAIL#,用mail()函数的表单中输入的字段替换。我有10个相同的字段名称的意思是:10个名字字段,10个姓氏,10个电子邮件我使用for循环吐出10个相同的HTML输入(我这样做,而不是输入10个相同类型的HTML输入标签)PHP通过验证处理此表单使用mail()函数。



我的代码工作目前在表单和PHP处理它在同一页myform.php:

 <?php验证?> //查看是否有空字段,或者如果提交
< form>
($ i = 1; $ i < = $; $ i $)
$ /


<?php
//打印或回复谢谢 如果没有问题的形式

//电子邮件...我目前有这里的我的代码,我想使用file_get_contents函数不知何故
$ body =谢谢{$ _POST [ 'firstname'] [$ i]}注册blah blah blah blah!
mail($ _ POST ['email'] [$ i],'Party Invitation',$ body,'From:email@example.com');
}
?>

我想阅读并邮件合并关键字的外部文本文件...



 您已请求此表单...您被邀请参加我们的晚会.. 
名称:#FIRSTNAME##LASTNAME #
电子邮件:#EMAIl

我想用$ firstname替换这些关键字, $ lastname使用外部文本文件,以便这将作为电子邮件消息使用mail()函数发送。可以做吗原因是这个外部文本文件就像一个我想改变的模板。

解决方案

一个简单的答案,而不涉及数据库,类等等。



使用file_get_contents ....读取模板sometempalte.txt文件。

 <?php 

//某些形式与验证

//通过电子邮件发送表单
//在这个somethingtemplate.txt中有内容作为电子邮件消息
//Hello#name#。您通过电子邮件注册:#email#。
//使用str_replace替换这些关键字
$ body = file_get_contents(sometemplate.txt);
$ body = str_replace(#name#,$ _ POST ['name'],$ body);
$ body = str_replace(#email#,$ _ POST ['email'],$ body);
mail($ _ POST ['email'],'Subject',$ body,'From:email@email.com');

?>


I'm trying to use some type of a mail merge where PHP reads the text file but in that text I'm trying to "replace" some keywords so that if I add "#FIRSNAME#", "#LASTNAME#, "#EMAIL#", replaces that with the entered fields on the form on the mail() function.... The form I have has 10 of the same field names meaning: 10 first name fields, 10 last names, 10 emails. I used the "for" loop to spit out 10 same HTML inputs (I did this rather than typing 10 HTML input tags of the same kind). PHP processes this form with validation. Using the mail() function.

My code that works currently where form and PHP processes it on the same page "myform.php":

<?php validation ?> // looks at if there are empty fields and or if submitted 
<form>
for($i=1; $i <= 10; $i++) { echo '<input firstname etc> <input lastname> <input email>';}
</form>

<?php 
//print or echos the "Thank you" if there's no problem with the form

//emails... I currently have HERE'S MY code that I want to use file_get_contents function somehow
$body = "Thank you {$_POST['firstname'][$i]} for registering with the blah   blah blah  blah!";
mail($_POST['email'][$i], 'Party Invitation', $body, 'From: email@example.com');
}
?>

The external text file that I would like to "read" and mail merge keywords...

You have requested this form... you're invited to our party..
Name: #FIRSTNAME# #LASTNAME#
Email: #EMAIl

I'd like to replace these keywords with $firstname and $lastname using the external text file so that this will be sent as an email message using the mail() function. Can that be done? The reason is that this external text file is like a "template" that I'd like to change.

解决方案

There was actually a simple answer without involving database, classes so on.

The template sometempalte.txt file is read using file_get_contents....

<?php

//some form with validation

//send form via email
//In this somethingtemplate.txt has content as an email message
//"Hello #name#. You registered with email: #email#."
//using str_replace to replace these keywords
$body = file_get_contents("sometemplate.txt"); 
$body = str_replace("#name#",$_POST['name'],$body);
$body = str_replace("#email#",$_POST['email'],$body);
mail($_POST['email'], 'Subject', $body, 'From: email@email.com');

?>

这篇关于获取PHP Form Mail()函数上的文件内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆