Bugs with SMTP and PHPMailer

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • talk2tim
    New Member
    • Feb 2015
    • 19

    #1

    Bugs with SMTP and PHPMailer

    Hello Folks. I want to first appreciate for all the help that i have received here. I am pretty new to PHP and im trying to use PHP mailer for my SMYP settings. With the settings below, i tried to send the mail, but after a long time of loading, it just stops with a blank page. I set up and error report, that would let me know whether the message was sent or not, but i got no error message, just blank screen. I think its the STMP settings. Im supposed to put my ISP's Username and Password here.

    Code:
        $mail->IsSMTP();
        $mail->Host     = "mail.example.com";
        $mail->Port     = 25;
        $mail->SMTPAuth = false;
        $mail->Username = "yourname@example.com";
        $mail->Password = "yourpassword";
    I originally put my Gmail username and password up there.

    This is the Full Code

    Code:
    <?php
        
    
        require_once("../zoomie_files/includes/PHPmailer/PHPMailerAutoload.php");
    
    
        $to_name = "Test Mail";
        $to = "timothyazubuikeh@gmail.com";
        $subject = "Mail Test at ".strftime("%T", time());//Provides the time
        $message ="This is a Test. Ignore the inbox";
        $message = wordwrap($message, 70);
        $from_name = "Timothy Azubuikeh";
        $from = "admin@zoomie.com";
    
    
        //PHP SMTP version default
        $mail = new PHPMailer;
    
        $mail->IsSMTP();
        $mail->Host     = "gmail.com";
        $mail->Port     = 25;
        $mail->SMTPAuth = false;
        $mail->Username = "my_personal_email@gmail.com";
        $mail->Password = "my_password";
    
        $mail->FromName = $from_name;
        $mail->From     = $from;
        $mail->AddAddress($to, $to_name);
        $mail->Subject  = $subject;
        $mail->Body     = $message;
    
    
        if (!$mail->send())
            {
                echo "Mailer Error: " . $mail->ErrorInfo;
            }
        else 
            {
               echo "Message sent!";
            }
    
      
    ?>
    Please i need help with the SMTP settings or whatever the issue with the code is. Thanks
    Last edited by talk2tim; Apr 6 '15, 02:32 PM. Reason: [CODE][/CODE]
  • Luuk
    Recognized Expert Top Contributor
    • Mar 2012
    • 1043

    #2
    RTFM (read the fine manual)

    it has:
    Code:
    $mail->SMTPAuth   = true;
    it can be found here:
    Complete, working PHPMailer 6.x examples for every transport: basic and advanced mail(), sendmail, authenticated SMTP, unauthenticated relay, Gmail App Password and XOAUTH2, plus keep-alive batching.


    oh, and use the toolbar to insert '[CODE/]' tags....
    Last edited by Luuk; Apr 6 '15, 01:34 PM. Reason: something about '[CODE/]' tags

    Comment

    • talk2tim
      New Member
      • Feb 2015
      • 19

      #3
      Thanks Luuk, before i try it out, do you mean that the email and password info i put there were correct?? Thanks.

      Comment

      • talk2tim
        New Member
        • Feb 2015
        • 19

        #4
        Thanks Luuk. I change the false to true in
        Code:
        $mail->SMTPAuth   = true;
        . It still did the same thing it did when SMTPAuth was false. (loaded for a long time and came up with a blank page).

        What do you think is the problem???

        Comment

        • Luuk
          Recognized Expert Top Contributor
          • Mar 2012
          • 1043

          #5
          The problem should be noted somewhere in your logfiles.....

          If I want to sent an email from PHP I would use:


          And not a library which is more than 4 years old, meant to send mail by PHP, with a now-working sample form

          But documentation is here:

          Comment

          • talk2tim
            New Member
            • Feb 2015
            • 19

            #6
            Thanks Luuk. The article you gave me is about just about the PHP mail() function.

            When you talk about log files and 'library which is more than 4 years old', honestly, i dont really understand would be grateful if you explained better. Im a novice.


            I ran this code

            Code:
            <?php 
            
            $inis = Array('SMTP', 'smtp_port', 'sendmail_path');// from not important 
            foreach($inis as $key=>$val){ 
              echo "\t<p>{$val}</p>\n<code>\n"; 
              var_dump(ini_get($val)); 
              echo "</code>\n"; 
            };
            And its output was

            SMTP

            string(9) "localhost"
            smtp_port

            string(2) "25"
            sendmail_path

            bool(false)

            I also ran this code to test the STMP class to see whether a connection can be made to the server

            Code:
            date_default_timezone_set('Etc/UTC'); 
            
            require ("../zoomie_files/includes/PHPmailer/PHPMailerAutoload.php"); 
            
            //Create a new SMTP instance 
            $smtp = new SMTP; 
            
            //Enable connection-level debug output 
            $smtp->do_debug = SMTP::DEBUG_CONNECTION; 
            
            try { 
            //Connect to an SMTP server 
                if ($smtp->connect('www.gmail.com', 25)) { 
                    //Say hello 
                    if ($smtp->hello('localhost')) { //Put your host name in here 
                        //Authenticate 
                        if ($smtp->authenticate('my_personal_email@gmail.com', 'my_pasword')) { 
                            echo "Connected ok!"; 
                        } else { 
                            throw new Exception('Authentication failed: ' . $smtp->getLastReply()); 
                        } 
                    } else { 
                        throw new Exception('HELO failed: '. $smtp->getLastReply()); 
                    } 
                } else { 
                    throw new Exception('Connect failed'); 
                } 
            } catch (Exception $e) { 
                echo 'SMTP error: '. $e->getMessage(), "\n"; 
            } 
            //Whatever happened, close the connection. 
            $smtp->quit(true);
            And its output was this.

            2015-04-07 20:34:50 Connection: opening to www.gmail.com:25, t=30, opt=array ( ) 2015-04-07 20:34:50 Connection: opened

            But the script still does as before,(blank page). Hasnt a connection to a server been made? What do you think is the problem? Thanks Luuk for your help. Thanks.

            Comment

            • Luuk
              Recognized Expert Top Contributor
              • Mar 2012
              • 1043

              #7
              I cannot find the file you included on line#3 ("PHPMailerAuto load.php"), I does not exist in PHPMailer, at least not in the version I downloaded from: http://sourceforge.net/projects/phpmailer/

              Do I have an old, or wrong, version?


              What I mean about 'more than 4 years old' is the dates on the site: http://www.worxware.com/kbn/?category_id=23

              Back to the topic, you seem to have the need to send email from PHP.
              1) What kind of email do you want to send?
              a) just text
              b) HTML
              c) text + attachment
              d) HTML + attachment

              The basic PHP mail function is able to do option a) very easy, and every next option is only slightly more complicated.
              Last edited by Luuk; Apr 8 '15, 06:40 PM. Reason: BTW on your line #13, you try to connect to www.gmail.com on port 25, this is not possible (from where I live)

              Comment

              • talk2tim
                New Member
                • Feb 2015
                • 19

                #8
                Thanks Luuk, been quite a while, i have been really busy. I opted against using PHPmailer because i heard that it is always meddled with complications. A friend suggested another third party email system. sendgrid.com . I signed up,looked up the documentation and looked at their integration with Open Source apps(PHP in this case). I worked with this code

                Code:
                require_once("../zoomie_files/includes/sendgrid/smtpapi-php.php");
                 
                 use Smtpapi\Header;
                 
                $transport = \Swift_SmtpTransport::newInstance('smtp.sendgrid.net', 587);
                $transport->setUsername('sendgrid_username');
                $transport->setPassword('sendgrid_password');
                 
                $mailer = \Swift_Mailer::newInstance($transport);
                 
                $message = new \Swift_Message();
                $message->setTos(array('send_to@gmail.com'));
                $message->setFrom('sent_from@gmail.com');
                $message->setSubject('Hello');
                $message->setBody('%how% are you doing?');
                 
                $header = new Header();
                $header->addSubstitution('%how%', array('Owl'));
                 
                $message_headers = $message->getHeaders();
                $message_headers->addTextHeader(HEADER::NAME, $header->jsonString());
                 
                try {
                    $response = $mailer->send($message);
                    print_r($response);
                } catch(\Swift_TransportException $e) {
                    print_r('Bad username / password');
                }

                It gave me some errors wherever the "\" appeared. That is not a filepath seperator ryt? For windows, its "/". Its triggering errors. Please what does the "\" mean?

                This is the first error statement

                "Parse error: syntax error, unexpected T_STRING, expecting T_CONSTANT_ENCA PSED_STRING or '(' in C:\wamp\www\btb _sandbox\sendem ail7.php on line 7"

                In

                Code:
                use Smtpapi\Header; //There is actually a php file titled 'Header' in a folder named 'Smtpapi'.
                //Is this an attempt to locate the file 'Header'?
                These are other cases where the "\" appeared. 
                *** $transport = \Swift_SmtpTransport::newInstance('smtp.sendgrid.net', 587);
                **  $mailer = \Swift_Mailer::newInstance($transport);
                *   $message = new \Swift_Message();

                What do you think is wrong? What is the '\' sign for? Thanks for your help, i really appreciate. Thanks

                Comment

                • Luuk
                  Recognized Expert Top Contributor
                  • Mar 2012
                  • 1043

                  #9
                  The '\' is for something with namespaces see:
                  Using namespaces: fallback to the global space for functions and constants


                  My knowledge about that is limited too ;)
                  (but Google is my friend....)

                  You need PHP version >= 5.3.0 for this to work.

                  Comment

                  • talk2tim
                    New Member
                    • Feb 2015
                    • 19

                    #10
                    Thanks Luuk. I upgraded to 5.5 and its giving me this error.

                    Fatal error: Class 'Swift_SmtpTran sport' not found in C:\wamp\www\btb _sandbox\sendem ail8.php on line 8

                    What do you think is the problem this time around? Thanks Luuk, you have been helpful.

                    Comment

                    Working...