Re: How do YOU set default function/method params?
| From: | Al | Date: | Tue, 06 Oct 2009 12:17:21 +0000 |
| Subject: | Re: How do YOU set default function/method params? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-298641@lists.php.net to get a copy of this message | ||
Jim Lucas wrote:
Here is a problem that I have had for years now. I have been trying to come up with the perfect solution for this problem. But, I have come down to two different methods for solving it. Here is the problem... <?php function sendEmail(To me the key word in your question is "default". Here is a send mail example of how I do it. You'll see that I assign default stuff in the function. The all caps are constants set in my config file. For extremely high volume applications, one could memory cache the defaults. I also use arrays assigned in my config file and then assign the array in the function using "global". When I do this, I immediately reassign the array so the function can't change the the assignments made in the config. e.g., function foo() global booArray(); boo2Array= booArray(); Only use boo2Array() in the function. function pearEmailSend($recipient, $emailSubj, $emailText, $applicEmailAddr) {$to, $from, $subject, $body, $attachments=array(), $headers=array() ) { # I typically do not put each argument on seperate lines, but I ran #out of width in this email...# do something here... mail(...);} sendEmail('john@doe.com','marykate@uhhh.net', 'Hi!', 'Check out my new pictures!!!', $hash_array_of_pictures );Now, we all have a function or method like this floating around somewhere. My question is, how do YOU go about setting the required entries of the $headers array() ? I see three possible solutions. I want to see a clean and simple solution. Here are my ideas so far: function sendEmail($to, $from, $subject, $body, $attachments=array(), $headers=array() ) { # I typically do not put each argument on seperate lines, but I ran #out of width in this email...if ( empty($headers['Date']) ) { $headers['Date'] = date('c'); } if ( empty($headers['Message-ID']) ) { $headers['Date'] = md5($to.$subject); } # and the example goes on...# do something here... mail(...);} Or, another example. (I will keep it to the guts of the solution now)$headers['Date'] = empty($headers['Date']) ? date('c') : $headers['Date']; $headers['Message-ID'] = empty($headers['Message-ID']) ? md5($to.$subject) : $headers['Message-ID'];OR, yet another example... $defaults = array('Date' => date('c'), 'Message-ID' => md5($to.$subject),); $headers += $defaults; END of examples... Now, IMO, the last one is the simplest one and for me, I think it will be the new way that I solve this type of problem. But, my question that I put out to all of you is... How would you solve this problem? TIA Jim Lucas
$emailTo = $recipient;
$headers['From'] = $applicEmailAddr;
$headers['To'] = $emailTo;
if(!empty($emailCC)) $headers['Cc'] = $emailCC;
$headers['Return-Path'] = $applicEmailAddr; //or can use SMTP_USER; bounces are sent to applic address
$headers['Reply-To'] = $applicEmailAddr;
$headers['X-miniReg'] = APPLIC_NAME;
$headers['Date'] = date('r');
$headers['Subject'] = $emailSubj;
$params['debug'] = EMAIL_DEBUG; //Careful, do not leave on, creates a nasty message for admins
$params['host'] = $_SERVER['SERVER_NAME'];
$params['auth'] = true; //binary, set in config; some servers require auth
$params["username"] = $applicEmailAddr; //was SMTP_USER; //If auth true, must have value
$params["password"] = SMTP_PW; //If auth true, must have value
$params["localhost"] = $_SERVER['SERVER_NAME'];
$params['persist'] = true; //Default true
$mail_object = @Mail::factory(EMAIL_MODE, $params);