Gmail & SwiftMailer Fatal Error - php

I've looked everywhere and cannot find a solution for this.
I just put up a website using GoDaddy hosting. The website has a form, which uses Gmail SMTP and was working perfectly when I was running a virtual host with XAMPP. After hosting with GoDaddy however, I get the following error when I try to submit:
Fatal error: Uncaught exception 'Swift_TransportException' with message 'Connection could not be established with host smtp.gmail.com [Connection refused #111]' in /home/modernautoinc/public_html/Swift/lib/classes/Swift/Transport/StreamBuffer.php:259 Stack trace: #0 /home/modernautoinc/public_html/Swift/lib/classes/Swift/Transport/StreamBuffer.php(64): Swift_Transport_StreamBuffer->_establishSocketConnection() #1 /home/modernautoinc/public_html/Swift/lib/classes/Swift/Transport/AbstractSmtpTransport.php(115): Swift_Transport_StreamBuffer->initialize(Array) #2 /home/modernautoinc/public_html/Swift/lib/classes/Swift/Mailer.php(80): Swift_Transport_AbstractSmtpTransport->start() #3 /home/modernautoinc/public_html/mech.php(24): Swift_Mailer->send(Object(Swift_Message)) #4 {main} thrown in /home/modernautoinc/public_html/Swift/lib/classes/Swift/Transport/StreamBuffer.php on line 259
Here is my php:
<?php
require_once 'Swift/lib/swift_required.php';
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$phone = $_REQUEST['phone'];
$make = $_REQUEST['make'];
$model = $_REQUEST['model'];
$VIN = $_REQUEST['VIN'];
$problem = $_REQUEST['problem'];
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, "ssl")
->setUsername('example#gmail.com')
->setPassword('example');
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance('Appointment: Mechanical')
->setFrom(array($email => $name))
->setTo(array('example#gmail.com' => 'MA'))
->setBody("From: ".$email."\nName: ".$name."\nPhone Number: ".$phone."\nMake: ".$make."\nModel: ".$model."\nVIN: ".$VIN."\nDescription of Problem: ".$problem);
$mailer->send($message);
header('Location: http://www.google.ca');
?>
Any help would be greatly appreciated. Thank you for your time.

For GoDaddy Hosting, I've configured my Silex app like this :
$app['swiftmailer.options'] = array(
'host' => 'p3plcpnl0689.prod.phx3.secureserver.net',
'port' => 465,
'username' => 'user#host.com',
'password' => 'your pass',
'encryption' => 'ssl'
);
and it works like a charm!!

Related

What is the best php option to connect to outlook imap with oAuth?

What would be the best option to connect to an outlook email account with PHP?
I´m not sure how to declare my credentials with oAuth, should I use my e-mail as username or the client Id key as username? Should I use client secret as password?
I've been trying the following solutions with no success:
https://github.com/javanile/php-imap2
include "inc/php-imap2/vendor/autoload.php";
// Start connection
$mailbox = "{smtp.office365.com:993/imap/ssl/novalidate-cert}";
$token = base64_encode("user=" . $email . "\1auth=Bearer " . $passw . "\1\1");
$imap = #imap2_open($mailbox, $email, $token, OP_XOAUTH2);
// List all mailboxes
if ($imap) {
$mailboxes = imap2_getmailboxes($imap, $mailbox, '*');
var_dump($mailboxes);
} else {
$error = imap2_last_error();
echo $error;
}
Return
Can not authenticate to IMAP server: A0001 NO AUTHENTICATE failed.
https://github.com/Webklex/php-imap
include "inc/php-imap/vendor/autoload.php";
use Webklex\PHPIMAP\ClientManager;
use Webklex\PHPIMAP\Client;
$cm = new ClientManager($options = []);
$client = $cm->make([
'host' => "smtp.office365.com",
'port' => 993,
'encryption' => 'ssl',
'validate_cert' => true,
'username' => $email,
'password' => $passw,
'protocol' => 'imap',
'authentication' => 'oauth',
]);
//Connect to the IMAP Server
$client->connect();
Return
got failure response: NO AUTHENTICATE failed.
PHP Fatal error: Uncaught Webklex\PHPIMAP\Exceptions\AuthFailedException in C:\inetpub\wwwroot\inc\php-imap\src\Client.php:385
Stack trace:
#0 C:\inetpub\wwwroot\inc\php-imap\src\Client.php(371): Webklex\PHPIMAP\Client->authenticate()
#1 C:\inetpub\wwwroot\test.php(57): Webklex\PHPIMAP\Client->connect()
#2 {main}
Next Webklex\PHPIMAP\Exceptions\ConnectionFailedException: connection setup failed in C:\inetpub\wwwroot\inc\php-imap\src\Client.php:391
Stack trace:
#0 C:\inetpub\wwwroot\inc\php-imap\src\Client.php(371): Webklex\PHPIMAP\Client->authenticate()
#1 C:\inetpub\wwwroot\test.php(57): Webklex\PHPIMAP\Client->connect()
#2 {main}
thrown in C:\inetpub\wwwroot\inc\php-imap\src\Client.php on line 391
Any help is appreciated

Can't Send PHPMailer with Oauth Google

I have some problem.
My code
<?php
require 'PHPMailer/PHPMailerAutoload.php';
$userEmail = "mail#mail.org";
$clientId = "xxxxxxxxx-xxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com";
$clientSecret = "xxxxxxxx-xxxxxxxxx";
$token = "1/xxxxxxxx-xxxxxxxx-5EmWcSmuvnRbJs";
$mailer = new PHPMailerOAuth;
$mailer->isSMTP();
$mailer->Host = 'smtp.gmail.com';
$mailer->SMTPAuth = true;
$mailer->AuthType = 'XOAUTH2';
$mailer->oauthUserEmail = $userEmail;
$mailer->oauthClientId = $clientId;
$mailer->oauthClientSecret = $clientSecret;
$mailer->oauthRefreshToken = $token;
$mailer->SMTPSecure = 'tls';
$mailer->Port = 587;
$mailer->setFrom("xxxxx#xxxxxxx.org");
$mailer->addAddress("xxxxxx#gmail.com");
$mailer->Subject = "Subject";
$mailer->Body = "Message";
if (! $mailer->send())
throw new RuntimeException('Mail submission failed! ' . $mailer->ErrorInfo);
This eror
Fatal error: Uncaught Error: Class 'League\OAuth2\Client\Provider\Google' not found in /opt/lampp/htdocs/mailer/PHPMailer/class.phpmaileroauthgoogle.php:54 Stack trace: #0 /opt/lampp/htdocs/mailer/PHPMailer/class.phpmaileroauthgoogle.php(67): PHPMailerOAuthGoogle->getProvider() #1 /opt/lampp/htdocs/mailer/PHPMailer/class.phpmaileroauthgoogle.php(74): PHPMailerOAuthGoogle->getToken() #2 /opt/lampp/htdocs/mailer/PHPMailer/class.smtp.php(470): PHPMailerOAuthGoogle->getOauth64() #3 /opt/lampp/htdocs/mailer/PHPMailer/class.phpmaileroauth.php(174): SMTP->authenticate('', '', 'XOAUTH2', '', '', Object(PHPMailerOAuthGoogle)) #4 /opt/lampp/htdocs/mailer/PHPMailer/class.phpmailer.php(1540): PHPMailerOAuth->smtpConnect(Array) #5 /opt/lampp/htdocs/mailer/PHPMailer/class.phpmailer.php(1335): PHPMailer->smtpSend('Date: Thu, 3 Au...', 'Message\n') #6 /opt/lampp/htdocs/mailer/PHPMailer/class.phpmailer.php(1213): PHPMailer->postSend() #7 /opt/lampp/htdocs/mailer/index.php(27): PHPMailer->send() #8 {main} thrown in /opt/lampp/htdocs/mailer/PHPMailer/class.phpmaileroauthgoogle.php on line 54
i don't know what my mistake, i just want to create mail with PHPmailer using google smtp with Oauth2.0, I've searched on google but I have not found the solution yet
Look at PHPMailer's composer.json file - it includes the OAuth class as a suggestion, because it's not a requirement, and won't work on older PHP versions that PHPMailer supports, so it can't be enabled by default.
You need to add it to your own composer.json file (the same one you load PHPMailer with).
I've added comments to this effect to the Gmail OAuth2 tutorial on the PHPMailer wiki.
While I'm here, I suggest you don't implement this using PHPMailer 5.2, but go directly to PHPMailer 6.0 which has much better support for OAuth2.

Unable to send mail from localhost in zend framework 2

