PHPMailer Failed to connect to server - Settings are correct - php

I am testing SMTP on my server, using PHPMailer.
SMTP server works fine, on my VBulletin forums, but on my own website engine (from localhost, but port 80 is open, so I am doing it via my ip) I get the following errors:
2014-04-14 12:14:18 SMTP ERROR: Failed to connect to server: SMTP connect() failed.
I asked my mate, whom is the manager of the server, he says that there are no connections that are coming from my IP.
My PHPMailer settings:
public function create() {
$mailer = new PHPMailer();
$mailer->isSMTP();
$mailer->CharSet = "UTF-8";
$mailer->SMTPSecure = "tls";
$mailer->host = Config::$SMTP_HOST;
$mailer->SMTPDebug = 2;
$mailer->SMTPAuth = true;
$mailer->Port = Config::$SMTP_PORT;
$mailer->Username = Config::$SMTP_USER;
$mailer->Password = Config::$SMTP_PASS;
return $mailer;
}
// SMTP details
public static $SMTP_PORT = 25;
public static $SMTP_HOST = "argonite.net";
public static $SMTP_USER = "recover#argonite.net";
public static $SMTP_PASS = "password";
public static $RECOVER_EMAIL = "recover#argonite.net";
And this is how I use it:
$mailer = (new MailFactory())->create();
$mailer->addAddress($email, $username);
$mailer->setFrom(Config::$RECOVER_EMAIL, "Recover");
$mailer->Subject = "Password Recovery - DO NOT REPLY";
$mailer->AltBody = "Argonite has sent you your password as requested.";
$mailer->msgHTML($this->buildMessage($username, $this->getPassword($username, $email)));
if (!$mailer->send()) {
$fh = fopen("logs/.mailer", 'a');
fwrite($fh, "\n[" . date("Y-m-d H:i:s") . "]: " . $mailer->ErrorInfo . "\n");
fclose($fh);
return "An error has occured while sending your request. Administrators have been notified.";
}
else {
return "success";
}
Why is it failing? Could it be something with my PHPMailer settings, version or my Apache/php configurations?
php: 5.5.9

Fixed - I don't really know how but that's the settings I set:
$mailer->SMTPSecure = "none";
$mailer->host = Config::$SMTP_HOST;
$mailer->SMTPDebug = 0;
$mailer->SMTPAuth = false;
$mailer->Port = Config::$SMTP_PORT;
$mailer->Username = Config::$SMTP_USER;
$mailer->Password = Config::$SMTP_PASS;
After adding secure = "none" and auth = false, and port 25, it worked.

Related

CodeIgniter email throws error

I cannot send mail using CodeIgniter. It displays the following error.
fwrite(): send of 28 bytes failed with errno=10054 An existing connection was forcibly closed by the remote host.
This my settings in email.php library
public $useragent = 'CodeIgniter';
public $mailpath = '/usr/sbin/sendmail'; // Sendmail path
public $protocol = 'smtp'; // mail/sendmail/smtp
public $smtp_host = 'smtp.mailhostbox.com';
public $smtp_user = 'xxx#xx.in';
public $smtp_pass = 'xxxxxx';
public $smtp_port = 25;
public $smtp_timeout = 5;
public $smtp_keepalive = FALSE;
public $smtp_crypto = '';
public $newline = "\r\n";
I could send email till yesterday. But from today morning, it shows this error.
UPDATE :
I have changed my host to gmail , but still not working. The following are the changes I made
public $useragent = 'CodeIgniter';
public $mailpath = '/usr/sbin/sendmail'; // Sendmail path
public $protocol = 'smtp'; // mail/sendmail/smtp
public $smtp_host = 'smtp.gmail.com';
public $smtp_user = 'noreply.xxx#gmail.com';
public $smtp_pass = 'xxxxxx';
public $smtp_port = 465;
public $smtp_timeout = 5;
public $smtp_keepalive = FALSE;
public $smtp_crypto = 'ssl';
public $newline = "\r\n";
But it shows error as
Message: fsockopen(): unable to connect to ssl://smtp.gmail.com:465:465 (Failed to parse address "smtp.gmail.com:465:465")
I changed the port from 465 to 587 as follows
public $smtp_port = 587;
public $smtp_crypto = 'tls';
But I got this error
Message: stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed
I couldn't understand what is the real issue, because it throws errors in every possible conditions
You need to initialise the config.see here
For Example
function send_email($attributes) {
$this->load->library('email');
$this->email->set_newline("\r\n");
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'host';
$config['smtp_port'] = '465';
$config['smtp_user'] = 'user#smtp.com';
$config['smtp_from_name'] = 'FROM NAME';
$config['smtp_pass'] = 'XXX';
$config['wordwrap'] = TRUE;
$config['newline'] = "\r\n";
$config['mailtype'] = 'html';
$this->email->initialize($config);
$this->email->from($config['smtp_user'], $config['smtp_from_name']);
$this->email->to($attributes['to']);
$this->email->cc($attributes['cc']);
$this->email->bcc($attributes['cc']);
$this->email->subject($attributes['subject']);
$this->email->message($attributes['message']);
if($this->email->send()) {
return true;
} else {
return false;
}
}

SMTP connect() failed phpMailer [out-of-date CA certificate issue]?

I've been using phpMailer to send emails until few days ago, and it stopped working surprisingly.
I'm using GMAIL SMTP server as a host.
Here's what the Exception and debugging message shows in console :
2017-12-19 05:39:02 Connection: opening to ssl://smtp.gmail.com:465, t=10, opt=array (
)
2017-12-19 05:39:02 SMTP ERROR: Failed to connect to server: (0)
2017-12-19 05:39:02 SMTP connect() failed.
And here's my mail functionality:
public static function mailTo($recipients)
{
$f3 = \Base::instance();
$edit = $f3->get('editTrue');
$user = AclHelper::getCurrentUser();
$template= new \Template;
if(isset($edit))
{
$mailBody = $template->render('leave/requestEdit.html');
}
else
{
$mailBody= $template->render('leave/emailTemp.html');
}
// When true, PHPMailer returns exceptions
$mail = new PHPMailer(true);
try {
$mail->isSMTP(); // Set mailer to use SMTP
$mail->isHTML(true);
$mail->addAddress($user['email']);
$mail->addAddress("malakar.rakesh1993#gmail.com");
// foreach($recipients as $recipient){
// $mail->addCC($recipient);
// }
$mail->SMTPDebug = 4;
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->Username = "malakar.rakesh1993#gmail.com";
$mail->Password = "abc123";
// $mail->Host = $f3->get('GBD.smtp.host'); // Specify main and backup SMTP servers
$mail->setFrom($user['email']);
$userFullName = trim(ucfirst($user['firstname'])) . " " . trim(ucfirst($user['lastname']));
$mail->FromName = $userFullName;
$mail->Body = $f3->get('message');
$mail->Body .="<br>". $mailBody;
if(isset($edit))
{
$mail->AltBody = '';
}
else
{
$mail->AltBody = 'Hello.. Not working';
}
$mail->Subject = 'Updates on leave date applied';
$mailStatus = (boolean)$mail->send();
if ($mailStatus === true)
{
return $mail;
}
}
catch (phpmailerException $e)
{
$response = array(
'status'=>'error',
'message'=>'Got some error while sending emails',
'exceptions'=>$e->getMessage()
);
return $response;
}
catch (Exception $e) {
$response = array(
'status'=>'error',
'message'=>'Got some error while sending emails',
'exceptions'=>$e->getMessage()
);
return $response;
}
}
I also tried using tls as encryption protocol and 587 port, but gives 500 internal server error.
The same code is running in development version in server, but unfortunately not in my localhost.
I was suggested of out-dated CA certificate in my system, and I tried renewing the up-to-date CA certificate following the link Trobuleshooting, but it's not working either.
Stupid maybe but I tried pinging to smtp.gmail.com [without ssl and port] in my command prompt and I'm receiving feedback.
Could it be that Google limits email sending? But I can still access my account.
I have followed almost every articles and solutions available in the Internet.
Please help me through this...this is driving me crazy.
Any help is very much appreciated. Thanks...

