Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

So the two functions below both send emails via PHPMailer but both hold different messages (which use different data from the db). I was just wondering (as I plan to use PHPMailer more) a way to consolidate these functions into perhaps a sendMail() function which could handle different data variables and messages, whilst also allowing me to only set the $mail parameters once.

     public function sendConfirmationEmail($firstName, $email, $emailCode) {

        global $mail;

        $to = $email;

        try {
            $mail->IsSMTP(); 
            $mail->Username           = "[email protected]";
            $mail->Password           = "••••••••"; 
            $mail->SMTPAuth           = true;            
            $mail->SMTPSecure         = "tls"; 
            $mail->Host               = "smtp.gmail.com";  
            $mail->Port               = 587; 
            $mail->addAddress($to);  

            $mail->From               = '[email protected]';
            $mail->FromName           = 'Connectd.io';
            $mail->AddReplyTo( '[email protected]', 'Contact Connectd.io' );

            $mail->isHTML(true); 

            $mail->Subject            = 'Activate your new Connectd account';

            $mail->Body               = "<p>Hey " . $firstName . "!</p>";
            $mail->Body              .= "<p>Thank you for registering with Connectd. Please visit the link below so we can activate your account:</p>";
            $mail->Body              .= "<p>" . BASE_URL . "login.php?email=" . $email . "&email_code=" . $emailCode . "</p>";
            $mail->Body              .= "<p>-- Connectd team</p>";
            $mail->Body              .= "<p><a href='http://connectd.io'>www.connectd.io</a></p>";
            $mail->Body              .= "<img width='180' src='" . BASE_URL . "assets/img/logo-email.jpg' alt='Connectd.io logo'><br>";

            $mail->Send();

        }catch(phpmailerException $e) {
            $general = new General($db);
            $general->errorView($general, $e);
        }catch(Exception $e) {
            $general = new General($db);
            $general->errorView($general, $e);
        }
    }


    public function sendVoteEmail($firstName, $email, $votes) {

        global $mail;

        $to = $email;

        try {
            $mail->IsSMTP(); 
            $mail->Username           = "[email protected]";
            $mail->Password           = "••••••••";
            $mail->SMTPAuth           = true; 
            $mail->SMTPSecure         = "tls"; 
            $mail->Host               = "smtp.gmail.com";
            $mail->Port               = 587; 
            $mail->addAddress($to);

            $mail->From               = '[email protected]';
            $mail->FromName           = 'Connectd.io';
            $mail->AddReplyTo('[email protected]', 'Contact Connectd.io');

            $mail->isHTML(true); 

            $mail->Subject            = 'You just got a vote on Connectd Trials!';

            $mail->Body               = "<p>Hey " . $firstName . "!</p>";
            $mail->Body              .= "<p>Congratulations - Someone has voted for you on Connectd Trials. </p>";
            $mail->Body              .= "<p>You now have <strong>" . $votes['CountOfvote_id'] . "/10</strong> votes</p>";
            $mail->Body              .= "<p>-- Connectd team</p>";
            $mail->Body              .= "<p><a href='http://connectd.io'>www.connectd.io</a></p>";
            $mail->Body              .= "<img width='180' src='" . BASE_URL . "assets/img/logo-email.jpg' alt='Connectd.io logo'><br>";

            $mail->Send();

        }catch(phpmailerException $e) {
            $general = new General($db);
            $general->errorView($general, $e);
        }catch(Exception $e) {
            $general = new General($db);
            $general->errorView($general, $e);
        }
    }
share|improve this question

You should pass the body of the mail as an argument and it will do what you need. The body part should have been on the outside and independent of the mailing function. This way if you decide to expand this - you wont need to copy paste a third version

share|improve this answer
    
But then what happens when I need to grab custom data in the body of an email? – jshjohnson Apr 3 '14 at 13:59
    
you do it from the outside. you build the entire body FROM the outside..its about the equivalent of calculating an area of a shape. the formula is inside the function, the length, width, height are coming from the outside. – azngunit81 Apr 3 '14 at 15:24
    
So say the body is just an included PHP file, and the function is just sendMail() - surely I will get an unexpected variable error if I pass say sendMail($firstName, $votes)? – jshjohnson Apr 4 '14 at 9:16
    
there are no unexpected variable if firstName and votes are blank. The send will simply fail and you need to add validation. In PHP you need your important variables to have checks on it. Furthermore good programming tells you that also. What YOU want to do is require_once a template that has variables inside because the HTML is formatted. The proper way is to have a function that takes an input, array, DB result or something and returns a variable which is your $body and within that function you validate all your variables. If something is missing and is of value you will need to catch it – azngunit81 Apr 4 '14 at 11:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.