SHARE
TWEET

PHPMailer Failer

a guest Jun 7th, 2017 213 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. I'm attempting to use PHPMailer to set up my email to receive emails/messages via a contact form on my website. I'm having trouble setting it up. Submitting the contact form calls this php file `sendemail.php`:
  3.  
  4.     <?php
  5.    
  6.     require_once('phpmailer/PHPMailerAutoload.php');
  7.    
  8.     $toemails = array();
  9.    
  10.     $toemails[] = array(
  11.                     'email' => 'myRealEmail@yahoo.com', // Your Email Address
  12.                     'name' => 'My Name' // Your Name
  13.                 );
  14.    
  15.     // Form Processing Messages
  16.     $message_success = 'We have <strong>successfully</strong> received your Message and will get Back to you as soon as possible.';
  17.    
  18.     // Add this only if you use reCaptcha with your Contact Forms
  19.     $recaptcha_secret = 'your-recaptcha-secret-key'; // Your reCaptcha Secret
  20.    
  21.     $mail = new PHPMailer();
  22.    
  23.     // If you intend you use SMTP, add your SMTP Code after this Line
  24.     $mail->IsSMTP();
  25.     $mail->Host = "smtp.mail.yahoo.com";
  26.     $mail->SMTPDebug = 0;
  27.     $mail->SMTPAuth = true;
  28.     $mail->Port = 587;
  29.     $mail->SMTPSecure = 'tls';
  30.     // $mail->SMTPSecure = 'ssl';
  31.     $mail->Username = "---@yahoo.com";
  32.     $mail->Password = "---";
  33.    
  34.    
  35.     if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
  36.         if( $_POST['template-contactform-email'] != '' ) {
  37.    
  38.             $name = isset( $_POST['template-contactform-name'] ) ? $_POST['template-contactform-name'] : '';
  39.             $email = isset( $_POST['template-contactform-email'] ) ? $_POST['template-contactform-email'] : '';
  40.             $phone = isset( $_POST['template-contactform-phone'] ) ? $_POST['template-contactform-phone'] : '';
  41.             $service = isset( $_POST['template-contactform-service'] ) ? $_POST['template-contactform-service'] : '';
  42.             $subject = isset( $_POST['template-contactform-subject'] ) ? $_POST['template-contactform-subject'] : '';
  43.             $message = isset( $_POST['template-contactform-message'] ) ? $_POST['template-contactform-message'] : '';
  44.    
  45.             $subject = isset($subject) ? $subject : 'New Message From Contact Form';
  46.    
  47.             $botcheck = $_POST['template-contactform-botcheck'];
  48.    
  49.             if( $botcheck == '' ) {
  50.    
  51.                 $mail->SetFrom( $email , $name );
  52.                 $mail->AddReplyTo( $email , $name );
  53.                 foreach( $toemails as $toemail ) {
  54.                     $mail->AddAddress( $toemail['email'] , $toemail['name'] );
  55.                 }
  56.                 $mail->Subject = $subject;
  57.    
  58.                 $name = isset($name) ? "Name: $name<br><br>" : '';
  59.                 $email = isset($email) ? "Email: $email<br><br>" : '';
  60.                 $phone = isset($phone) ? "Phone: $phone<br><br>" : '';
  61.                 $service = isset($service) ? "Service: $service<br><br>" : '';
  62.                 $message = isset($message) ? "Message: $message<br><br>" : '';
  63.    
  64.                 $referrer = $_SERVER['HTTP_REFERER'] ? '<br><br><br>This Form was submitted from: ' . $_SERVER['HTTP_REFERER'] : '';
  65.    
  66.                 $body = "$name $email $phone $service $message $referrer";
  67.    
  68.                 // Runs only when File Field is present in the Contact Form
  69.                 if ( isset( $_FILES['template-contactform-file'] ) && $_FILES['template-contactform-file']['error'] == UPLOAD_ERR_OK ) {
  70.                     $mail->IsHTML(true);
  71.                     $mail->AddAttachment( $_FILES['template-contactform-file']['tmp_name'], $_FILES['template-contactform-file']['name'] );
  72.                 }
  73.    
  74.                 // Runs only when reCaptcha is present in the Contact Form
  75.                 if( isset( $_POST['g-recaptcha-response'] ) ) {
  76.                     $recaptcha_response = $_POST['g-recaptcha-response'];
  77.                     $response = file_get_contents( "https://www.google.com/recaptcha/api/siteverify?secret=" . $recaptcha_secret . "&response=" . $recaptcha_response );
  78.    
  79.                     $g_response = json_decode( $response );
  80.    
  81.                     if ( $g_response->success !== true ) {
  82.                         echo '{ "alert": "error", "message": "Captcha not Validated! Please Try Again." }';
  83.                         die;
  84.                     }
  85.                 }
  86.    
  87.                 $mail->MsgHTML( $body );
  88.                 $sendEmail = $mail->Send();
  89.    
  90.                 if( $sendEmail == true ):
  91.                     echo '{ "alert": "success", "message": "' . $message_success . '" }';
  92.                 else:
  93.                     echo '{ "alert": "error", "message": "Email <strong>could not</strong> be sent due to some Unexpected Error. Please Try Again later.<br /><br /><strong>Reason:</strong><br />' . $mail->ErrorInfo . '" }';
  94.                 endif;
  95.             } else {
  96.                 echo '{ "alert": "error", "message": "Bot <strong>Detected</strong>.! Clean yourself Botster.!" }';
  97.             }
  98.         } else {
  99.             echo '{ "alert": "error", "message": "Please <strong>Fill up</strong> all the Fields and Try Again." }';
  100.         }
  101.     } else {
  102.         echo '{ "alert": "error", "message": "An <strong>unexpected error</strong> occured. Please Try Again later." }';
  103.     }
  104.    
  105.     ?>
  106.  
  107. Which calls this `PHPMailerAutoload.php` file:
  108.  
  109.     <?php
  110.     /**
  111.      * PHPMailer SPL autoloader.
  112.      * PHP Version 5
  113.      * @package PHPMailer
  114.      * @link https://github.com/PHPMailer/PHPMailer/ The PHPMailer GitHub project
  115.      * @author Marcus Bointon (Synchro/coolbru) <phpmailer@synchromedia.co.uk>
  116.      * @author Jim Jagielski (jimjag) <jimjag@gmail.com>
  117.      * @author Andy Prevost (codeworxtech) <codeworxtech@users.sourceforge.net>
  118.      * @author Brent R. Matzelle (original founder)
  119.      * @copyright 2012 - 2014 Marcus Bointon
  120.      * @copyright 2010 - 2012 Jim Jagielski
  121.      * @copyright 2004 - 2009 Andy Prevost
  122.      * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  123.      * @note This program is distributed in the hope that it will be useful - WITHOUT
  124.      * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  125.      * FITNESS FOR A PARTICULAR PURPOSE.
  126.      */
  127.    
  128.     /**
  129.      * PHPMailer SPL autoloader.
  130.      * @param string $classname The name of the class to load
  131.      */
  132.     function PHPMailerAutoload($classname)
  133.     {
  134.         //Can't use __DIR__ as it's only in PHP 5.3+
  135.         $filename = dirname(__FILE__).DIRECTORY_SEPARATOR.'class.'.strtolower($classname).'.php';
  136.         if (is_readable($filename)) {
  137.             require $filename;
  138.         }
  139.     }
  140.    
  141.     if (version_compare(PHP_VERSION, '5.1.2', '>=')) {
  142.         //SPL autoloading was introduced in PHP 5.1.2
  143.         if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
  144.             spl_autoload_register('PHPMailerAutoload', true, true);
  145.         } else {
  146.             spl_autoload_register('PHPMailerAutoload');
  147.         }
  148.     } else {
  149.         /**
  150.          * Fall back to traditional autoload for old PHP versions
  151.          * @param string $classname The name of the class to load
  152.          */
  153.         function __autoload($classname)
  154.         {
  155.             PHPMailerAutoload($classname);
  156.         }
  157.     }
  158.  
  159. Gives me a SMTP connection failed error
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top