1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Storing Outgoing & Incoming Messages

Discussion in 'PHP' started by oo7ml, Oct 19, 2012.

  1. #1
    Hi, my site sends out several (21 to be precise) different emails to its users, such as "Welcome to the site", "You have received a new PM", "Your email address has been updated", etc...

    All of these messages are saved in a email-out.php file.

    Question A: What is your view on moving and storing the emails in php syntax in the database so that i can edit the raw code from the admin section. I am thinking of leaving them in the email-out.php file as the content of the emails will not change that often, but would still like your input on the best practice / professional set up

    I also receive different incoming emails everyday from users and potential users asking questions and looking for general information. These emails come from different contact forms on the site.

    Question B: What is your view on storing all incomming emails that are sent from contact forms if the content is sanitised correctly, or should i just leave them in my inbox and manage them from there... like above, just looking for advise on the best practice / professional set up

    Thanks in advance...
     
    oo7ml, Oct 19, 2012 IP
  2. plussy

    plussy Peon

    Messages:
    152
    Likes Received:
    5
    Best Answers:
    9
    Trophy Points:
    0
    #2
    I would store both in a db. makes it much easier as you have them in a central place.
     
    plussy, Oct 19, 2012 IP
  3. oo7ml

    oo7ml Well-Known Member

    Messages:
    656
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    105
    #3
    Ok,would you save the outgoing emails in their raw php syntax
     
    oo7ml, Oct 19, 2012 IP
  4. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #4
    Can you post an example of what you mean by raw php syntax? I strongly oppose storing php or any application logic of any sort in a database.
     
    jestep, Oct 19, 2012 IP
  5. oo7ml

    oo7ml Well-Known Member

    Messages:
    656
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    105
    #5
    I mean variables, such as:

    Dear $receiver_first_name,

    etc...
     
    oo7ml, Oct 19, 2012 IP
  6. Wogan

    Wogan Peon

    Messages:
    81
    Likes Received:
    3
    Best Answers:
    2
    Trophy Points:
    0
    #6
    Storing the templates in your database will make it easier to manage - you can then replace (what must be a large) php file with a simpler one that just does the actual sending.

    Sticking to the PHP syntax will be a little more difficult though. I'm guessing that you have a function/script somewhere that populates all of those variables, then loads your email template (which does the substitutions before sending):

    $first_name = "John";
    $last_name = "Doe";
    
    $message = "Hi $first_name $last_name, you've won a million dollars!";
    
    mail('john.doe@example.org', 'Well done', $message);
    PHP:
    Is it something like that? If so, you can store your email templates in the following format:

    <p>Hi {FIRST_NAME} {LAST_NAME}, you've won a million dollars!</p>
    HTML:
    And then update your email sending code to do something like this:

    $tokens = array();
    $tokens["FIRST_NAME"] = "John";
    $tokens["LAST_NAME"] = "Doe";
    
    $message = getTemplate('welldone', $tokens);
    mail('john.doe@example.org', 'Well done', $message);
    
    // function
    function getTemplate($tpl, $tokens)
    {
        $content = mysql_result(mysql_query("SELECT * FROM `templates` WHERE `name` = '$tpl'"), 0, "content")); // Far from best practice :(
    
        foreach($tokens as $search => $replace) {
            $content = str_replace("{$search}", $replace, $content);
        }
    
        return $content;
    }
    PHP:
    Is that more or less what you're after? You can now write something to edit the "content" column in your "templates" table in your DB, and the changes will take effect immediately.

    As for storing incoming emails, I wouldn't bother. Email is a notoriously unpredictable medium. You can easily grab and store the content, but there's no real benefit to it if you're already using a web-hosted mail interface.
     
    Wogan, Oct 19, 2012 IP
  7. oo7ml

    oo7ml Well-Known Member

    Messages:
    656
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    105
    #7
    Cool, all makes perfect sense, thanks for all of this...
     
    oo7ml, Oct 19, 2012 IP