hetal gohel
hetal gohel

Reputation: 355

smtp email with pear library is not working gives error

my code is as below,

include('../include/pear/Mail.php');
include('../include/pear/Mail/mime.php');



$from = "[email protected]";
    $to = "[email protected]"; 
    $subject = 'Weekly Summary';
    $headers = array('From' => $from, 'To' => $to, 'Subject' => $subject);
    $html = '<html><body>Hello,<br/>Please find attached file</body></html>';
    $file = $filepath.$filename;
    $crlf = "\n";
    $mime = new Mail_mime($crlf);
    $mime->setHTMLBody($html);
    $mime->addAttachment($file, 'application/octet-stream');
    $body = $mime->get();
    $headers = $mime->headers($headers);
    $host = "smtp.gmail.com";
    $username = "[email protected]";
    $password = "password";
    $port = 465;
    $smtp = Mail::factory('smtp', array(
                'host' => $host,
                'auth' => false,
                'port' => $port,
                'username' => $username,
                'password' => $password,
                'timeout' => 20,
                'debug' => false,
                'persist' => true));
    $mail = $smtp->send($to, $headers, $body);
    if (PEAR::isError($mail)) {
        echo("<p>" . $mail->getMessage() . "</p>");
    } else {
        echo("<p>Message successfully sent!</p>");
    }
    echo "completed";
    die;

I have also try port 587 but it always gives me below error,

Failed to connect to smtp.gmail.com:465 [SMTP: Invalid response code received from server (code: -1, response: )]

if I remove 'timeout' => 20 from above code it gives me error

Failed to connect to smtp.gmail.com:465 [SMTP: Failed to connect socket: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. (code: -1, response: )]

Please help me to resolve above issue

Upvotes: 0

Views: 231

Answers (1)

Gaterde
Gaterde

Reputation: 819

https://support.google.com/accounts/answer/6010255?hl=en

You probably have to allow "Less Secure Apps" on the used google account.

Upvotes: 1

Related Questions