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);
}
}