Unable to send E-mails using Swift Mailer - html-email

$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, "ssl")
->setUsername('username')
->setPassword('password');
$username = $_SESSION['username'];
$from = $_POST['from'];
$to = $_POST['to'];
$subject = $_POST['subject'];
$body = $_POST['message'];
$message = Swift_Message::newInstance($subject)
->setFrom(array($from => $username))
->setTo(array($to))
->setBody($body);
$mailer = Swift_Mailer::newInstance($transport);
$result = $mailer->send($message);
Error returned on web browser:
Fatal error: Uncaught exception 'Swift_TransportException' with message 'Connection could not be established with host smtp.gmail.com [Unable to find the socket transport "ssl" - did you forget to enable it when you configured PHP? #157919304]' in C:\Program Files\xampp\htdocs\healthcare system\swift\lib\classes\Swift\Transport\StreamBuffer.php:259 Stack trace: #0 C:\Program Files\xampp\htdocs\healthcare system\swift\lib\classes\Swift\Transport\StreamBuffer.php(64): Swift_Transport_StreamBuffer->_establishSocketConnection() #1 C:\Program Files\xampp\htdocs\healthcare system\swift\lib\classes\Swift\Transport\AbstractSmtpTransport.php(115): Swift_Transport_StreamBuffer->initialize(Array) #2 C:\Program Files\xampp\htdocs\healthcare system\swift\lib\classes\Swift\Mailer.php(80): Swift_Transport_AbstractSmtpTransport->start() #3 C:\Program Files\xampp\htdocs\healthcare system\mail_process.php(32): Swift_Mailer->send(Object(Swift_Message)) #4 {main} thrown in C:\Program Files\xampp\htdocs\healthcare system\swift\lib\classes\Swift\Transport\StreamBuffer.php on line 259

Replace the below code:
$message = Swift_Message::newInstance('$subject')
->setFrom(array('$from' => '$username'))
->setTo(array('$to'))
->setBody('$body');
With
$message = Swift_Message::newInstance($subject)
->setFrom(array($from => $username))
->setTo(array($to))
->setBody($body);
As here you are using the single quotation mark that is the problem in your code.

replace this
->setUsername('username')
->setPassword('password');
with
$transport->setUsername('username'); /* note: user name is your gmail username(eg
asaa#gmail.com) */
$transport->setPassword('password'); /password is your gmail password/

Related

phpmailer 6.0 success message but no mail received using gmail as relay

I've tried a bunch of alterations to my code but with no effect. The code itself does not return any errors but instead gives a success message. I am using gmail as my relay.
P.S, I commented out $mail->IsSMTP(); because I saw a similar question here that used it as a fix, I was getting an "smtp failed to connect" error.
I am using PHPmailer 6.0.
Here is my code:
<?php
require_once('vendor/autoload.php');
define('GUSER', 'example#gmail.com'); // GMail username
define('GPWD', '*********'); // GMail password
function smtpmailer($to, $from, $from_name, $subject, $body) {
global $error;
$mail = new PHPMailer\PHPMailer\PHPMailer(true); // create a new object
//$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 4; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for GMail
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Username = GUSER;
$mail->Password = GPWD;
$mail->SetFrom($from, $from_name);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress($to);
if(!$mail->Send()) {
$error = 'Mail error: '.$mail->ErrorInfo;
return false;
} else {
$error = 'Message sent!';
return true;
}
}
smtpmailer('to#mail.com', 'from#mail.com', 'yourName', 'test mail message', 'Hello World!');
if (smtpmailer('to#mail.com', 'from#mail.com', 'yourName', 'test mail message', 'Hello World!')) {
// do something
}
if (!empty($error)) echo $error;
?>
If I uncomment $mail->IsSMTP(); I get this error log:
2017-12-27 07:58:54 Connection: opening to smtp.gmail.com:465, timeout=300, options=array()
2017-12-27 07:58:54 Connection failed. Error #2: stream_socket_client(): unable to connect to smtp.gmail.com:465 (Network is unreachable) [/srv/disk2/2564570/www/consorttest.dx.am/vendor/phpmailer/phpmailer/src/SMTP.php line 325]
2017-12-27 07:58:54 SMTP ERROR: Failed to connect to server: Network is unreachable (101)
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Fatal error: Uncaught PHPMailer\PHPMailer\Exception: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting in /srv/disk2/2564570/www/consorttest.dx.am/vendor/phpmailer/phpmailer/src/PHPMailer.php:1726 Stack trace: #0 /srv/disk2/2564570/www/consorttest.dx.am/vendor/phpmailer/phpmailer/src/PHPMailer.php(1481): PHPMailer\PHPMailer\PHPMailer->smtpSend('Date: Wed, 27 D...', 'Hello World!\r\n') #1 /srv/disk2/2564570/www/consorttest.dx.am/vendor/phpmailer/phpmailer/src/PHPMailer.php(1320): PHPMailer\PHPMailer\PHPMailer->postSend() #2 /srv/disk2/2564570/www/consorttest.dx.am/mailtest.php(23): PHPMailer\PHPMailer\PHPMailer->send() #3 /srv/disk2/2564570/www/consorttest.dx.am/mailtest.php(32): smtpmailer('to#mail.com', 'from#mail.com', 'yourName', 'test mail messa...', 'Hello World!') #4 {main} thrown in /srv/disk2/2564570/www/consorttest.dx.am/vendor/phpmailer/phpmailer/src/PHPMailer.php on line 1726
You're not "using gmail as your relay" if you comment out isSMTP() because then it's not using SMTP at all, and will ignore all your SMTP settings. You're sending via your local mail server using PHP's built-in mail function.
When sending through gmail, you can't use arbitrary from addresses, though you can preset aliases in your gmail account.
You've based your code on a very old and obsolete example - use the gmail one provided with PHPMailer.
The most important part of the error output is: Network is unreachable - that probably means your ISP blocks outbound SMTP - are you by any chance using GoDaddy?
Next up, you have a basic misconfiguration: you're connecting to port 465 using SMTPSecure = 'tls', which means it will try to use SMTP+STARTTLS explicit TLS encryption, and that just won't work on port 465. This is a key reason to use the provided examples - they don't make basic errors like this.
Every one of these things is covered in the troubleshooting guide the error links to.

