Attachment.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php namespace App\Controllers;
  2. use CodeIgniter\Controller;
  3. use App\Andy\Utility;
  4. use PHPMailer\PHPMailer\PHPMailer;
  5. use PHPMailer\PHPMailer\OAuth;
  6. use PHPMailer\PHPMailer\Exception;
  7. use PHPMailer\PHPMailer\SMTP;
  8. use CodeIgniter\I18n\Time;
  9. class Attachment extends BaseController
  10. {
  11. protected $mimeType;
  12. protected $mimeLogic;
  13. protected $fileSize;
  14. protected $count;
  15. protected $newCount;
  16. protected $myTime;
  17. public function __construct()
  18. {
  19. parent::__construct();
  20. $this->myTime = parent::getTime();
  21. $this->myDate= date("d/m/Y",$this->myTime);
  22. }
  23. public function index()
  24. {
  25. echo "i am class Attachment, method index";
  26. }
  27. public function showForm()
  28. {
  29. $data =[
  30. 'title'=>'for attachment ',
  31. 'info'=>'&nbsp; ',
  32. 'count'=>'null',
  33. 'date'=>$this->myDate
  34. ];
  35. echo view('header',$data);
  36. echo view('attachForm',$data);
  37. echo view('footer',$data);
  38. }
  39. public function process()
  40. {
  41. session_start();
  42. helper('form');
  43. helper('html');
  44. $description = $this->request->getVar('fileDescription');
  45. $accessCode = $this->request->getVar('code');
  46. $file = $this->request->getFile('userfile');
  47. $fileName= $file->getName();
  48. $tempfile = $file->getTempName();
  49. $theFile = new \CodeIgniter\Files\File($tempfile);
  50. $this->mimeType= $theFile->getMimeType();
  51. $this->fileSize= $theFile->getSize('kb');
  52. $handle= new Utility();
  53. $this->mimeLogic= $handle->checkMime($this->mimeType);
  54. $ip = $_SERVER['REMOTE_ADDR'];
  55. $theIp = strval($ip);
  56. $theIp;
  57. $message =" enquiry for printing: \r\n"."their Ip address" .$theIp. " \r\n ". " file description ".$description. "";
  58. if ($accessCode!="000")
  59. {
  60. if(isset( $_SESSION['count']) ==false)
  61. {
  62. $_SESSION['count']= 5;
  63. $this->count= $_SESSION['count'];
  64. echo $this->count;
  65. }
  66. if(isset( $_SESSION['count']) ==true)
  67. {
  68. $this->count= $_SESSION['count'];
  69. $this->newCount=$this->count-1;
  70. $_SESSION['count']= $this->newCount;
  71. if ($_SESSION['count'] < 1)
  72. {
  73. return redirect('spam');
  74. }
  75. }
  76. $data =[
  77. 'title'=>'for attachment ',
  78. 'info'=>'you have no right to upload',
  79. ];
  80. echo view('header',$data);
  81. echo view('attachForm',$data);
  82. echo view('footer');
  83. exit();
  84. }
  85. if ($this->fileSize > 400 )
  86. {
  87. helper('form');
  88. helper('html');
  89. $data =[
  90. 'title'=>'for attachment ',
  91. 'info'=>'file too big max 400kb'
  92. ];
  93. echo view('header',$data);
  94. echo view('attachForm',$data);
  95. echo view('footer');
  96. exit();
  97. }
  98. if ($this->mimeLogic!=True)
  99. {
  100. helper('form');
  101. helper('html');
  102. $data =[
  103. 'title'=>'for attachment ',
  104. 'info'=>'thats not an allowed file '
  105. ];
  106. echo view('header',$data);
  107. echo view('attachForm',$data);
  108. echo view('footer');
  109. exit();
  110. }
  111. try
  112. {
  113. $mail = new PHPMailer();
  114. $mail->isSMTP();
  115. $mail->Timeout = 20;
  116. //Enable SMTP debugging, 0 = off (for production use), 1 = client messages, 2 = client and server messages
  117. $mail->SMTPDebug = 0;
  118. //Set the hostname of the mail server
  119. $mail->Host = 'smtp.gmail.com';
  120. $mail->Port = 587;
  121. $mail->SMTPSecure = 'tls';
  122. $mail->SMTPAuth = true;
  123. //Username to use for SMTP authentication - use full email address for gmail
  124. $mail->Username = "andybrookestar@gmail.com";
  125. //Password to use for SMTP authentication
  126. $mail->Password = "ewskcctyzsieqvhr";
  127. //Set who the message is to be sent from
  128. $mail->setFrom('andy@benxmidia.com', 'andy brookes');
  129. $mail->addAddress('andybrookestar@yahoo.com', 'andy');
  130. //$mail->addAddress('blackcatglm@gmail.com', 'benjain');
  131. $mail->addAttachment($tempfile,$fileName);
  132. //$mail->addReplyTo('replyto@example.com', 'First Last');
  133. //$mail->addAddress('ginaacheampong@gmail.com', 'gina');
  134. //$mail->AddCC('andybrookestar@yahoo.com','andy');
  135. //Set the subject line
  136. $mail->Subject = 'enquiry from benxmidia dev slackware ';
  137. //Read an HTML message body from an external file, convert referenced images to embedded,
  138. //convert HTML into a basic plain-text alternative body
  139. //$mail->msgHTML(file_get_contents('contents.html'), __DIR__);
  140. //Replace the plain text body with one created manually
  141. $mail->Body = $message;
  142. $logic2 = $result= $mail->send();
  143. }
  144. catch ( \Exception $e)
  145. {
  146. $data = [
  147. 'title' => 'info',
  148. 'info'=>'somwething went wrong '
  149. ];
  150. echo view('header', $data);
  151. echo view('info', $data);
  152. echo view('footer');
  153. die();
  154. //above try catch works
  155. }
  156. if($logic2== true)
  157. {
  158. $data =[
  159. 'title'=>'info ',
  160. 'info'=>'your file was sent'
  161. ];
  162. echo view('header',$data);
  163. echo view('info',$data);
  164. echo view('footer');
  165. }
  166. }//end process
  167. }//end class