Sending Mail using CakePHP 3.0 - email

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

Related

How to send an email to the user when performing login in cakephp

I do not have any knowledge about cakephp mail so explain the solution briefly I mean what to do and how to do from the beginning.
From the cakephp official side I just used this "use Cake\Mailer\Email;" and then the mail function but a error message shows like as shown below
Could not send email: mail(): Failed to connect to mailserver at
quot;server.com" port 25, verify your "SMTP" and
quot;smtp_port" setting in php.ini or use ini_set()
MY users controller login function
public function login() {
$this->viewBuilder()->setLayout('');
if ($this->request->is('post')) {
$data = $this->request->getData();
$query = $this->Users->find()->where(['email' => $data['email'], 'password' => md5($data['password'])]);
if ($query->count()) {
$user = $query->first()->toArray();
$this->Auth->setUser($user);
//FOR MAIL START
ini_set('SMTP', "server.com");
ini_set('smtp_port', "25");
ini_set('sendmail_from', "restrange5#gmail.com");
$email = new Email('default');
$email->setFrom(['restrange5#gmail.com' => 'My Site'])
->setTo('ramakantasahoo835#gmail.com')
->setSubject('About')
->send('My message');
//FOR MAIL END
$this->Flash->success(_('Login Successfull'));
$this->redirect(['action' => 'dashboard']);
} else {
$this->Flash->error(__('Username/Password not found!!'));
return $this->redirect($this->referer());
}
}
}
How much I know as I have just changed in users controller only. What else I have do please suggest.
**Your cofiguration in app.php file is something like this **'
EmailTransport' => [
'default' => [
'className' => 'Smtp',
// The following keys are used in SMTP transports
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'timeout' => 30,
'username' => 'email here',
'password' => 'password here',
'client' => null,
'tls' => null,
'url' => env('EMAIL_TRANSPORT_DEFAULT_URL', null),
],
]

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

cakephp email issue with some domains

I have cakephp application, and it has no domain name we are accessing using ip.
I am using the following smpt setting to send email
class EmailConfig {
public $smtp = array(
'transport' => 'Smtp',
'from' => array('myemail#otherdomain.com' => 'my name'),
'host' => 'mail.otherdomain.com',//not the same domain from which sending email
'port' => 25,
'timeout' => 30,
'username' => 'myemail#otherdomain.com',
'password' => 'password',
'client' => null,
'log' => false,
//'charset' => 'utf-8',
//'headerCharset' => 'utf-8',
);
}
all emails are being sent to any_email # gmail, otherdomain etc
but there is only one specific govt domain(abc.gov.com) who are not receiving email
they may have implemented some security measures, which may prevent my emails to them, what things can I try at my side and how can I debug to get exact error.
I tried to send email where
to = email#abc.gov.com
cc = email#otherdomain.com
the same email is being received by email#otherdomain.com while not by email#abc.gov.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.