0

I'm storing some emails in the database, because they are templates and I want to enable admins to edit them. So there are three columns, initialmail, followup, followup2. However, these templates look like this:

Hello $Client, we are contacting you about your $product 

etc. After that, send mail will just pickup a random row from the right column and send it. However, I don't know how to enable admins to add/edit e-mails and store variable names rather then values within the database, so sendmail.php will know which variables are there and replace them with correct values. Keep in mind this needs to be easy, considering admins will edit this on site, so I can't have them type ' .$Clientname. ' each time they want a variable stored in the database.

Thank you!

Predrag Beocanin
  • 1,402
  • 3
  • 18
  • 25
  • And... what exactly is your question? Where do you struggle? – Yogu Jul 28 '13 at 18:46
  • I don't know how to store this in the database. It would be nice if admin could write an email like "Hello USERNAME" and php would translate that to `$username` and store it as `$username` in the database. Or any other way that doesn't involve too much hassle while writing an email to insert a variable. – Predrag Beocanin Jul 28 '13 at 18:48
  • Thanks, that answers second part of my question, I still don't know how to store this `$USERNAME` in the database as `$USERNAME`. – Predrag Beocanin Jul 28 '13 at 18:52
  • Why don't you simply store the user input direclty in the data base, and replace the templates when you send the email? – Yogu Jul 28 '13 at 18:53
  • Hm maybe I asked this the wrong way. Let me break it down 1) Admin adds new email along with the `$varname` 2) Admin opens up client page 3) Admin clicks send 4) Script pulls the relevant info about that specific client from the database, pulls email template too, replaces all `$varname` with data and sends it Does it make more sense now? I might be doing something wrong here, but this seemed as a logical way to handle this considering there will be endless templates and I can't just store one for each client – Predrag Beocanin Jul 28 '13 at 18:54
  • 1
    Why not something like '##username##' and replace that with your script? – putvande Jul 28 '13 at 19:00
  • Hm that works too. How do I replace ##username## with `$username` and store it as such in the DB? Also, if the email script takes a template that's like "Hi $username, how are you $today" will it replace all the variables with values or do I have to parse the template before I send it? Because if I insert `$username` into the database, I have a feeling that email will look like: "Hello $username". – Predrag Beocanin Jul 28 '13 at 19:03
  • Just have the admins write "$username", ##username## or whatever delimiter you define and store the template as is, then you parse it and replace with values in your email script, take a look at the question linked by mario – omma2289 Jul 28 '13 at 19:29

1 Answers1

3

You can do something like this:

$replaceWord = array(
    "[name]" => $row['username'],
    "[age]" => $row['age'],
);

$emailContent = strtr($emailContentDatabase, $replaceWord); //notice $emailContentDatabase is what there is saved in the database.

For example your e-mail in the database looks like this:

Welcome [name],

Your age is: [age].

Then the using the above script will output something like this

Welcome Perry,

Your age is: 45.
Perry
  • 11,172
  • 2
  • 27
  • 37
  • My code is too long to post it here, but it works exactly as you said it. I even assigned some variables that got calculated within the script to replace like `"[price]" => $Price,` and it plain works. Thanks a lot! – Predrag Beocanin Jul 30 '13 at 01:59
  • I am happy to hear it works! :) – Perry Jul 30 '13 at 07:42