Advertisement
Guest User

email.php

a guest
Sep 6th, 2018
3,271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.76 KB | None | 0 0
  1. <?php
  2. use PHPMailer\PHPMailer\PHPMailer;
  3. use PHPMailer\PHPMailer\Exception;
  4.  
  5. require 'PHPMailer/src/Exception.php';
  6. require 'PHPMailer/src/PHPMailer.php';
  7. require 'PHPMailer/src/SMTP.php';
  8.  
  9. $db_host = "localhost";
  10. $db_username = "root";
  11. $db_password = "password";
  12. $db_database = "mail";
  13. $db_tablename = "tbl_system_mail_pickup";
  14.  
  15. $mail_username = "";
  16. $mail_password = "";
  17.  
  18.  
  19.  
  20. function fetch_body($id, $args)
  21. {
  22.     $body = null;
  23.     switch($id)
  24.     {
  25.     case 0:
  26.         $body = "<div><span style=\"color: #e68038;\"><strong>The ID below has alredy been registered</strong></span></div>
  27. <div>&nbsp;</div>
  28. <div><span style=\"color: #000080;\">Your registration of foundit.ie label ID is now active</span></div>
  29. <div>&nbsp;</div>
  30. <div><span style=\"color: #000080;\">ID: {0}</span></div>
  31. <div><span style=\"color: #000080;\">Name: {1}</span></div>
  32. <div>&nbsp;</div>
  33. <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>
  34. <div>&nbsp;</div>
  35. <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>
  36. <p><span style=“color: #800080;\">To contact foundit.ie please see our website http://foundit.ie or email info@foundit.ie.</span></p>";
  37.  
  38.         break;
  39.     case 1:
  40.         $body = "<div><span style=\"color: #e68038;\"><strong>This ID has alredy been registered</strong></span></div>
  41. <div>&nbsp;</div>
  42. <div><span style=\"color: #000080;\">Your registration of foundit.ie label ID {0} has failed as it has already been registered.&nbsp;</span></div>
  43. <div>&nbsp;</div>
  44. <div><span style=\"color: #000080;\">Please contact us if you feel this is an error.</span></div>
  45. <div>&nbsp;</div>
  46. <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>
  47. <p><span style=“color: #800080;\">To contact foundit.ie please see our website http://foundit.ie or email info@foundit.ie.</span></p>";
  48.  
  49.         break;
  50.  
  51.     default:
  52.         throw new Exception("Invalid msgid: " . $id);
  53.     }
  54.    
  55.     foreach($args as $i => $val)
  56.     {
  57.         echo "{" . $i . "} => " . $val . "\n";
  58.         $body = str_replace("{".$i."}",$val,$body);
  59.     }
  60.     return $body;
  61. }
  62.  
  63. function send_mail($row)
  64. {
  65.     global $mail_username, $mail_password;
  66.    
  67.     $mail = new PHPMailer(true);                              // Passing `true` enables exceptions
  68.     try {
  69.         //Server settings
  70.         $mail->SMTPDebug = 2;                                 // Enable verbose debug output
  71.         $mail->isSMTP();                                      // Set mailer to use SMTP
  72.         $mail->Host = 'smtp.gmail.com';                       // Specify main and backup SMTP servers
  73.         $mail->SMTPAuth = true;                               // Enable SMTP authentication
  74.         $mail->Username = $mail_username;           // SMTP username
  75.         $mail->Password = $mail_password;                       // SMTP password
  76.         $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
  77.         $mail->Port = 587;                                    // TCP port to connect to
  78.  
  79.         //Recipients
  80.         $mail->setFrom($row["mFrom"]);
  81.         $mail->addAddress($row["mTo"]);     // Add a recipient
  82.         if ($row["mCC"] != null && !empty($row["mCC"]))
  83.             $mail->addCC($row["mCC"]);
  84.         if ($row["mBCC"] != null && !empty($row["mBCC"]))
  85.         $mail->addBCC($row["mBCC"]);
  86.  
  87.         //Content
  88.         $mail->isHTML(true);                                  // Set email format to HTML
  89.         $mail->Subject = $row["mSubject"];
  90.         $mail->Body    = fetch_body($row["msgid"], explode(";",$row["msgargs"]));
  91.  
  92.         $mail->send();
  93.         return true;
  94.     } catch (Exception $e) {
  95.         echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
  96.         return false;
  97.     }
  98. }
  99.  
  100.  
  101.  
  102. // Create connection
  103. $conn = new mysqli($db_host, $db_username, $db_password, $db_database);
  104.  
  105. // Check connection
  106. if ($conn->connect_error)
  107. {
  108.     die("Connection failed: " . $conn->connect_error);
  109. }
  110.  
  111. $stmt = $conn->prepare("UPDATE " . $db_tablename . " SET email_sent = TRUE WHERE id=?");
  112. if(!$stmt)
  113. {
  114.     die("Query failed: (". $conn->errno . ") " . $conn->error);
  115. }
  116.  
  117. $res = $conn->query("SELECT * FROM " . $db_tablename . " WHERE email_sent;");
  118.  
  119. if(!$res)
  120. {
  121.     die("Query failed: (". $conn->errno . ") " . $conn->error);
  122. }
  123.  
  124. while ($row = $res->fetch_assoc()) {
  125.     if(send_mail($row))
  126.     {
  127.         $stmt->bind_param("i",$row["id"]);
  128.         $stmt->execute();
  129.         $stmt->reset();
  130.         echo "Email sent to " . $row["mTo"] . "\r\n";
  131.     }
  132.     else
  133.     {
  134.         echo "Failed to send email: " . error_get_last()['message'] . "\n";
  135.     }
  136. }
  137.  
  138. $res->close();
  139.  
  140. $conn->close();
  141. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement