I have a function that sends emails from the site (using phpmailer), what I want to do, mainly for php, is to replace all the plachholders in the email.tpl file with the content that I feed it. The problem for me is that I do not want to repeat the code, so I created a function (below).
Without a php function, I would do the following in a script
// email template file $email_template = "email.tpl"; // Get contact form template from file $message = file_get_contents($email_template); // Replace place holders in email template $message = str_replace("[{USERNAME}]", $username, $message); $message = str_replace("[{EMAIL}]", $email, $message);
Now I know how to do the rest, but I'm stuck on str_replace() as shown above. I have several str_replace() functions to replace placeholders in an email template. I would like to add the str_replace() function to my function (see below) and get it to find all the instances [\] in the email template that I give it and replace it with placeholder values that I will give you as follows: str_replace("[\]", 'replace_with', $email_body)
The problem is that I don’t know how to pass multiple placeholders and their replacement values to my function and get str_replace("[{\}]", 'replace_with', $email_body) to handle all the placeholders that I give them, and replace them with the appropriate values.
Since I want to use this function in several places and to avoid code duplication, in some scripts I can pass the function 5 placeholders and there are values, and another script may need to pass 10 placeholders and use the values for the function in the email template there .
I'm not sure if I will need to use an array in script (s) that will use the function and the for loop in the function, perhaps to force my php function to accept xx placeholders and xx values from the script, and to scroll the placeholders and replace their values there.
Here is my function, which I mentioned above. I commented on a script that might explain a lot easier.
// WILL NEED TO PASS PERHAPS AN ARRAY OF MY PLACEHOLDERS AND THERE VALUES FROM x SCRIPT // INTO THE FUNCTION ? function phpmailer($to_email, $email_subject, $email_body, $email_tpl) { // include php mailer class require_once("class.phpmailer.php"); // send to email (receipent) global $to_email; // add the body for mail global $email_subject; // email message body global $email_body; // email template global $email_tpl; // get email template $message = file_get_contents($email_tpl); // replace email template placeholders with content from x script // FIND ALL INSTANCES OF [{}] IN EMAIL TEMPLATE THAT I FEED THE FUNCTION // WITH AND REPLACE IT WITH THERE CORRESPOING VALUES. // NOT SURE IF I NEED A FOR LOOP HERE PERHAPS TO LOOP THROUGH ALL // PLACEHOLDERS I FEED THE FUNCTION WITH AND REPLACE WITH THERE CORRESPONDING VALUES $email_body = str_replace("[{\}]", 'replace', $email_body); // create object of PHPMailer $mail = new PHPMailer(); // inform class to use smtp $mail->IsSMTP(); // enable smtp authentication $mail->SMTPAuth = SMTP_AUTH; // host of the smtp server $mail->Host = SMTP_HOST; // port of the smtp server $mail->Port = SMTP_PORT; // smtp user name $mail->Username = SMTP_USER; // smtp user password $mail->Password = SMTP_PASS; // mail charset $mail->CharSet = MAIL_CHARSET; // set from email address $mail->SetFrom(FROM_EMAIL); // to address $mail->AddAddress($to_email); // email subject $mail->Subject = $email_subject; // html message body $mail->MsgHTML($email_body); // plain text message body (no html) $mail->AltBody(strip_tags($email_body)); // finally send the mail if(!$mail->Send()) { echo "Mailer Error: " . $mail->ErrorInfo; } else { echo "Message sent Successfully!"; } }