Internal server error using Swiftmailer on Silex - php

I'm working on a Silex project and I'm trying to send an email using the Swiftmailer provider but always get an internal server error(500).
Registering:
$app->register(new Silex\Provider\SwiftmailerServiceProvider(), array(
'swiftmailer.options' => array(
'host' => 'smtp.gmail.com',
'port' => '465',
'username' => 'my_email',
'password' => 'my_password')
));
If I call the mailer $app['mailer'] throw an internal server error.
If I try with the documentation example I got an internal server error:
$app->post('/feedback', function () use ($app) {
$request = $app['request'];
$message = \Swift_Message::newInstance()
->setSubject('[YourSite] Feedback')
->setFrom(array('noreply#yoursite.com'))
->setTo(array('feedback#yoursite.com'))
->setBody($request->get('message'));
$app['mailer']->send($message);
return new Response('Thank you for your feedback!', 201);
});
I don't know what's the problem, I have no troubles with the other providers.
Any ideas?

Would it help to add the following entries in your 'swiftmailer.options' array? Gmail by default uses ssl on port 465.
'encryption' => 'ssl',
'auth_mode' => 'login',
[Update]
If you define namespace in your code, need to add '\' in front of Silex\Provider..
$this->register(new Silex\Provider\SwiftmailerServiceProvider(), array(
to
$this->register(new \Silex\Provider\SwiftmailerServiceProvider(), array(

Related

Cakephp 3 email with SMTP not working

I am trying to send an email from my CakePHP 3 application. But every time it is using the localhost SMTP and I am getting the error.
So here is my code.
public function sendEmail($email, $subject, $message){
// Sample SMTP configuration.
$this->loadModel('Generalsettings');
$query = $this->Generalsettings->find('all')->where(['meta_key' => 'smtp_details'])->applyOptions(['default' => false]);
$smtpdetail = $query->first();
$detail = json_decode($smtpdetail->value);
Email::configTransport('gmail', [
'host' => $detail['host'], //value is 'ssl://smtp.gmail.com'
'port' => $detail['port'], //value is 465
'username' => $detail['username'],
'password' => $detail['password'],
'className' => 'Smtp'
]);
$emailClass = new Email();
$emailClass->from(['er.dhimanmanoj#gmail.com' => "Sender"])
->to($email)
->subject($subject)
->send($message);
}
Please tell me if I am doing something wrong. Thanks in advance.
You haven't specified the transport you just created using configTransport() method. So it is taking the default settings from config/app.php.
You can setup transport like this:
$emailClass = new Email();
$emailClass->transport('gmail');
NOTE: Deprecated since version 3.4.0: Use setTransport() instead of transport().
For more info please refer to this link #https://book.cakephp.org/3.0/en/core-libraries/email.html
Hope this helps!

Silex + Swift Mailer not working

I have a Silex app with Swift Mailer, but it seems like the configuration was not loaded from $app['swiftmailer.options'].
I registered the service in my bootstrap file
$app->register(new SwiftmailerServiceProvider());
And in my configuration file
$app['swiftmailer.options'] = array(
'host' => 'smtp.mandrillapp.com',
'port' => '587',
'username' => 'MY_USERNAME',
'password' => 'MY_PASSWORD',
'encryption' => null,
'auth_mode' => null
);
And then I send my email with
$message = \Swift_Message::newInstance()
->setSubject($this->app['forum_name'] . ' Account Verification')
->setFrom(array('no-reply#domain.com'))
->setTo(array('recipient#example.com'))
->setBody('My email content')
->setContentType("text/html");
$this->app['mailer']->send($message);
The send function returns 1 but the email was never sent. But, when I try manually creating an instance of Swift_SmtpTransport, the email would send.
$transport = \Swift_SmtpTransport::newInstance('smtp.mandrillapp.com', 587)
->setUsername('MY_USERNAME')
->setPassword('MY_PASSWORD');
...
$mailer = \Swift_Mailer::newInstance($transport);
$mailer->send($message);
So the problem is the $app['swiftmailer.options'] is not read or loaded. Am I missing something here?
I'm following the instructions from here.
By default the SwiftmailerServiceProvider uses a spooled transport to queue up emails and sends them all during the TERMINATE stage (after a response is sent back to the client). If you don't call Application->run(), you are bypassing this process. Your mail will stay in the spool and never get sent.
If you want to sent mail outside of the normal Silex flow, you can flush the spool manually with
if ($app['mailer.initialized']) {
$app['swiftmailer.spooltransport']
->getSpool()
->flushQueue($app['swiftmailer.transport']);
}
That's taken directly from the SwiftmailerServiceProvider.
Or you can simply turn off spooling with
$app['swiftmailer.use_spool'] = false;
Try this:
$app->register(new \Silex\Provider\SwiftmailerServiceProvider(), array(
'swiftmailer.options' => array(
'sender' => 'sender',
'host' => 'host',
'port' => 'port',
'username' => 'username',
'password' => 'password'
)
));
It is not in the documentation.

Laravel Password remind - connection refused 61

I am stumped.
I am using Laravel's Password::remind, which has already been written for me, so there is nothing that I have changed:
try {
$reset = Password::remind($credentials);
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
When I submit the form, then I receive the following exception:
Exception
Connection could not be established with host localhost [Connection refused #61]
Which points to my throw exception line above
In my app/config/mail.php file, I have tried everything from mail to sendmail, from localhost to smtp.gmail.com - whatever I change in this config file, Laravel still thinks that it is localhost. Even tried "/usr/sbin/sendmail -t -i"
I have restarted apache and fpm - the error does not change.
When I try mail(email, title, message) - it works just fine. Of course, my goal is to not just send an email but to use Laravel's Password::remind - function where it sends an email with a link for the user to reset their password.
I have changed the /usr/local/etc/php/5.5/php.ini file, both the smtp and smtp_port
What do I need to do, this seems so straight forward in their documentation and no one else has complained about this issue for connection refused # 61. There are other connection refused and they have nothing to do with the built in Password::remind. This is driving me nuts.
I am running fpm-nginx.
Thanks in advance
Just to be on the safe side in respect to any configuration issues, I suggest you to try your application in a closed environment such as Homestead. This way, i.e. by relying on a fresh virtual machine, you might be able to figure out whether it is a configuration issue on the level of the different applications (apache, php, etc.) you are using. Otherwise, you would have to reinspect your code again. You can find more information on Homestead here: http://laravel.com/docs/4.2/homestead
OK, there were a couple of configurations that had to be in place and I am posting this answer in case anyone else using Yosemite is having this issue.
First, from my searching for the error "Connection refused #61" this is usually related to connectivity with a database as Korush suggested above. However, if I typed in an email that was not part of the database, Laravel would come back with a message that such and such email was not found, which told me that it was connected to the database, from the stand point of searching the email that was entered.
However, if a person does not have a "password_reminders" table in their localhost database, then a person would receive a connection refused error - be sure that you have this for Laravel to use in your localhost db:
CREATE TABLE password_reminders (
email VARCHAR(50) NOT NULL,
token VARCHAR(100) NOT NULL,
created_at TIMESTAMP
)
Second, Laravel can use the mail server on your system. In my case, I am using Yosemite, which has "postfix" available in the terminal:
sudo postfix start
Here is my local config which allows Laravel to use the "password_reminders" table, which is located in app/config/database.php:
'local' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'yourdb',
'username' => 'yourusername',
'password' => 'yourpassword',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
Within the app/config/mail.php:
'driver' => 'smtp',
'host' => 'localhost',
'port' => 25,
'from' => array('address' => 'service#yourdomain.com', 'name' => 'Your Company'),
'encryption' => '',
'username' => null,
'password' => null,
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
I still need to figure out how to get the redirects and messages to display, but this is working for the emailing a link to reset the password:
public function request()
{
$message = "";
$session = null;
$request = array('email' => Input::get('USER_EMAIL'));
Password::remind(Input::only('USER_EMAIL'), function($message)
{
$message->subject('Password Reminder');
});
if ($request == 'reminders.sent') {
$session = 'message';
$success = true;
$message = 'Email with further instruction has been sent to "' . Input::get('USER_EMAIL'). '".';
return Password::remind($request);
} elseif ($request == 'reminders.user') {
$session = 'error';
$success = false;
$message = 'Email Address: ' . Input::get('USER_EMAIL') . ' WAS NOT FOUND!';
return Password::remind($request);
}else{
$message = 'Not meeting either condition "' . Input::get('USER_EMAIL') . '".';
return Password::remind($request);
}
Session::flash($session, $message);
return Redirect::to('/').with($session, $message);
}
Here are my routes related to password remind:
Route::get('password/reset', array(
'uses' => 'PasswordController#remind',
'as' => 'password.remind'
));
Route::post('password/reset', array(
'uses' => 'PasswordController#request',
'as' => 'password.request'
));
Route::get('password/reset/{token}', array(
'uses' => 'PasswordController#reset',
'as' => 'password.reset'
));
Route::post('password/reset/{token}', array(
'uses' => 'PasswordController#update',
'as' => 'password.update'
));
Maybe it is trying to send an e-mail and it is not working.

Timeout sending email with Cakemail

I'm trying to send an email using CakeMail.
I'm using Wamp Server, ok?
My email.php is this:
public $gmail = array(
'host' => 'ssl://smtp.gmail.com',
'from' => array('support#xxx.com' => 'xxx'),
'port' => 465,
'transport' => 'Smtp',
'username' => 'igorpoxxx#gmail.com',
'password' => 'xxx',
);
In My controller:
App::uses('CakeEmail', 'Network/Email');
$Email = new CakeEmail('gmail');
$Email->to('igor.ces#sotreq.com');
$Email->subject('About');
$Email->send('My message');
And I get Timeout!!!
Fatal Error
Error: Maximum execution time of 30 seconds exceeded
File: C:\wamp\www\societario\lib\Cake\Network\CakeSocket.php
Line: 190
Any help, please?
Solved.
I tested the application in another network and this work fine. Although the first network having Internet for some reason it's don't work.
Was some kind of network problem.
Thanks

cakephp 2.0 smtp email

I am trying to send a email message using CakePhp 2.0. in my controller i use this code (i know it's fine, i took it from the cookbook) :
App::uses('CakeEmail', 'Network/Email');
$email = new CakeEmail("myConfig");
$email->from(array('from#example.com' => 'From Example'));
$email->to($to);
$email->subject($msgtitle);
$ok = $email->send($content);
and in app/config/email.php i have this config :
<?php
class EmailConfig {
public $myConfig = array(
'host' => 'mail.myServer.com',
'port' => 587,
'username' => 'mYaccount',
'password' => 'secret',
'transport' => 'Smtp'
);
}
?>
the problem is the server answers with :
SMTP Error: 530 5.7.0 Must issue a STARTTLS command first.
the account name is correct, as is the password. The config works when loading it up in thunderbird, the connection to the smtp server is set up as :
server name : mail.myServer.com
port : 587
connection security : STARTTLS
authentication : normal password
user name : mYaccount
Are you certain your SMTP supports tls? Try sending the ehlo command:
telnet 1.2.3.4 25
ehlo testing
You should see something like:
250-STARTTLS
in the list.
If you see it, then it is most likely not enabled. You will need to enable it. If you do not see it, you will need to add it.
public $smtp = array(
.................,
'tls' => true
);
Below code is working for me over GoDaddy server using CakePHP SMTP Email:
Email.php file inside config folder - CakePHP 2.4 MVC version:
// for Live Server GoDaddy.com domain
public $smtp = array(
'transport' => 'Smtp',
'host' => 'ssl://smtpout.asia.secureserver.net', <-- important
'port' => 465, <-- important
#'timeout' => 30,
'username' => 'no-reply#godaddy-domain.com',
'password' => 'password',
#'tls' => false,
#'log' => false,
'charset' => 'utf-8',
'headerCharset' => 'utf-8',
);
And here is the controller file code below:
// Controller Code to Send Actual Email
// email configuration
$Email = new CakeEmail('smtp');
$Email->from(array('no-reply#godaddy-domain.com' => 'App Name'))
->sender('no-reply#godaddy-domain.com', 'App Name')
->to(array($email))
->bcc(array('xyz#xyz.com'))
->subject('Test Email from GoDaddy')
->emailFormat('both')
->send($hash.'<br><strong>My</strong> message 45 قبل الميلاد، مما يجعله أكثر من');
Hope it helps !
Thanks
From the CakePHP Cookbook:
You can configure SSL SMTP servers, like GMail. To do so, put the 'ssl://' at prefix in the host and configure the port value accordingly. Example:
class EmailConfig {
public $gmail = array(
'host' => 'ssl://smtp.gmail.com',
(...)
Give the following a try:
<?php
class EmailConfig {
public $myConfig = array(
'host' => 'ssl://mail.myServer.com',
'port' => 465,
'username' => 'mYaccount',
'password' => 'secret',
'transport' => 'Smtp'
);
}
?>
Make sure your
php_openssl.dll
extension is running.
You can check it on thephp.ini file.
If you are using XAMPP php.ini should be on C:\xampp\php
php.ini:
;extension=php_oci8.dll ; Use with Oracle 10gR2 Instant Client
;extension=php_oci8_11g.dll ; Use with Oracle 11gR2 Instant Client
extension=php_openssl.dll
;extension=php_pdo_firebird.dll

Categories