0

I'm working on a template for use in my email system. And i'll send mails to my customers.

This will be my create-email.php file :

and will have a textarea, i need some variables for use on it;

Hello {firstname} {lastname},

Your email address {email} has been added in to our newsletter list.

You can unsubscribe with using this link : http://example.com/unsubscribe.php?memberid={memberid}&email={email}

This textarea will be POST'ed to send-email.php

and my problem is starting here. I want to replace the {firstname} {lastname} {email} {memberid} with my data's coming from my database.

How can i do this ?

9
  • It sounds like your send-email.php is exposed to the public. You don't want to do that, or anybody will be able to generate emails from your server. Commented Mar 14, 2014 at 14:46
  • @Alex How so, this looks to be a typical email tpl and does not give an indication of being exposed? Commented Mar 14, 2014 at 14:48
  • This textarea will be POST'ed to send-email.php It's just a guess but it sounds like send-email.php is hosted on site instead of being called internally. If that site is public, I can hit the page myself. Commented Mar 14, 2014 at 14:50
  • First thak you for your answer, It'll used in admin panel. And only admins can change this template. And when someone register, purchase something, or contact us, this system will work and send emails to customers. Commented Mar 14, 2014 at 14:50
  • Restricting changes to the template is irrelevant if you're POSTing it to another page. I can just POST whatever I want. Can somebody who is not authenticated hit the send-email.php page? Commented Mar 14, 2014 at 14:52

2 Answers 2

2

Use str_replace with an array of values.

// $array can be set from a query perhaps?
$array = array('firstname' => 'Bob', 'lastname' => 'Dave',...);

// $template could also be a string which is a large block such as yours above.
$template = file_get_contents($template);
foreach($array as $key => $value) {
    $template = str_replace("{$key}", $value, $template);
}

You can also do this with using two arrays, but I do not actually advocate this as its using more memory and is problematic when keeping track of what field matches what value.

// Array of fields to replace
$fields = array('firstname', 'lastname',...);
$values = array('bob', 'dave',...);
$template = file_get_contents($template);
$template = str_replace($fields, $values, $template);
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, this one what i want to do ! Ty so much!
1

This little Goldie is perfect for you :)

<?php
$email_content    = $_POST['email_content'];
$search           = array('{firstname}', '{lastname}', '{email}', '{memberid}');
$replace          = array('Christian', 'Sany', '[email protected]', '123123123');
$replaced_content = str_replace($search, $replace, $email_content);
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.