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.
Related
I am trying to load email list using oauth 2. Following is my code:
<?php
require_once "vendor/autoload.php";
use src\API\Type;
use garethp\ews\MailAPI as API;
$token='EwA......'; //access token
$api = API::withCallbackToken('outlook.office365.com', $token);
$mail = $api->getMailItems();
foreach ($mail as $email) {
$email = $api->getItem($email->getItemId());
$email_id=$email->getItemId();
$subject = $email->getSubject();
$sender = $email->getSender()->getMailbox()->getEmailAddress();
$body = (string) $email->getBody();
echo $body;
}
but getting response as follows:
Fatal error: Uncaught garethp\ews\API\Exception\UnauthorizedException
in
C:\inetpub\wwwroot\bephpforTteam\New\php_ews\php-ews-master\src\API\ExchangeWebServices.php:438
Stack trace: #0
C:\inetpub\wwwroot\bephpforTteam\New\php_ews\php-ews-master\src\API\ExchangeWebServices.php(356):
garethp\ews\API\ExchangeWebServices->handleNonSuccessfulResponses(NULL,
401) #1
C:\inetpub\wwwroot\bephpforTteam\New\php_ews\php-ews-master\src\API\ExchangeWebServices\MiddlewareFactory.php(57):
garethp\ews\API\ExchangeWebServices->processResponse(NULL) #2
C:\inetpub\wwwroot\bephpforTteam\New\php_ews\php-ews-master\src\API\ExchangeWebServices.php(497):
garethp\ews\API\ExchangeWebServices->garethp\ews\API\ExchangeWebServices{closure}(Object(garethp\ews\API\MiddlewareRequest),
Object(Closure)) #3
C:\inetpub\wwwroot\bephpforTteam\New\php_ews\php-ews-master\src\API\ExchangeWebServices\MiddlewareFactory.php(66):
garethp\ews\API\ExchangeWebServices->garethp\ews\API{closure}(Object(garethp\ews\API\MiddlewareRequest)) #4 C:\inetpub\wwwroot\bephpforTt in C:\inetpub\wwwroot\bephpforTteam\New\php_ews\php-ews-master\src\API\ExchangeWebServices.php
on line 438
<?php
require_once 'swiftmailer/swift_required.php';
require_once('dompdf/dompdf_config.inc.php');
spl_autoload_register('DOMPDF_autoload');
date_default_timezone_set('Asia/Manila');
function pdf_create($html,$filename,$stream = TRUE){
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$output = $dompdf->output();
file_put_contents('puja.pdf', $output);
}
$tym = date('g:i s');
$filename = 'print'.$tym;
$dompdf = new DOMPDF();
$html = file_get_contents('vamshi.php');
pdf_create($html,$filename);
?>
<?php
// Create the mail transport configuration
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com',465,'ssl')
->setUsername('rajeshchintu.14#gmail.com')
->setPassword('9397343655');
// Create the message
$message = Swift_Message::newInstance()
->setTo(array('saivamshigupta#gmail.com' =>'vamshi'))
->setSubject("Registration Success !")
->setBody("You have Registered Successfully ! Thank You For Registering With Us.Click link to confirm your account",'text/html')
->setFrom("rajeshchintu.14#gmail.com", "Rajesh")
->attach(Swift_Attachment::fromPath('/full/file/path/puja.pdf'));
// Send the email
$mailer = Swift_Mailer::newInstance($transport);
$mailer->send($message);
if($mailer){
echo 'eafbaefdbaifhefj';
}
?>
Fatal error: Uncaught exception 'Swift_TransportException' with
message 'Expected response code 250 but got code "535", with message
"535-5.7.8 Username and Password not accepted. Learn more at 535 5.7.8
https://support.google.com/mail/answer/14257 g74sm5822247pfj.1 - gsmtp
"' in
C:\xampp\htdocs\PHPLab\new\swiftmailer\classes\Swift\Transport\AbstractSmtpTransport.php:382
Stack trace: #0
C:\xampp\htdocs\PHPLab\new\swiftmailer\classes\Swift\Transport\AbstractSmtpTransport.php(279):
Swift_Transport_AbstractSmtpTransport->_assertResponseCode('535-5.7.8
Usern...', Array) #1
C:\xampp\htdocs\PHPLab\new\swiftmailer\classes\Swift\Transport\EsmtpTransport.php(243):
Swift_Transport_AbstractSmtpTransport->executeCommand('RSET\r\n',
Array, Array) #2
C:\xampp\htdocs\PHPLab\new\swiftmailer\classes\Swift\Transport\Esmtp\Auth\XOAuth2Authenticator.php(54):
Swift_Transport_EsmtpTransport->executeCommand('RSET\r\n', Array) #3
C:\xampp\htdocs\PHPLab\new\swiftmailer\classes\Swift\Transport\Esmtp\AuthHandler.php(177):
Swift_Transport_Esmtp_A in
C:\xampp\htdocs\PHPLab\new\swiftmailer\classes\Swift\Transport\AbstractSmtpTransport.php
on line 382
Google introduced a new security feature that prevents sending of emails from less secure application. In this regard any application to developed by google is more or less considered a less secure app.
So what does this mean for me ?
To get around this issue you can set your google account to allow sending of emails for a less secure application.
You can do this from here https://myaccount.google.com/security
alternatively you can use another smtp server to send out your emails
here is a similar question i answered based on yii2 framework
https://stackoverflow.com/a/35908151/2536812
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...
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);
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!!