Regarding SSL Protocol in SMTP server Validity

I've success to sent a mail through PHP in last month with this code.
if($this->form_validation->run()){
#generate a random key
$key = md5(uniqid());
$this->load->model('model_users');
require_once "swift/lib/swift_required.php";
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')
->setUsername('xxxxx#gmail.com')
->setPassword('xxxxx');
// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
$messagebody = "<p>Thank you for signing up!</p>";
$messagebody .="<form action= '".base_url()."main/register_user/$key' method='post'>
<input type='submit' value='Activate my Account'> </form> ";
// <p><a href='".base_url()."main/register_user/$key'>Click Here</a> to confirm your account.
// Create a message
$message = Swift_Message::newInstance('CBS Account Activation Message')
->setFrom(array('xxxxx#gmail.com' => 'CBS Group'))
->setContentType("text/html")
->setTo(array($this->input->post('email') => $this->input->post('email')))
->setBody($messagebody)
->setMaxLineLength(100)
;
// Send the message
$result = $mailer->send($message);
//send and email to user
if($this->model_users->add_temp_user($key)){
if($result){
echo "The email has been sent!";
$this->load->view('signup_success');
}
else{
// Failure Message
echo "Could not send mail";
}
}else
echo "Problem adding to database";
#add them to the temp_user db
}
Present I'm trying the use same code in same system it raises an error.
Fatal error: Uncaught exception 'Swift_TransportException' with message 'Connection could not be established with host smtp.gmail.com [ #0]' in C:\xampp\htdocs\code\application\controllers\swift\lib\classes\Swift\Transport\StreamBuffer.php:266 Stack trace: #0 C:\xampp\htdocs\code\application\controllers\swift\lib\classes\Swift\Transport\StreamBuffer.php(64): Swift_Transport_StreamBuffer->_establishSocketConnection() #1 C:\xampp\htdocs\code\application\controllers\swift\lib\classes\Swift\Transport\AbstractSmtpTransport.php(115): Swift_Transport_StreamBuffer->initialize(Array) #2 C:\xampp\htdocs\code\application\controllers\swift\lib\classes\Swift\Mailer.php(80): Swift_Transport_AbstractSmtpTransport->start() #3 C:\xampp\htdocs\code\application\controllers\main.php(370): Swift_Mailer->send(Object(Swift_Message)) #4 [internal function]: Main->forgot_validation() #5 C:\xampp\htdocs\code\system\core\CodeIgniter.php(359): call_user_func_array(Array, Array) #6 C:\xampp\htdocs\code\index.php(202): require_once('C:\\xampp\\htdocs. in C:\xampp\htdocs\code\application\controllers\swift\lib\classes\Swift\Transport\StreamBuffer.php on line 266
If i use this same code in another system it's run successfully and sent mail to particular mail id....
I didn't changed the code also.
How can i resolve this problem

mblox soap api - php soap Payment Request not working

mblox soap api - php soap Payment Request not working and gives error.
I use as below code in subscribe.php file...
$client = new SoapClient("https://ngp.us.mblox.com/client-gateway/services?wsdl", array('trace' => 1, 'encoding' => 'UTF-8', 'soap_version' => SOAP_1_2));
$array = array(
'SecurityContext'=>array('userId'=>'aaaaaaaa','password'=>'bbbbbbbb'),
'ClientDetails'=>array('shortcode'=>'234242', 'brandName'=>'aaaaaaaaaa.com', 'programSponsor'=>'aaaaaaaaaa', 'originatingUrl'=>'www.aaaaaaaaaa.com', 'minPageUrl'=>'www.aaaaaaaaaa.com', 'successUrl'=>'www.aaaaaaaaaa.com/subscribConfirm.php', 'cancelUrl'=>'www.aaaaaaaaaa.com', 'tcUrl'=>'www.aaaaaaaaaa.com/terms.html', 'postBackUrl'=>'http://aaaaaaaaaa/subscribe.php'),
'paymentDetails'=>array('paymentType'=>'PSMS', 'amount'=>'9.99', 'currency'=>'USD', 'billingFrequency'=>'MONTHLY'),
'msisdn'=>'243233232',
'serviceId'=>'332',
'operatorId'=>'33343',
'productDescription'=>'Text test',
'optInBody'=>'aaaaaaaaaa',
'browserSessionId'=>'123456',
);
$result = $client->initiatePayment($array);
It shows error as below on last line => $result = $client->initiatePayment($array);
Fatal error: Uncaught SoapFault exception: [(null)] in
/aaaaaaaa/Source/developement/PHP/ver1/subscribe.php:97 Stack trace:
0 /aaaaaaaa/Source/developement/PHP/ver1/subscribe.php(97): SoapClient->__call('initiatePayment', Array) #1
/aaaaaaaa/Source/developement/PHP/ver1/subscribe.php(97):
SoapClient->initiatePayment(Array) #2 {main} thrown in
/aaaaaaaa/Source/developement/PHP/ver1/subscribe.php on line 97
I got things working using curl,
I use This
as a reference and code working.
Thankx

Zend Mail keeps giving me a Socket Error

I'm having trouble with sending email using the zend framework. I keep getting a "Could not open socket" error.
I don't know whats wrong here - it used to work on my other host. Ever since I shifted it to another host I can't send emails. I've set up the configuration values to match the new email server.
Heres my code:
$config = array('auth' => _config('mail', 'auth'),
'username' => _config('mail', 'email'),
'password' => _config('mail', 'password'));
$tr = new Zend_Mail_Transport_Smtp(_config('mail', 'smtp'), $config);
$mail = new Zend_Mail();
$mail->setDefaultTransport($tr);
$mail->setFrom(_config('mail','email'), _config('mail','name'));
$mail->addTo($account_email);
$mail->setSubject($mailTitle);
$mail->setBodyText($mailContent);
$mail->send($tr);
EDIt ===
Well the code posted above is my actual code - I don't know whats wrong with it as it used to work on another host.
The following is the exact error I'm getting
Could not open socketstring(1237) "#0 /home/india/public_html/demo/library/Zend/Mail/Protocol/Smtp.php(167): Zend_Mail_Protocol_Abstract->_connect('tcp://mail.indi...')
#1 /home/india/public_html/demo/library/Zend/Mail/Transport/Smtp.php(199): Zend_Mail_Protocol_Smtp->connect()
#2 /home/india/public_html/demo/library/Zend/Mail/Transport/Abstract.php(348): Zend_Mail_Transport_Smtp->_sendMail()
#3 /home/india/public_html/demo/library/Zend/Mail.php(1194): Zend_Mail_Transport_Abstract->send(Object(Zend_Mail))
#4 /home/india/public_html/demo/application/controllers/AccountController.php(2153): Zend_Mail->send(Object(Zend_Mail_Transport_Smtp))
#5 /home/india/public_html/demo/library/Zend/Controller/Action.php(513): AccountController->forgetPasswordAction()
#6 /home/india/public_html/demo/library/Zend/Controller/Dispatcher/Standard.php(295): Zend_Controller_Action->dispatch('forgetPasswordA...')
#7 /home/india/public_html/demo/library/Zend/Controller/Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http))
#8 /home/india/public_html/demo/application/bootstrap.php(26): Zend_Controller_Front->dispatch() #9 /home/india/public_html/demo/html/index.php(4): Bootstrap::run() #10 {main}"
marhaba Ali! ,
digging the code of Zend mail shows http://framework.zend.com/svn/framework/standard/trunk/library/Zend/Mail/Protocol/Abstract.php
protected function _connect($remote)
{
$errorNum = 0;
$errorStr = '';
// open connection
$this->_socket = #stream_socket_client($remote, $errorNum, $errorStr, self::TIMEOUT_CONNECTION);
if ($this->_socket === false) {
if ($errorNum == 0) {
$errorStr = 'Could not open socket';
}
/**
* #see Zend_Mail_Protocol_Exception
*/
require_once 'Zend/Mail/Protocol/Exception.php';
throw new Zend_Mail_Protocol_Exception($errorStr);
}
if (($result = $this->_setStreamTimeout(self::TIMEOUT_CONNECTION)) === false) {
/**
* #see Zend_Mail_Protocol_Exception
*/
require_once 'Zend/Mail/Protocol/Exception.php';
throw new Zend_Mail_Protocol_Exception('Could not set stream timeout');
}
return $result;
}
and usually the error number 0 because of
if ($errorNum == 0) {
$errorStr = 'Could not open socket';
}
from : http://php.net/manual/en/function.stream-socket-client.php
On failure the errno and errstr
arguments will be populated with the
actual system level error that
occurred in the system-level connect()
call. If the value returned in errno
is 0 and the function returned FALSE,
it is an indication that the error
occurred before the connect() call.
This is most likely due to a problem
initializing the socket. Note that the
errno and errstr arguments will always
be passed by reference.
I guess its some firewall blocking the connection to be sent out ,
system-level or network-level error
If you update your answer with more detailed info , i would be happy to help

Zend_Log with mail

Has anyone succeed to send an mail(SMTP GOOGLE) with Zend_Log.I found this http://framework.zend.com/manual/en/zend.log.writers.html
Chapter 32.2.4. Writing to Email but by default it's using mail(), but I want to use SMTP.So until now my code looks like this:
$mailconf = array('ssl'=>'ssl',
'auth' => 'login',
'username' => 'mymail#gmail.com',
'password' => 'mypass',
'port'=>'465');
$transport = new Zend_Mail_Transport_Smtp('smtp.gmail.com', $mailconf);
$mail = new Zend_Mail();
$mail->setFrom('from.website#example.com')
->addTo('mysecondmail#site.com');
$writer = new Zend_Log_Writer_Mail($mail);
// On fixe le sujet du mail
$writer->setSubjectPrependText('Important Log Events');
// Only email warning level entries and higher.
$writer->addFilter(Zend_Log::WARN);
$loggerZendMail = new Zend_Log();
$loggerZendMail->addWriter($writer);
$loggerZendMail->log('unable to connect to database',Zend_Log::WARN);
I'm getting this error:
Fatal error: Uncaught exception 'Zend_Log_Exception' with message 'Unable to send mail' in C:\wamp\www\zf_log\Zend\Log\Writer\Mail.php:256 Stack trace: #0 C:\wamp\www\zf_log\Zend\Log.php(84): Zend_Log_Writer_Mail->shutdown() #1 C:\wamp\www\zf_log\Zend\Controller\Action.php(512): Zend_Log->__destruct() #2 C:\wamp\www\zf_log\Zend\Controller\Action.php(512): IndexController->indexAction() #3 C:\wamp\www\zf_log\Zend\Controller\Dispatcher\Standard.php(288): Zend_Controller_Action->dispatch('indexAction') #4 C:\wamp\www\zf_log\Zend\Controller\Front.php(945): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http)) #5 C:\wamp\www\zf_log\index.php(30): Zend_Controller_Front->dispatch() #6 {main} thrown in C:\wamp\www\zf_log\Zend\Log\Writer\Mail.php on line 256
Any suggestions?
You need to set the transport for Zend_Mail to use
Example:
Zend_Mail::setDefaultTransport($transport);
I would check if SMTP is enabled on server.
Use TLS instead of SSL also check if the port 587 works
so the below config should work
$mailconf = array('ssl'=>'tls', 'auth' => 'login', 'username' => 'mymail#gmail.com', 'password' => 'mypass', 'port'=>'587');