0

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.

mythoslife
  • 203
  • 1
  • 6
  • 20
  • Yes it can be done. And imagine what: You're not the first one who asks. So better search a bit around if you want answers fast. – hakre Aug 27 '13 at 20:55
  • don't use a flat file use a db –  Aug 27 '13 at 20:55
  • @Dagon: Luckily the file-system is a database. – hakre Aug 27 '13 at 20:55
  • possible duplicate of [replace multiple placeholders with php?](http://stackoverflow.com/questions/10106052/replace-multiple-placeholders-with-php) – hakre Aug 27 '13 at 20:56
  • just a little overly pedantic –  Aug 27 '13 at 20:57
  • @hakre thanks yes I've been googling it some of them use functions with arrays, some use MySQL which is something I don't like to do like what the other person suggested of course that would work but isn't needed since I don't have to have a database – mythoslife Aug 27 '13 at 21:15
  • @Dagon database isn't required and not needed as currently I have an external file that is required to be used as the basis of mail() function email message. Thanks though. – mythoslife Aug 27 '13 at 21:20
  • @hakre oh that could work thank you! – mythoslife Aug 27 '13 at 21:25
  • Darn I'm not comfortable with classes... – mythoslife Aug 27 '13 at 21:28
  • fine, but its much easier working with one, and ill keep saying so as you keep asking the same question :-) –  Aug 27 '13 at 21:49
  • @Dagon I see what you're saying and I understand that conceptually it's great to utilize db as it's probably better in the long run. There's also other ways to work with forms and other ways to do validations using sanitation for security purposes etc. There's also probably a way to make it nicer with CSS in mind as far as the design. I'm not very versed with OOP and db at this time. I'm just trying to go about what I know within limit. That's all. – mythoslife Aug 27 '13 at 22:17
  • I think I got it where I would use a function mail_message(some variable) and in there will use file_get_contents() as well as str_replace() so search for the keywords to replace them with. Then will use mail($to, $email_subject, $email_message, $headers). – mythoslife Aug 27 '13 at 22:54

1 Answers1

0

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');

?>
mythoslife
  • 203
  • 1
  • 6
  • 20