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.


// 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?! :shifty:

Sincerely,

Debbie

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

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:



<?
//      This is /inc/myemailaddr.php
$MyEmailAddr = 'johndoe@yahoo.com';


and then assuming you have some kind of template that starts like :



<?
session_start();
header("Cache-Control: no-cache");
$root = $_SERVER['DOCUMENT_ROOT']."/";


you would add this line to it:



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.

Why not just use a Constant and define it once, in one place?

Debbie

Yes, do that.

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.

class Email
{
  const MYEMAIL = 'myaddress@mydomain.com';
  // rest of class below
}

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

:wink:

Debbie

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


	// 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\
";
			$headers .= "MIME-Version: 1.0\\r\
";
			$headers .= "Content-Type: text/html; charset=ISO-8859-1\\r\
";

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

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

			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

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?

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

<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

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\
"; 
    $headers .= "MIME-Version: 1.0\\r\
"; 
    $headers .= "Content-Type: text/html; charset=ISO-8859-1\\r\
";

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

Usuage:

    // 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.

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.

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