Cakephp 3 email with SMTP not working - php

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!

Related

Zend smtp mailJet template

I'm trying to send email on my zend app Via mailjet.
It works but cannot load the desired template.
Code:
$config = array('ssl' => 'ssl',
'port' => 465,
'auth' => 'login',
'username' => 'mailjet api username',
'password' => '8mailjet api key');
$transport = new Zend_Mail_Transport_Smtp('in-v3.mailjet.com', $config);
$mail = new Zend_Mail();
$mail->addHeader('X-MJ-TemplateLanguage',true);
$mail->addHeader('X-MJ-TemplateID','validationV2');
// This is the template I wanna use.(above)
$mail->setFrom('test#test.org', 'You');
$mail->addTo('test#test.org', 'Anybody');
$mail->setSubject('My first email by Mailjet');
$mail->setBodyHtml('wxxxxxxxxxxxxxxxxxxxxxxxxx');
$mail->send($transport);
If I could get any help woudl be great!
You could try putting the numerical ID of the template in X-MJ-TemplateID. If it still doesn't work, file a support ticket
I encountered the same problem. It could be that Mailjet v3 for SMTP Relay is not Zend compatible.
However, the workaround I have tested is to use directly the Mailjet API after importing 'mailjet/mailjet-apiv3-php' via composer, like so :
$mail = [
'FromEmail' => 'test#test.org',
'FromName' => 'You',
'To' => 'Anybody <test#test.org>,
'MJ-TemplateID' => 1,
'MJ-TemplateLanguage' => true
];
$mj = new Client('mailjet api username', '8mailjet api key');
$mj->post(Resources::$Email, ['body' => $mail]);
I'm curious what Mailjet's answer was for your support ticket, however!

How to conditionally change mail transporter in laravel 5?

I use laravel 5.3.
I need to send mail with different credentials (host,port,username, password).
I can send with default laravel config(.env).
But i need dynamic level implementation.
I make array of config,
// Pre-Mail Setup Config.
$store_config = [
'list' =>
//SET 1
['from_name' => 'sender1',
'from_address' => 'from_adderss1',
'return_address' => 'reply1',
'subject' => 'subject1',
'host' => 'host1',
'port' => 'post1',
'authentication' => 'auth1',
'username' => 'uname1',
'password' => 'pass1'],
//SET 2
[.........],
//SET 3
[.........]
];
I try the following to send mail, but it won't work.
// Inside Foreach.
$transporter = \Swift_MailTransport::newInstance('smtp.gmail.com', 465, 'ssl')
->setUsername($config['username'])
->setPassword($config['password']);
$mailer = \Swift_Mailer::newInstance($transporter);
$message->from($config['from_address'], $config['from_name']);
$message->to('To_Email, 'Name')
->subject('My Subject')
->setBody('My Content', 'text/html');
$mailer->send($message);
What's wrong with my code?
Is it possible?
Or any other solution?
Finally i find the way to solve this.
Actually Laravel 5 is not fully support this multi transporter config.
so i use alternative package to achieve it.
My Code is ,
foreach ($store_configs['list'] as $store_config) {
// Create Custom Mailer Instances.
$mailer = new \YOzaz\LaravelSwiftmailer\Mailer();
$transport = \Swift_SmtpTransport::newInstance(
$store_config['host'],
$store_config['port'],
$store_config['authentication']);
// Assign Dynamic Username.
$transport->setUsername($store_config['username']);
// Assign Dynamic Password.
$transport->setPassword($store_config['password']);
$smtp = new \Swift_Mailer($transport);
$mailer->setSwiftMailer($smtp);
$mailer->send('template', ['data'], function ($message) use ($queue) {
// Default Response goes here
$message->from('From Address', 'From Name');
$message->to($email, 'Name')->subject('My Subject')
->setBody('My HTML', 'text/html');
$message->getSwiftMessage();
//
});
}
Its works fine with multiple and dynamic transporter.
Thanks to All !

Override CakeEmail destination globally

Is there an easy way to "override" the destination of an email using CakeEmail?
I have a model method that send an email using this piece of code:
$Email = new CakeEmail();
$Email->config('default');
$sent = $Email->template('new-notification')
->subject('A new notification for you')
->viewVars([
'name' => $foo['User']['name'],
'text' => $foo['Notification']['text'],
)
->emailFormat('html')
->to($foo['User']['email'])
->send();
And this configuration:
class EmailConfig
{
public $default = array(
'host' => 'smtp.server.com',
'port' => 587,
'username' => 'user#domain.com',
'password' => 'pwdAsYouKnow',
'from' => array('noreply#domain.com' => 'Company'),
'transport' => 'Smtp'
);
}
As you can see, I send the email for a dynamically defined user's email.
My objective is when I'm developing locally on my machine, to force every call to ->send() to force a destination like developer#domain.com.
I can easily detect if I'm on development, but I don't know how to force in a "master" way to CakeEmail only send to a defined account overriding the one set on the call.
I already tried to set an
'to' => array('developer#domain.com' => 'Dev'),
inside $default but, no success.
I appreciate any help, thanks!
i'm assuming when you are on local machine you run in debug mode, so you can check if debug mode is on then use that to send to different email
if(Configure::read('debug')){
$emailTo = 'developer#domain.com'
}
else{
$emailTo = $foo['User']['email'];
}
then you just use variable for email address:
$sent = $Email->template('new-notification')
->subject('A new notification for you')
->viewVars([
'name' => $foo['User']['name'],
'text' => $foo['Notification']['text'],
)
->emailFormat('html')
->to($emailTo)
->send();

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.

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