I am unable to send mail using localhost connection and correct code . But getting this error
Zend\Mail\Protocol\Exception\RuntimeException
File:
/var/www/html/rustagi/vendor/ZF2/library/Zend/Mail/Protocol/AbstractProtocol.php:348
Message:
5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbsl
5.7.14 6EWOp6ZKxNQid3Q84gVgHX7gRcrLH3FRL88s1Y-aurZXgnxEziLaRJrbc2OxtCgugbO98W
5.7.14 CMXNsqYvCG5EwG5iP9GU2N3NErFFURRUqffsIRCJW_u7_4WdRXu_Rb1mZjTITQm8VexWbn
5.7.14 tDk2rYv_9cv7NAcQUi5eItDC9HhF0mKns_yLRj1Rsl3nz0sQmn0rtfiJkg_mF8hQelwSAZ
5.7.14 F943KqTxTU3tWiZzO54KNZdUd3YWI> Please log in via your web browser and
5.7.14 then try again.
5.7.14 Learn more at
5.7.14 https://support.google.com/mail/answer/78754 tn5sm2706283pac.32 - gsmtp
Stack trace:
#0 /var/www/html/rustagi/vendor/ZF2/library/Zend/Mail/Protocol/Smtp/Auth/Login.php(83): Zend\Mail\Protocol\AbstractProtocol->_expect(235)
#1 /var/www/html/rustagi/vendor/ZF2/library/Zend/Mail/Protocol/Smtp.php(193): Zend\Mail\Protocol\Smtp\Auth\Login->auth()
#2 /var/www/html/rustagi/vendor/ZF2/library/Zend/Mail/Transport/Smtp.php(340): Zend\Mail\Protocol\Smtp->helo('localhost')
#3 /var/www/html/rustagi/vendor/ZF2/library/Zend/Mail/Transport/Smtp.php(325): Zend\Mail\Transport\Smtp->connect()
#4 /var/www/html/rustagi/vendor/ZF2/library/Zend/Mail/Transport/Smtp.php(336): Zend\Mail\Transport\Smtp->lazyLoadConnection()
#5 /var/www/html/rustagi/vendor/ZF2/library/Zend/Mail/Transport/Smtp.php(205): Zend\Mail\Transport\Smtp->connect()
#6 /var/www/html/rustagi/module/ZfcAdmin/src/ZfcAdmin/Controller/AdminController.php(185): Zend\Mail\Transport\Smtp->send(Object(Zend\Mail\Message))
#7 /var/www/html/rustagi/vendor/ZF2/library/Zend/Mvc/Controller/AbstractActionController.php(83): ZfcAdmin\Controller\AdminController->SendMailAction()
#8 [internal function]: Zend\Mvc\Controller\AbstractActionController->onDispatch(Object(Zend\Mvc\MvcEvent))
#9 /var/www/html/rustagi/vendor/ZF2/library/Zend/EventManager/EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#10 /var/www/html/rustagi/vendor/ZF2/library/Zend/EventManager/EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#11 /var/www/html/rustagi/vendor/ZF2/library/Zend/Mvc/Controller/AbstractController.php(117): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#12 /var/www/html/rustagi/vendor/ZF2/library/Zend/Mvc/DispatchListener.php(114): Zend\Mvc\Controller\AbstractController->dispatch(Object(Zend\Http\PhpEnvironment\Request), Object(Zend\Http\PhpEnvironment\Response))
#13 [internal function]: Zend\Mvc\DispatchListener->onDispatch(Object(Zend\Mvc\MvcEvent))
#14 /var/www/html/rustagi/vendor/ZF2/library/Zend/EventManager/EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#15 /var/www/html/rustagi/vendor/ZF2/library/Zend/EventManager/EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#16 /var/www/html/rustagi/vendor/ZF2/library/Zend/Mvc/Application.php(313): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#17 /var/www/html/rustagi/index.php(17): Zend\Mvc\Application->run()
#18 {main}
I am using this code :-
$options = new Mail\Transport\SmtpOptions(array(
'name' => 'localhost',
'host' => 'smtp.gmail.com',
'port'=> 587,
'connection_class' => 'login',
'connection_config' => array(
'username' => 'someone#gmail.com',
'password' => '********',
'ssl'=> 'tls',
),
));
$fileContents = fopen("/usr/share/pixmaps/faces/sky.jpg", 'r');
$attachment = new Mime\Part($fileContent);
$attachment->type = 'image/jpg';
$attachment->disposition = Mime\Mime::DISPOSITION_ATTACHMENT;
// $this->renderer = $this->getServiceLocator()->get('ViewRenderer');
$content = "gdgdfgdfgdfgddfg";
// make a header as html
$html = new MimePart($content);
$html->type = "text/html";
$body = new MimeMessage();
$body->setParts(array($html,$attachment));
// instance mail
$mail = new Mail\Message();
$mail->setBody($body); // will generate our code html from template.phtml
$mail->setFrom('someone#gmail.com','Sender Name');
$mail->setTo('someone1#gmail.com');
$mail->setSubject('Your Subject');
$transport = new Mail\Transport\Smtp($options);
$transport->send($mail);
How can i achieve this .I've searched alot on this but getting no solution for this your help will be highly appriciated thanx.
Sorry if i'm digging up this subject but i got the same problem. The send function seems to not works but there is no error message...
Here is a part of my code:
$mail = new Mail\Message();
$options = new Mail\Transport\SmtpOptions(array(
'name' => 'localhost',
'host' => 'smtp.gmail.com',
'port'=> 465,
'connection_class' => 'login',
'connection_config' => array(
'username' => 'someone#gmail.com',
'password' => '***',
'ssl'=> 'ssl',
),
));
$mail->setBody("This is the text of the email");
$mail->setFrom('noreply#reims.fr','Ecole Municipal des Sports');
$mail->addTo('someone#gmail.com');
$mail->setSubject('Inscription annulée');
$transport = new Mail\Transport\Sendmail('someone#gmail.com',$options);
$transport->send($mail);
var_dump($mail);
var_dump($transport);
var_dump($transport->send($mail));
change line
'ssl'=> 'tls' // tls causes problems when sending via gmail
to
'ssl'=> 'ssl'
be sure that in the configuration ini there is no overwriting entry
smtp.params.ssl = "tls" // this is bad -> tls causes problems when sending via gmail
Nothing to do with TLS. You should also allow less secure apps through your gmail account here. Worked for me..!!
see your configuration is false
using ssl or tls?
i suggest to you using phpmailer script
first active SMTP service from php.ini
search: [mail function]
[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = localhost
; http://php.net/smtp-port
smtp_port = 25
and next time you must using PHPMailer class ~>
Download PHPMailer
template function:
Use this ↓
<?php
require_once ("PHPMailer/class.phpmailer.php");
$mail = new PHPMailer(true);
$mail->isSMTP();
try{
$mail->host = "smtp#gmail.com";
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Port = 465;
$mail->Username = "username#email.com";
$mail->Password = "yourPassword";
$mail->AddAddress("target#email.com");
$mail->SetForm("yourMail#Email.com","your name");
$mail->Subject = "emailSubject";
$mail->CharSet = "UTF-8";
$mail->ContentType = "text/htm";
$mail->msgHTML("MailContent");
$mail->send();
echo 'Seccess';
}
catch(phpmailerException $mail){
echo $mail->errorMessage();
}
catch(Exception $mail){
echo $mail->getMessage();
}
?>

Swift mailer not working in server but working in local machine

My code is:
require_once 'Swift-5.0.3/lib/swift_required.php';
$transport = Swift_SmtpTransport::newInstance('smtp-mail.outlook.com', 587, "tls")
->setUsername('username#outlook.com')
->setPassword('password');
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance('Test')
->setFrom(array('demo#gmail.com' => 'Dpu'))
->setTo(array('demo2#gmail.com'))
->setBody('test body');
$result = $mailer->send($message);
if($result)
echo "success";
else
echo "Failed";
The code is working and mail is sent when I working in local machine. But in server I got the error and mail was not sent.
The error is:
Fatal error: Uncaught exception 'Swift_TransportException' with message 'Failed to authenticate on SMTP server with username "username#outlook.com" using 2 possible authenticators' in /home/xxx/public_html/xxx/Mail/Swift-5.0.3/lib/classes/Swift/Transport/Esmtp/AuthHandler.php:184 Stack trace: #0 /home/xxx/public_html/xxx/Mail/Swift-5.0.3/lib/classes/Swift/Transport/EsmtpTransport.php(312): Swift_Transport_Esmtp_AuthHandler->afterEhlo(Object(Swift_SmtpTransport)) #1 /home/xxx/public_html/xxx/Mail/Swift-5.0.3/lib/classes/Swift/Transport/AbstractSmtpTransport.php(120): Swift_Transport_EsmtpTransport->_doHeloCommand() #2 /home/xxx/public_html/xxx/Mail/Swift-5.0.3/lib/classes/Swift/Mailer.php(80): Swift_Transport_AbstractSmtpTransport->start() #3 /home/xxx/public_html/xxx/Mail/mail.php(19): Swift_Mailer->send(Object(Swift_Message)) #4 {main} thrown in /home/xxx/public_html/xxx/Mail/Swift-5.0.3/lib/classes/Swift/Transport/Esmtp/AuthHandler.php on line 184
Please help me...

Can't send mail with SwiftMailer using SmtpTransport in live environment but can send in localhost

I am using SwiftMailer to send a simple mail. But when I use the SmtpTransport (with host, username and a password) method, I get the following error of using 2 possible authenticators. I searched the issue on stackoverflow but could not find a solution.
Fatal error: Uncaught exception 'Swift_TransportException' with message 'Failed to authenticate on SMTP server with username "xxx#yyyyyyyy.com" using 2 possible authenticators' in /home/yyyyyyyy/public_html/swiftmailer/lib/classes/Swift/Transport/Esmtp/AuthHandler.php:181 Stack trace: #0
/home/yyyyyyyy/public_html/swiftmailer/lib/classes/Swift/Transport/EsmtpTransport.php(307): Swift_Transport_Esmtp_AuthHandler->afterEhlo(Object(Swift_SmtpTransport)) #1
/home/yyyyyyyy/public_html/swiftmailer/lib/classes/Swift/Transport/AbstractSmtpTransport.php(118): Swift_Transport_EsmtpTransport->_doHeloCommand() #2
/home/yyyyyyyy/public_html/swiftmailer/lib/classes/Swift/Mailer.php(79): Swift_Transport_AbstractSmtpTransport->start() #3
/home/yyyyyyyy/public_html/testmail.php(51): Swift_Mailer->send(Object(Swift_Message)) #4 {main} thrown in /home/lexington21/public_html/swiftmailer/lib/classes/Swift/Transport/Esmtp/AuthHandler.php on line 181
My source is as follows. This source work perfectly in my localhost but not when it is uploaded to the live environment.
$transport = Swift_SmtpTransport::newInstance("SMTPHOST", 25)
->setUsername("user")
->setPassword("pass");
$mailer = Swift_Mailer::newInstance($transport);
$to = "abc#aaa.com";
$subject = "Test Mail";
$email_body = "Hi Please confirm if you have received this email.";
$mail = Swift_Message::newInstance($subject)
->setFrom(array("research#lexingtonstudies.com"))
->setTo(array($to))
->setBcc(array("bcc1#aaa.com", "bcc2#bbb.com"))
->setBody($email_body, 'text/html');
$mailing_result = $mailer->send($mail);
I used 465 with ssl but got this error.
Swift_SmtpTransport::newInstance("SMTPHOST", 465, 'ssl')
Error
Fatal error: Uncaught exception 'Swift_TransportException' with message 'Connection could not be established with host SMTPHOST [Connection refused #111]' in /home/yyyyyyyy/public_html/swiftmailer/lib/classes/Swift/Transport/StreamBuffer.php:265 Stack trace: #0
/home/yyyyyyyy/public_html/swiftmailer/lib/classes/Swift/Transport/StreamBuffer.php(62): Swift_Transport_StreamBuffer->_establishSocketConnection() #1
/home/yyyyyyyy/public_html/swiftmailer/lib/classes/Swift/Transport/AbstractSmtpTransport.php(113): Swift_Transport_StreamBuffer->initialize(Array) #2
/home/yyyyyyyy/public_html/swiftmailer/lib/classes/Swift/Mailer.php(79): Swift_Transport_AbstractSmtpTransport->start() #3
/home/yyyyyyyy/public_html/testmail.php(51): Swift_Mailer->send(Object(Swift_Message)) #4 {main} thrown in /home/yyyyyyyy/public_html/swiftmailer/lib/classes/Swift/Transport/StreamBuffer.php on line 265
Please help me solve this.
The security setting might need to be 'tls' instead of 'ssl' (check with your SMTP host). Also confirm the port is correct. And I set Swiftmailer's "local" domain. Here is the code that works for me in 3 environments: localhost, internal postfix SMTP server, and external SMTP (Mailgun):
// $config is an array populated from a config file
$host = $config['host'];
$port = $config['port'] ?: 25;
$user = $config['user'];
$pass = $config['pass'];
$security = 'tls';
if (isset($config['security'])) {
$security = $config['security'];
}
if (empty($config['security'])) {
$security = 'tcp';
}
$transport = Swift_SmtpTransport::newInstance($host, $port, $security)
->setUsername($user)
->setPassword($pass);
// Explicitly set HELO domain
$transport->setLocalDomain($host);
$mailer = Swift_Mailer::newInstance($transport);

Categories