phpmailer returning response null in slim

I am using slim framework to create api's for android app.In CreateUser api, i am sending mail to user using phpmailer.Phpmailer is sending the mails to user perfectly but my array response is returning null array response due to it.When i remove the code of phpmailer response is correct and when i add it ,it generates null reponse.
Here is my code in Dbhandler.php:
$app->post('/requestPreview', function() use ($app) {
verifyRequiredParams(array('fname', 'lname', 'email','event_code','app_version'));
global $user_id;
$response = array();
$fname = urldecode($app->request->post('fname'));
$lname = urldecode($app->request->post('lname'));
$email = urldecode($app->request->post('email'));
$event_code = urldecode($app->request->post('event_code'));
$app_version = urldecode($app->request->post('app_version'));
$db = new DbHandler();
//$db->sendmail($email);
$res = $db->signup($fname, $lname, $email,$event_code,$app_version);
if ($res['status']=="one") {
//$password=$res['password'];
$response["event_code"] = $event_code;
$response["email"] = $email;
$response["fname"] =$fname;
$response["lname"] = $lname;
$response["CreateUser"] = true;
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->Username = "abc.abc#gmail.com";
$mail->Password = "***********";
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->From = "abc.abc#gmail.com";
$mail->AddAddress($email);
$mail->isHTML(true);
$mail->Subject = "Welcome to www.abc.com .Your Account has been created and your password is:";
$mail->Body = "Welcome to www.abc.com .Your Account has been created and your password is:";
$mail->send();
} else if ($res['status']=="two"){
$response["error"] ="already_in_system";
$response["fname"] = null;
$response["lname"] = null;
}
echoRespnse(200, $response);
});
Update to latest PHPMailer and base your code on an up to date example, then read the troubleshooting docs. It's very likely you're running into Google auth issues.
Your PHPMailer code never touches $response, suggesting it may be exiting prematurely due to an error, so trace what's happening in your function.

PHPMailer fails to send

I see most PHPMailer questions use IsSMTP(), but I'm working from a very basic example I found yesterday on SO (can't seem to find the link in History though) that doesn't.
require_once("class.phpmailer.php");
$email = new PHPMailer();
if (isset($_FILES['upload']['size']))
{
echo 'file size: '.(basename($_FILES['upload']['size'])* 1024).'<br />';
if (move_uploaded_file($_FILES['upload']['tmp_name'], $target_path))
{
echo "The file ". basename( $_FILES['upload']['name'])." has been uploaded<br />";
$email->AddAttachment($target_path);
}
else
{
echo "There was an error uploading the file, please try again!<br /> ".basename($_FILES['upload']['error']);
}
}
else
{
echo "No file was found for the upload.<br />";
}
$email->To = "me#this.com";
$email->From = $email_from;
$email->FromName = $first_name;
$email->Subject = "Query from website";
$email->Body = $email_message;
//echo "mail built...<br />";
if (!$email->Send())
{
echo "Mailer error: " . $email->ErrorInfo;
}
I've tried sending twice. The code executes fine and the last if statement returns true.
Do I have to use IsSMTP()? If so, does it have to route through SSL like I've seen suggested here?
Thanks in advance?
EDIT
I've added details required for SMTP as follows:
$email->IsSMTP();
$email->Host = "localhost";
$email->Port = 465;
$email->SMTPAuth = true;
$email->Username = "bacon";
$email->Password = "4ndCh33se";
This doesn't seem to have triggered it to send. There also still isn't any error reported.
EDIT
Turns out there was an error, I had a redirect in a weird place that was preventing me from seeing it. The error is as follows:
You must provide at least one mailer is not supported.
Initially, the addition of the IsSMTP() seemed vital and was implemented as per other answers.
All answers failed, however, to address one missing piece of the puzzle.
PHPMailer requires at least 1 address to be added as per below. I just used the same address here as was set in the To property:
$email->To = "me#this.com";
$email->AddAddress("me#this.com");
Once this was done, all worked perfectly fine.
if you want to send mail through smtp you need to set SMTP username and password and other setting.
And also you need to add IsSMTP().
Example Gmail SMTP setup
$this->SwiftMailer->smtpType = 'tls';
$this->SwiftMailer->smtpHost = 'smtp.gmail.com';
$this->SwiftMailer->smtpPort = 587;
$this->SwiftMailer->smtpUsername = 'emailaddress#gmail.com';
$this->SwiftMailer->smtpPassword = 'gmailPassword';
$this->SwiftMailer->sendAs = 'html';
$this->SwiftMailer->from = 'sender#gmail.com';
$this->SwiftMailer->fromName = 'Sender Name';
$this->SwiftMailer->to = "receiver#anything.com";
$this->Email->sendAs = 'html';
$email->To = "me#this.com";
$email->From = $email_from;
$email->FromName = $first_name;
$email->Subject = "Query from website";
$email->Body = $email_message;
I have the following besides yours. Maybe it helps:
$email->IsSMTP(); // set mailer to use SMTP
$email->Host = "mail.webaddress.com"; // specify main and backup server
$email->SMTPAuth = true; // turn on SMTP authentication
$email->Username = "********"; // SMTP username
$email->Password = "********"; // SMTP password
I have changed nothing in class.phpmailer.php
Try this
$email->IsSMTP(); // set mailer to use SMTP
$email->Host = "mail.webaddress.com"; // specify main and backup server
$email->SMTPAuth = false; // turn on SMTP authentication
$email->Port = 25
$email->SMTPSecure = "";
$email->Username = "********"; // SMTP username
$email->Password = "********"; // SMTP password

Send emails from ubuntu virtual machine and use catchmail: Could not execute: smtp://localhost:1025

I am using ubuntu on my virtual machine. I would like to send emails using catchmail as described here: http://berk.es/2011/05/29/mailcatcher-for-drupal-and-other-php-applications-the-simple-version/
I am trying to send emails like that:
//Mailer class:
class Mailer extends PHPMailer
{
public $UTF8Encode = false;
public function __construct($param = null)
{
parent::__construct($param);
$this->Mailer = 'sendmail';
$this->Sendmail = 'smtp://localhost:1025';
$this->From = 'xxxx#xxxx.com';
$this->FromName = 'Support';
$this->WordWrap = 50;
$this->CharSet = 'UTF-8';
}
}
....etc....
And:
//Sending emails
$mail = new Mailer();
$mail->Body = "xxxx";
$mail->Subject = "xxx";
$mail->From = 'xxxx#xxxx.org';
$mail->FromName = 'Support';
$mail->WordWrap = 50;
$mail->AddAddress(xxxx#xxxx.com);
And I am getting the error:
Could not execute: smtp://localhost:1025
$this->Mailer = 'sendmail';
$this->Sendmail = 'smtp://localhost:1025';
The problem with this is you're telling PHPMailer to use a command line program called sendmail instead of using smtp. And PHPMailer tries to do something like:
exec("smtp://localhost:1025 --args-and-stuff");
And as you can tell this won't work.
To tell PHPMailer to use smtp you need to do the following:
$this->Mailer = 'smtp';
$this->Host = 'localhost';
$this->Port = 1025;
If your SMTP server needs Authentification you can do that as follows:
$mail->SMTPAuth = true;
$mail->Username = "yourname#yourdomain";
$mail->Password = "yourpassword";

Categories