Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.81 KB | None | 0 0
  1. <?php
  2.  
  3. // you need to replace your SMTP login details here
  4. // this script is working for sending mail but not making a proper js call back yet
  5.  
  6. use PHPMailer\PHPMailer\PHPMailer;
  7. use PHPMailer\PHPMailer\Exception;
  8.  
  9. require 'phpmailer/Exception.php';
  10. require 'phpmailer/PHPMailer.php';
  11. require 'phpmailer/SMTP.php';
  12.  
  13.   //$mail->SMTPDebug  = 2;
  14.  
  15.   // this should catch a lot of spam bots
  16.   $honeypot = trim($_POST["email"]);
  17.  
  18.   if(!empty($honeypot)) {
  19.     echo "BAD ROBOT!";
  20.     exit;
  21.   }
  22.  
  23.   $msg = '';
  24.   //Don't run this unless we're handling a form submission
  25.   if (array_key_exists('email', $_POST)) {
  26.     date_default_timezone_set('Etc/UTC');
  27.     $mail = new PHPMailer;
  28.     //Tell PHPMailer to use SMTP - requires a local mail server
  29.     //Faster and safer than using mail()
  30.     $mail->isSMTP();                                      // Set mailer to use SMTP
  31.     $mail->Host = 'smtp.server.net';                      // Specify main and backup SMTP servers
  32.     $mail->SMTPAuth = true;                               // Enable SMTP authentication
  33.     $mail->Username = 'mailer@server.net';                // SMTP username
  34.     $mail->Password = 'mailerPASSWord';                   // SMTP password
  35.     $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
  36.     $mail->Port = 25;                                     // TCP port to connect to
  37.     //Use a fixed address in your own domain as the from address
  38.     //**DO NOT** use the submitter's address here as it will be forgery
  39.     //and will cause your messages to fail SPF checks
  40.     $mail->setFrom('sender@server.net', 'Your Name');
  41.     //Send the message to yourself, or whoever should receive contact for submissions
  42.  
  43.     $mail->addAddress('testsendmail@greenant.net', 'Your Name');
  44.     //Put the submitter's address in a reply-to header
  45.     //This will fail if the address provided is invalid,
  46.     //in which case we should ignore the whole request
  47.     if ($mail->addReplyTo($_POST['email_real'], $_POST['name'])) {
  48.         $mail->Subject = 'PHPMailer contact form';
  49.         //Keep it simple - don't use HTML
  50.         $mail->isHTML(false);
  51.         //Build a simple message body
  52.         $mail->Body = <<<EOT
  53. Email: {$_POST['email_real']}
  54. Name: {$_POST['name']}
  55. Message: {$_POST['message']}
  56. EOT;
  57.         //Send the message, check for errors
  58.         if (!$mail->send()) {
  59.             //The reason for failing to send will be in $mail->ErrorInfo
  60.             //but you shouldn't display errors to users - process the error, log it on your server.
  61.             $msg = 'Sorry, something went wrong. Please try again later.';
  62.         } else {
  63.             $msg = 'sent';
  64.         }
  65.     } else {
  66.         $msg = 'Invalid email address, message ignored.';
  67.     }
  68.  
  69.   echo $msg;
  70.   return $msg;
  71.   //exit;
  72.   }
  73.  
  74.  
  75.  
  76.  
  77. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement