Zend_Log with mail - zend-framework

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');

Related

Facebook PHP SDK "couldn't connect to host"

I'm trying to connect to facebook API
require_once '/composer/vendor/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => 'xxxx', // obviously, I've put my app ID in here
'app_secret' => 'xxxx', // and my app secret in here
'default_graph_version' => 'v2.4',
]);
To do the following request, which, if I'm right, should give me the number of shares of the URL :
$target = 'http://stackoverflow.com';
$node = $fb->get('/?id='.urlencode($target).'&fields=share')->getGraphNode();
$shareCount = $node['share']['share_count'];
However, I get the following error :
Fatal error:
Uncaught exception 'Facebook\Exceptions\FacebookSDKException'
with message 'couldn't connect to host'
in /backend/composer/vendor/facebook/php-sdk-v4/src/Facebook/HttpClients/FacebookCurlHttpClient.php:83
Stack trace:
#0 /backend/composer/vendor/facebook/php-sdk-v4/src/Facebook/FacebookClient.php(216):
Facebook\HttpClients\FacebookCurlHttpClient->send('https://graph.f...', 'GET', '', Array, 60)
#1 /backend/composer/vendor/facebook/php-sdk-v4/src/Facebook/Facebook.php(504):
Facebook\FacebookClient->sendRequest(Object(Facebook\FacebookRequest))
#2 /backend/composer/vendor/facebook/php-sdk-v4/src/Facebook/Facebook.php(377):
Facebook\Facebook->sendRequest('GET', '/?id=http%3A%2F...', Array, NULL, NULL, NULL)
#3 /backend/the_script_i_am_currently_trying_to_run.php(79):
Facebook\Facebook->get('/?id=http%3A%2F...')
#4 {main} thrown in /backend/composer/vendor/facebook/php-sdk-v4/src/Facebook/HttpClients/FacebookCurlHttpClient.php on line 83
From what I have read, it could be that I'm not allowed to use cURL on my server, but I'm using it somewhere else and it works, so I don't think the problem comes from here.
Any idea of what I'm doing wrong ?
Thanks

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

Zend Framework 2 Zend_Http_Client SSL Connection Error

i want to parse all kinds of ssl/https Sites.
E.g i want the content of https://microsoft.de, but it do not work. Following Erros appears:
Fatal error: Uncaught exception 'ErrorException' with message 'stream_socket_enable_crypto() [<a href='function.stream-socket-enable-crypto'>function.stream-socket-enable-crypto</a>]:
SSL operation failed with code 1.
OpenSSL Error messages: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed'
in C:\Users\Privat\Desktop\Server\Apache2\htdocs\allsubmitter\server\vendor\zendframework\zend-http\Zend\Http\Client\Adapter\Socket.php:276
Stack trace: #0 [internal function]: Zend\Stdlib\ErrorHandler::addError(2, 'stream_socket_e...', 'C:\Users\Privat...', 276, Array) #1 C:\Users\Privat\Desktop\Server\Apache2\htdocs\allsubmitter\server\vendor\zendframework\zend-http\Zend\Http\Client\Adapter\Socket.php(276): stream_socket_enable_crypto(Resource id #35, true, 2)
#2 C:\Users\Privat\Desktop\Server\Apache2\htdocs\allsubmitter\server\vendor\zendframework\zend-http\Zend\Http\Client.php(1356): Zend\Http\Client\Adapter\Socket->connect('microsoft.de', 443, true) #3 C:\Users\Privat\Desktop\Server\Apache2\htdocs\ in C:\Users\Privat\Desktop\Server\Apache2\htdocs\allsubmitter\server\vendor\zendframework\zend-http\Zend\Http\Client\Adapter\Socket.php on line 299
$config = array(
'adapter' => 'Zend_Http_Client_Adapter_Socket',
'ssltransport' => 'tls'
);
$client = new \Zend\Http\Client("https://microsoft.de");
You can tell the client not to verify the SSL, try this config:
$clientConfig = array(
'adapter' => 'Zend\Http\Client\Adapter\Curl',
'curloptions' => array(
CURLOPT_FOLLOWLOCATION => TRUE,
CURLOPT_SSL_VERIFYPEER => FALSE
),
);
$this->_client = new \Zend\Http\Client('https://microsoft.de', $clientConfig);

Unable to send E-mails using Swift Mailer

$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/

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