Aamir Mahmood
Aamir Mahmood

Reputation: 2724

Multiple require_once of a file

I am working on a script developed by other developer. There is an alert system which sends email and each module have its own alert file and that alert file require_once PHPMailer

<?php
    // assume this is 1.php
    //.......... some code above //
    $mail_title = 'Message Title';
    $mail_body = 'Message body with complete details what has been done here';    
    require_once 'module_alert.php'; // <- PHP Mailer is requir_once in this file
    //........... ajax and some response //
?>

As soon as I require_once it generates an email and dispatch that.

Now I have to update a PHP page in a module. That page have to post data to 3 different php pages of module. Before that page was sending data to only one php page VIA ajax, and other two pages use First one's auto incremented ID.

Due to complexity I am thinking to include the other two files as well in file 1

<?php
    // assume this is 1.php
    //.......... some code above //
    $mail_title = 'Message Title';
    $mail_body = 'Message body with complete details what has been done here';    
    require_once 'module_alert.php'; // <- PHP Mailer is requir_once in this file
    //........... ajax and some response //
    if($condition){
        include '2.php'; // <- this file again require_once 'module_alert.php'
        include '3.php'; // <- this file again require_once 'module_alert.php'
    }
?>

The problem is that I want to send alert for all 3 steps, and also I dont want to change a whole lot of code in other files as I might break the script.

What are my options to survive in this condition?

EDIT

In module_alerts.php there are number of things. Mail layout, tables images, conditional recipient, who should get alert for this module etc. $mail->send() function so to send email to particular receiver.

Upvotes: 0

Views: 218

Answers (1)

jstephenson
jstephenson

Reputation: 2180

The logic to dispatch an email should be wrapped in a function which you can then call, with the appropriate parameters, at any point in your program. For example:

function sendMyEmail($recipient, $subject, $message)
{ ... }

You could of course build a class, a series of functions, really any sort of legitimate reusable facility and this would alleviate the problem both now and in the future.

HTH

Upvotes: 2

Related Questions