Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- use PHPMailer\PHPMailer\PHPMailer;
- use PHPMailer\PHPMailer\Exception;
- require 'PHPMailer/src/Exception.php';
- require 'PHPMailer/src/PHPMailer.php';
- require 'PHPMailer/src/SMTP.php';
- $db_host = "localhost";
- $db_username = "root";
- $db_password = "password";
- $db_database = "mail";
- $db_tablename = "tbl_system_mail_pickup";
- $mail_username = "";
- $mail_password = "";
- function fetch_body($id, $args)
- {
- $body = null;
- switch($id)
- {
- case 0:
- $body = "<div><span style=\"color: #e68038;\"><strong>The ID below has alredy been registered</strong></span></div>
- <div> </div>
- <div><span style=\"color: #000080;\">Your registration of foundit.ie label ID is now active</span></div>
- <div> </div>
- <div><span style=\"color: #000080;\">ID: {0}</span></div>
- <div><span style=\"color: #000080;\">Name: {1}</span></div>
- <div> </div>
- <div><span style=\"color: #000080;\">Once a found item is reported with your ID an email will be sent to all the registered email addresses notifying you how to recover your lost property.</span></div>
- <div> </div>
- <div><strong><span style=\"color: #e68038;\">The foundit.ie team</span></strong><br /> <img src=\"https://foundit.ie/images/FoundIt-ie-logo-banner.png\" alt=\"FoundIt ie logo banner\" width=\"124\" height=\"78\" /></div>
- <p><span style=“color: #800080;\">To contact foundit.ie please see our website http://foundit.ie or email info@foundit.ie.</span></p>";
- break;
- case 1:
- $body = "<div><span style=\"color: #e68038;\"><strong>This ID has alredy been registered</strong></span></div>
- <div> </div>
- <div><span style=\"color: #000080;\">Your registration of foundit.ie label ID {0} has failed as it has already been registered. </span></div>
- <div> </div>
- <div><span style=\"color: #000080;\">Please contact us if you feel this is an error.</span></div>
- <div> </div>
- <div><strong><span style=\"color: #e68038;\">The foundit.ie team</span></strong><br /> <img src=\"https://foundit.ie/images/FoundIt-ie-logo-banner.png\" alt=\"FoundIt ie logo banner\" width=\"124\" height=\"78\" /></div>
- <p><span style=“color: #800080;\">To contact foundit.ie please see our website http://foundit.ie or email info@foundit.ie.</span></p>";
- break;
- default:
- throw new Exception("Invalid msgid: " . $id);
- }
- foreach($args as $i => $val)
- {
- echo "{" . $i . "} => " . $val . "\n";
- $body = str_replace("{".$i."}",$val,$body);
- }
- return $body;
- }
- function send_mail($row)
- {
- global $mail_username, $mail_password;
- $mail = new PHPMailer(true); // Passing `true` enables exceptions
- try {
- //Server settings
- $mail->SMTPDebug = 2; // Enable verbose debug output
- $mail->isSMTP(); // Set mailer to use SMTP
- $mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
- $mail->SMTPAuth = true; // Enable SMTP authentication
- $mail->Username = $mail_username; // SMTP username
- $mail->Password = $mail_password; // SMTP password
- $mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
- $mail->Port = 587; // TCP port to connect to
- //Recipients
- $mail->setFrom($row["mFrom"]);
- $mail->addAddress($row["mTo"]); // Add a recipient
- if ($row["mCC"] != null && !empty($row["mCC"]))
- $mail->addCC($row["mCC"]);
- if ($row["mBCC"] != null && !empty($row["mBCC"]))
- $mail->addBCC($row["mBCC"]);
- //Content
- $mail->isHTML(true); // Set email format to HTML
- $mail->Subject = $row["mSubject"];
- $mail->Body = fetch_body($row["msgid"], explode(";",$row["msgargs"]));
- $mail->send();
- return true;
- } catch (Exception $e) {
- echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
- return false;
- }
- }
- // Create connection
- $conn = new mysqli($db_host, $db_username, $db_password, $db_database);
- // Check connection
- if ($conn->connect_error)
- {
- die("Connection failed: " . $conn->connect_error);
- }
- $stmt = $conn->prepare("UPDATE " . $db_tablename . " SET email_sent = TRUE WHERE id=?");
- if(!$stmt)
- {
- die("Query failed: (". $conn->errno . ") " . $conn->error);
- }
- $res = $conn->query("SELECT * FROM " . $db_tablename . " WHERE email_sent;");
- if(!$res)
- {
- die("Query failed: (". $conn->errno . ") " . $conn->error);
- }
- while ($row = $res->fetch_assoc()) {
- if(send_mail($row))
- {
- $stmt->bind_param("i",$row["id"]);
- $stmt->execute();
- $stmt->reset();
- echo "Email sent to " . $row["mTo"] . "\r\n";
- }
- else
- {
- echo "Failed to send email: " . error_get_last()['message'] . "\n";
- }
- }
- $res->close();
- $conn->close();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement