PHPMailer library SMTP class blocked

php

#1

Hello everybody. Trying to make my script send mail messages via Gmail using PHPMailer, but it fails everytime with an error “SMTP connect() failed.” PHPMailer troubleshooting wiki on Github says on this case: “This is often reported as a PHPMailer problem, but it’s almost always down to local DNS failure, firewall blocking or other issue on your local network. It means that PHPMailer is unable to contact the SMTP server you have specified in the Host property, but doesn’t say exactly why.”

Here is the code fragment regarding PHPMailer:

require_once("/path/to/PHPMailer/PHPMailerAutoload.php");
            
            // instantiate mailer
            $mail = new PHPMailer();
            $mail->IsSMTP();
            
            //required for gmail
            $mail->SMTPAuth   = true;
            $mail->Host = "smtp.gmail.com";
            
            // login to gmail
            $mail->Username   = "username";
            
            // password to gmail
            $mail->Password   = "password";
            
            //required for gmail
            $mail->SMTPSecure = "tls"; 
            $mail->Port = 587;
            
            // set From:
            $mail->SetFrom("from@email");
            
            // set To:
            $mail->AddAddress($realmail);
            // set Subject:
            $mail->Subject = "Subject";
            // set body
            $mail->Body = "Text";
                           
            // set alternative body, in case user's mail client doesn't support HTML
            $mail->AltBody = "AltText";
            
            $mail->SMTPdebug = 4;
            
            // send mail
            if ($mail->Send() === false)
                die($mail->ErrorInfo . "\n");

#2

I don’t think it is possible to access PHP native mailing in C9 - the way I got around it was using a third party smtp provider - I used mandrill (unfortunately it is not free anymore…). There are others I think, it is a lot of messing around and I wish C9 would make it easier somehow…


#3

Yes, you’re right (( The only way I managed to send e-mail messages is posting directly to SendGrid API with curl():

$mail = curl_init("https://api.sendgrid.com/api/mail.send.json");

$message = "<html> 
                                <body>
                                    Message text here
                                </body>                            
                            </html>";
                            
                $params = array(
                    'api_user'  => "SendGrid username",
                    'api_key'   => "SendGrid password",
                    'to'        => $realmail,
                    'toname'    => $username,
                    'subject'   => "Subject",
                    'html'      => $message,
                    'fromname'  => 'FromName',
                    'replyto'   => "e-mail@addre.ss",
                    'from'      => "e-mail@addre.ss",
                );
                
                curl_setopt($mail, CURLOPT_POST, true);
                curl_setopt($mail, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($mail, CURLOPT_POSTFIELDS, $params);
                curl_setopt($mail, CURLOPT_SSLVERSION, 'CURL_SSLVERSION_TLSv1_2');
                
                $response = curl_exec($mail);

    if (!$response) 
        echo "Error: " . curl_error($mail);

    curl_close($mail);