SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,756
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    How to manage things like Contact E-mail?

    There are maybe 10-15 places in my website where I need to send out an e-mail.

    This might be to Members confirming a registration, or to myself letting me know some System error occurred.

    Right now I just define my Admin e-mail in a variable, but in retrospect, that seems awfully inefficient!

    I have even added a // LOOK comment in each script next to each line with my e-mail so I remember where to change it if needed.

    PHP Code:
    // LOOK
        
    $emailTo 'Debbie@mail.com'

    Is there a better approach to take managing my e-mail?


    I had considered sticking it in a CONSTANT, but for some unknown reason that seems like a Security Risk?!

    Sincerely,


    Debbie

  2. #2
    SitePoint Enthusiast
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    88
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    To send yourself an email, I'd create an 'include' script that assigns it to a var and then add it as a prepend to all my scripts. You could also use a db to store the email and use that include script to read it from there, but a simple assignment of a string is the simplest. One change when necessary and all your scripts requiring it will have it.

    Of course you don't have to use the prepend feature - simply add the include line to your php template (if you have one) and it will be there with every new script, and you can drop it if you don't need it for that script.

  3. #3
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,756
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ginerjm View Post
    To send yourself an email, I'd create an 'include' script that assigns it to a var and then add it as a prepend to all my scripts. You could also use a db to store the email and use that include script to read it from there, but a simple assignment of a string is the simplest. One change when necessary and all your scripts requiring it will have it.

    Of course you don't have to use the prepend feature - simply add the include line to your php template (if you have one) and it will be there with every new script, and you can drop it if you don't need it for that script.
    I don't know what you mean about "prepending"...

    What about defining my Admin Email as a Constant?


    Debbie

  4. #4
    SitePoint Enthusiast
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    88
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    A 'prepend script' is something you can set in your .ini file. It points to a piece of code (like something you would include) that will go in front of every script you run. As I said it's an option, but you can just as easily have an include statement pointing to a small piece of code in your standard template that you use to begin every new script.

    Contant/ variable that's set - no difference. You have to set the constant every time as well.

    Something like:

    Code:
    <?
    //      This is /inc/myemailaddr.php
    $MyEmailAddr = 'johndoe@yahoo.com';
    and then assuming you have some kind of template that starts like :

    Code:
    <?
    session_start();
    header("Cache-Control: no-cache");
    $root = $_SERVER['DOCUMENT_ROOT']."/";
    you would add this line to it:

    Code:
    include($root . "inc/myemailaddr.php");
    When you need to modify it you simply change the myemailaddr.php logic to store a new address. All your scripts will then be using the new address.

  5. #5
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,756
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Why not just use a Constant and define it once, in one place?


    Debbie

  6. #6
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,050
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DoubleDee View Post
    Why not just use a Constant and define it once, in one place?


    Debbie
    Yes, do that.
    PHP Code:
    define('MYEMAIL''myaddress@mydomain.com'); 
    Or if you have a Email Class that handles the sending of all emails from your site, put it as a constant in there.
    PHP Code:
    class Email
    {
      const 
    MYEMAIL 'myaddress@mydomain.com';
      
    // rest of class below

    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  7. #7
    SitePoint Enthusiast
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    88
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    If you have a block of code that you are including in your scripts that send emails to you, well, of course you would put the address in there, either as a var or a constant. This is not much different from what I was saying above. You just didn't mention that you had an email module so I skipped that.

    The point here is that you can't define a constant in one place unless it is in a database. You keep asking about a Constant, but you have to realize it is nothing more than a variable that gets assigned but is readonly. It still has to be defined at some point in every script.

    If you have a template that is your starting point for developing new scripts, I'd recommend doing it that way. Once you've set it up, you'll eventually forget all about it.

  8. #8
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,756
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ginerjm View Post
    If you have a block of code that you are including in your scripts that send emails to you, well, of course you would put the address in there, either as a var or a constant. This is not much different from what I was saying above. You just didn't mention that you had an email module so I skipped that.

    The point here is that you can't define a constant in one place unless it is in a database. You keep asking about a Constant, but you have to realize it is nothing more than a variable that gets assigned but is readonly. It still has to be defined at some point in every script.

    If you have a template that is your starting point for developing new scripts, I'd recommend doing it that way. Once you've set it up, you'll eventually forget all about it.
    What??

    You might want to read the Manual before you keep giving advice on Constants and their scope...

    http://php.net/manual/en/language.constants.php






    Debbie

  9. #9
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,756
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    Yes, do that.
    PHP Code:
    define('MYEMAIL''myaddress@mydomain.com'); 
    Or if you have a Email Class that handles the sending of all emails from your site, put it as a constant in there.
    PHP Code:
    class Email
    {
      const 
    MYEMAIL 'myaddress@mydomain.com';
      
    // rest of class below

    So on a related note, cpradio, how could I make the following code more efficient...

    PHP Code:
        // Log Error in Database.                                                        *

        // Build Query.
        // Execute query.
        // Verify Insert.
        
    if (mysqli_stmt_affected_rows($stmt1)==1){
            
    // Error Logged in DB.
            // Continue processing...

        
    }else{
            
    // Error Not Logged in DB.
            // Email Admin about Failed Insert.
            // LOOK
                
    $emailTo 'Debbie@mail.com';

                
    $headers  "From: Admin <admin@MySite.com>\r\n";
                
    $headers .= "MIME-Version: 1.0\r\n";
                
    $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

                
    $subject_FailedInsert 'Re: Unloggable Website Error  (' $currTime ')';

                
    $body_FailedInsert "<p>" $currTime ."</p>\r\n";
                
    $body_FailedInsert .= "<p>Dear Admin,</p>\r\n";
                
    $body_FailedInsert .= "<p>A website error has occurred, but could not be logged in the database.</p>\r\n";
                
    $body_FailedInsert .= "<p>Please take immediate action!!</p>\r\n";

                
    mail($emailTo$subject_FailedInsert$body_FailedInsert$headers);

            
    // Continue processing...

        
    }// End of LOG ERROR IN DATABASE 

    That is a lot of code, but if you look closer, most of it is just me defining the E-mail From, Subject, and Body.

    I can't think of a way to make it more efficient, because even if I made that a Class or Function, I would still need to have the verbose arguments to my mail function, right??

    Sincerely,


    Debbie

  10. #10
    SitePoint Enthusiast
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    88
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I actually read that very page cause i wasnt sure about the permanence of a define.

    Didnt see anything to dissuade me from my stand. Can u explain what im missing?

  11. #11
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,050
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Well, I can only think of a small few ways, but it is based on whether all of the different email scenarios you have can follow a template(s). Think of the following:

    error.template
    HTML Code:
    <p>{current_time}</p>
    <p>Dear Admin,</p>
    <p>A website error has occurred, but could not be logged in the database.</p>
    <p>Please take immediate action!!</p>
    <p>Error occurred in {function_name} trying to do {action}</p>
    sendemail.class.php
    PHP Code:
    class SendEmail
    {
      public function 
    HtmlEmail($to$from$subject$template$arguments)
      {
        
    $template file_get_contents('templates/' $template);
        foreach (
    $arguments as $key => $val)
        {
          
    $template str_replace("{$key}"$val$template);
          
    $subject str_replace("{$key}"$val$template);
        }

        
    $headers  "From: Admin <{$from}>\r\n"
        
    $headers .= "MIME-Version: 1.0\r\n"
        
    $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

         
    mail($to$subject$template$headers);
      }

    Usuage:
    PHP Code:
        // Log Error in Database.                                                        * 

        // Build Query. 
        // Execute query. 
        // Verify Insert. 
        
    if (mysqli_stmt_affected_rows($stmt1)==1){ 
            
    // Error Logged in DB. 
            // Continue processing... 

        
    }else{ 
            
    $sendMail = new SendMail();
            
    $arguments = array('current_time' => $currTime'function_name' => 'MyFunction''action' => 'Inserting Comment');
            
    $sendMail->HtmlEmail('myemail@emailaddress.com''admin@myemail.com''Re: Unloggable Website Error  ({current_time})''error.template'$arguments);
        }
    // End of LOG ERROR IN DATABASE 
    I'm sure there is a better way yet, but that is a quick and dirty approach to it.
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  12. #12
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,756
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ginerjm View Post
    I actually read that very page cause i wasnt sure about the permanence of a define.

    Didnt see anything to dissuade me from my stand. Can u explain what im missing?

    From the PHP Manual...
    Like superglobals, the scope of a constant is global. You can access constants anywhere in your script without regard to scope. For more information on scope, read the manual section on variable scope.

  13. #13
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,756
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    Well, I can only think of a small few ways, but it is based on whether all of the different email scenarios you have can follow a template(s).

    I'm sure there is a better way yet, but that is a quick and dirty approach to it.
    So for now, I guess I will create an Admin Email Constant in my config.inc.php file and reference that in my code.

    At least that way I only have to go to one place to change my e-mail everywhere.

    And as far as the differing Email Subjects and Bodies, I guess what I have will work for now. Although it is definitely a topic to be revisited!

    Thanks,


    Debbie


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •