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);
}
}
1 Answer 1
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
-
\$\begingroup\$ But then what happens when I need to grab custom data in the body of an email? \$\endgroup\$jshjohnson– jshjohnson2014年04月03日 13:59:57 +00:00Commented Apr 3, 2014 at 13:59
-
\$\begingroup\$ 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. \$\endgroup\$azngunit81– azngunit812014年04月03日 15:24:12 +00:00Commented Apr 3, 2014 at 15:24
-
\$\begingroup\$ 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 saysendMail($firstName, $votes)
? \$\endgroup\$jshjohnson– jshjohnson2014年04月04日 09:16:43 +00:00Commented Apr 4, 2014 at 9:16 -
\$\begingroup\$ 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 \$\endgroup\$azngunit81– azngunit812014年04月04日 11:38:08 +00:00Commented Apr 4, 2014 at 11:38