Cakephp 3 Cake\Network\Email configTransport Issue - email

I'm using Cake\Network\Email\Email class to send some emails.
I have successfully send an email with my smtp configuration that I put on config/app.php
The problem is that I don't want the config to be there.
I have this piece of code:
function enviacorreo($email, $asunto, $content, $cc=null) {
Email::configTransport('gmail', [
'port'=>$smtpport,
'timeout'=>'30',
'host' => $smtp,
'username'=>$smtpuser,
'password'=>$smtppasswd
]);
$email_obj = new Email();
$email_obj->template('default')
->emailFormat('html')
->to($email)
->from([$smtpuser => $nombreSistema])
->subject($asunto)
->transport('gmail');
if($email_obj->send($content))
return array('exito'=>1,'error'=>'Ninguno');
else
return array('exito'=>0,'error'=>'El correo no pudo ser enviado');
}
I'm following the official book here
The error that I get is this:
Transport config "gmail" is missing.
I know that ->transport('gmail'); is looking for a key inside the EmailTransport array definded in app.php
But. How can I have this 'configuration' in my code?
Hope to explained well.

The error message is a little misleading, as the exception is not only being thrown in case the configuration missing, but also in case the className option is missing, which is the case in your example (#7204).
If you look closely at the docs, you should see that you haven't followed it correctly.
[...]
// Sample smtp configuration.
Email::configTransport('gmail', [
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'username' => 'my#gmail.com',
'password' => 'secret',
'className' => 'Smtp' // <------ there it is
]);
[...]
Cookbook > Email > Configuring Transports

Related

SMTP server did not accept the password

I had written following code to send email.
static function sendEmail($email,$data,$type){
$Email = new CakeEmail();
$Email->config('general');
switch($type){
case 1:
$Email->template('confirmation_free', null);
$Email->subject('Confirmation of registration with XXXXXXXXXXXXX');
$Email->viewVars(array('Email'=>$data["Email"],'full_name'=>$data['full_name'],'Id'=>$data['Id'],'url'=>$_SERVER['SERVER_NAME'], 'password'=>$data['password']));
break;
case 2:
$Email->template('group-invite', 'default');
$Email->subject('XXXXXXXX Group Invite - Notification');
$Email->viewVars(array('Email'=>$data["Email"],'Username'=>$data['Username'],'Id'=>$data['Id'],'url'=>$_SERVER['SERVER_NAME']));
break;
case 3:
$Email->template('forgot_password', null);
$Email->subject('XXXXXXXX - Forgot Password');
$Email->viewVars(array('Email'=>$data["Email"],'Key'=>$data['Key'],'url'=>$_SERVER['SERVER_NAME'],'Id'=>$data['id']));
break;
}
$Email->to($email);
if($Email->send())
return true;
else
return false;
}
with following sendgrid smtp settings.
public $general = array(
'transport' => 'Smtp',
'from' => array('XXXXX#XXXXXXX' => 'XXXXXX Administrator'),
'host' => 'smtp.sendgrid.net',
'port' => 587,
'timeout' => 30,
'username' => 'XXXXXXX',
'password' => 'XXXXXX',
'client' => null,
'log' => false,
'emailFormat' => 'html'
);
It was working perfectly fine on my local and dev server. But after we installed SSL on the dev server, it started throwing following error "SMTP server did not accept the password "
Please note that I'm using a sendgrid free account. Do I need a paid account to send emails from a server with SSL?
You need to either use the tls option in your CakeEmail config or prefix the host with ssl:// https://book.cakephp.org/2.0/en/core-utility-libraries/email.html
Xylon, Please try this.
In Email.php
public $smtp = array(
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'username' => 'your email',
'password' => 'password',
'transport' => 'Smtp',
'log' => true,
'auth' => true,
'charset' => 'utf-8',
'headerCharset' => 'utf-8',
);
$Email = new CakeEmail('smtp'); // In Controller where you want send mail
$Email->viewVars(array("data" => $data));
$Email->template($template)
->emailFormat('html')
->to($reciever)
->from(array($mail_from => "Ecotrak"))
->subject($subject)
->send();
I hope this will resolve problem.
Email could not be sent: SMTP server did not accept the password. See trace
If you are facing that issue and your check everything is fine there is no issue anywhere but you still facing that issue.
Just do this simple process.
Go to your Google Account.
Select Security.
Go down
Click on Less Secure app access
Turn it on
Now check it... your issue will resolve

smtp not working on 1and1

I'm using 1and1 server to host my CakePHP 3.2 application
This is how, I have configured email component on CakePHP
'EmailTransport' => [
'default' => [
'className' => 'Smtp',
// The following keys are used in SMTP transports
'host' => 'smtp.1and1.com',
'port' => 587,
'timeout' => 30,
'username' => 'noreply#mywebsite.com',
'password' => 'password',
'client' => null,
'tls' => null,
'url' => env('EMAIL_TRANSPORT_DEFAULT_URL', null),
],
],
But this is not working and no email is sent and gives Connection Time out after 30 seconds.
From 1and1 email configuration page. It says
Outgoing port (TLS must be activated)
How to enable/configure TLS in CakePHP ?
Edit 2
the error stack shows
SmtpTransport.php
protected function _connect()
{
$this->_generateSocket();
if (!$this->_socket->connect()) { // marked this line
throw new SocketException('Unable to connect to SMTP server.');
}
$this->_smtpSend(null, '220');
Action to send email
public function sendEmail($user_id = null, $email_id = null, $hash = null, $request = null)
{
switch($request) {
case 'register' : $subject = 'Account Confirmation';
$message = 'You have successfully registered. Click below link to verify it http://website.com/sellers/verify/'.$email_id.'/'.$hash;
break;
}
$email = new Email('default');
if ($email->from(['anujsharma9196#gmail.com' => 'Argo Systems'])
->to((string)$email_id)
->subject($subject)
->send($message)) {
return true;
} else {
return false;
}
}
and calling this function from same controller by
$this->sendEmail($user->id, $user->email, $hash, 'register');
from the manual (bold is mine)
You can configure SSL SMTP servers, like Gmail. To do so, put the ssl:// prefix in the host and configure the port value accordingly. You can also enable TLS SMTP using the tls option:
so just set
'tls' => true
in your configuration array and try if it works
Edit
following this page I found that you don't even need to use the Smtp transporter
just use a simple Mail transporter this way
'default' => [
'className' => 'Mail'
]
There is no need to supply a username, password, or specify which mail server should be used to send the mail since this information is already contained in PHP variables.
try that!
I used below configuration in system for mail using gmail. it work fine. you just try with your server.
'EmailTransport' => [
'default' => [
'className' => 'SMTP',
// The following keys are used in SMTP transports
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'timeout' => 100,
'username' => 'example#gmail.com',
'password' => 'exmple123',
'url' => env('EMAIL_TRANSPORT_DEFAULT_URL', null),
],
],
Check with your port and host.Hope fully it will works
Try
Thanks

Laravel sending E-Mail does not work

I am trying to sent emails with laravel but somehow it does not work I get following message:
Swift_TransportException in AbstractSmtpTransport.php line 383:
Expected response code 220 but got code "421", with message "421
Cannot connect to SMTP server 52.20.34.166 (52.20.34.166:587), connect
error 10060 "
my .env file looks like this:
APP_ENV=local
APP_DEBUG=true
APP_KEY=rFhHPTfEg4nxYEeqVzoQWX70a0AA5uw3
DB_HOST=localhost
DB_DATABASE=test
DB_USERNAME=root
DB_PASSWORD=
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=587
MAIL_USERNAME=test#test.de
MAIL_PASSWORD=123456789
MAIL_ENCRYPTION=tls
my mail.php looks like this:
<?php
return [
'driver' => env('MAIL_DRIVER', 'mail'),
'host' => env('MAIL_HOST', ''),
'port' => env('MAIL_PORT', 587),
'from' => ['address' => 'test#test.de', 'name' => 'test'],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
];
And inside of my Controlller I called it like this:
# Send Mail
Mail::send('emails.email', $data, function($message) use ($data)
{
$message->from($data['email'] , $data['title']);
$message->to('test#test.de', 'my name')->subject('contact request');
});
I cannot find the mistake in my code. Thanks for help.
Other thing I notices that mailtrap gives you specific username and password. Did you try to use them instead of test#test.de;123456789 ?
You need put a valid email on .env, find your personal mail configuration and try again with this.
Maybe you want to change the config from on config/mail.php to show your name and mail.
I say this because I see that you have the host mailtrap.io, but test#test.de is not a valid user for this host.
And maybe 587 to the MAIL_PORT is not the correct value.
You need to use this command:
php artisan config:cache
It works for it before your changed env file and type this command and run it. It will work.

Sending Mail using CakePHP 3.0

I am developing a website with new version 3.0 of CakePHP Framework. I am working on localhost and would like to send an email after a user has filled a form. Below is the code in my controller to send the email.
public function index(){
if ($this->request->is('post'){
$email = new Email();
$email->from([$this->request->data["sender"] => "Sender"]
->to("myEmail#hotmail.com")
->subject($this->request->data["Subject"])
->send($this->request->data["message"]);
}
}
When this code is executed nothing happen, no error, no message in my mailbox. I have seen that it exist in cakephp3.0 a class called DebugTransport but I don't know how to use it in order to debug my code. Someone has already use it ?
Hi thanks everyone for your answer.
By using mailjet.com I was able to send e-mails in localhost. Below the different steps:
Step 1
Create an account on mailjet website
Step 2
In app.php add a new entry in the table EmailTransport. The different parameter host, port, username and password can be found on mailjet website.
'EmailTransport' => [
'default' => [
'className' => 'Mail',
// The following keys are used in SMTP transports
'host' => 'localhost',
'port' => 25,
'timeout' => 30,
'username' => 'user',
'password' => 'secret',
'client' => null,
'tls' => null,
],
'mailjet' => [
'host' => 'in-v3.mailjet.com',
'port' => 587,
'timeout' => 60,
'username' => 'xxxxx',
'password' => 'xxxxx',
'className' => 'Smtp'
]
],
Step 3
In your controller
<?php
namespace App\Controller;
use App\Controller\AppController;
use Cake\Network\Email\Email;
class ContactController extends AppController {
var $helpers = array('Html');
public function index(){
if($this->request->is('post')){
$userName = $this->request->data['firstname'] . " " . $this->request->data['lastname'];
$email = new Email();
$email->transport('mailjet');
try {
$res = $email->from([$this->request->data['email'] => $userName])
->to(['myEmail#hotmail.com' => 'My Website'])
->subject('Contact')
->send($this->request->data['message']);
} catch (Exception $e) {
echo 'Exception : ', $e->getMessage(), "\n";
}
}
}
You had to use a SMTP server for delivery your email from your localhost in your config email.
There are 2 ways to achieve this:
Use it from a real server with a mail configuration
Use SMTP server for your test on your localhost. there are a lot of SMTP server with let you use it.
see mailjet.com

CakePHP SMTP EMail Not Working on Server

I am able to send SMTP Emails from my local server machine in CakePHP while I am not able to do the same on my GoDaddy live server, in CakePHP.
Any ideas for the same?
Thanks
Answer is below as per my experience with GoDaddy:
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
Update your code to check for an error message:
if(!$this->Email->send()) {
CakeLog::write('debug', $this->Email->smtpError);
}
Then check the /app/tmp/logs/debug file on the server.