I'm pretty new to CakePHP and this is my first attempt setting up an email form.
Keeping the example simple:
<?php
App::uses('AppController', 'Controller');
App::uses('CakeEmail', 'Network/Email');
class EmailController extends AppController {
public function send_email($from, $subject, $message) {
$Email = new CakeEmail();
$Email->from($from)
->to('[my personal email]')
->subject($subject);
if($Email->send($message)) {
$result = 'Your email has been sent.';
} else {
$result = 'Your email failed to send.';
}
$this->set('result', $result);
$this->set('params', '('.$from.'|'.$subject.'|'.$message.')');
}
}
send_email.ctp
<?php echo $result;?>
<br>
<?php echo $params;?>
I'm getting "Your email has been sent.", the $params look as I expect, and I am not seeing any errors... but I'm not getting the email. Any idea why this might happen?
Before this you need to define Email configuration in email.php under Config folder
Here we have gmail configuration for example
class EmailConfig {
public $gmail = array(
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'username' => 'username#gmail.com',
'password' => '*****',
'transport' => 'Smtp'
);
}
then you can use this setting in controller like
$Email= new CakeEmail('gmail');
Inshort you have to configure SMTP according to requirement. I hope this will be handy for you. Thanks
Related
When I am submitting my form in CodeIgniter, I get this error Message: Undefined property: Register::$encrypt. I want to hash the password that's the reason I used the encrypt.
I have tried to include encrypt library in autoload.php but still another error pops up.
this is where the error is popping up.
function validation(){
$this->form_validation->set_rules('user_name','Name','required|trim');
$this->form_validation->set_rules('user_email','Email Address','required|trim|valid_email|is_unique[codeigniter_register.email]');
$this->form_validation->set_rules('user_password','Password','required|trim');
if($this->form_validation->run()){
$verification_key=md5(rand());
$encrypted_password = $this->encrypt->encode($this->input->post('user_password'));
$data = array(
'name' => $this->input->post('user_name'),
'email' => $this->input->post('user_email'),
'password' => $encrypted_password,
'verification_key' => $verification_key
);
$id=$this->register_model->insert($data);
if($id > 0){
$subject='Please verify email for login';
$message="
<p>Hi".$this->input->post('user_name')."</p>
<p>Verify your email for login to this system. Click this <a href='".base_url()."register/verify_email/".$verification_key."'>link</a>.</p>
<p>Use this link to log in in to this system.</p>
<p>Thanks You.</p>
";
$config = array(
'protocol' => 'smtp',
'smtp_host' => 'smtpout.secureserver.net',
'smtp_port' => 80,
'smtp_user' => 'root',
'smtp_pass' => 'root',
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'wordwrap' =>TRUE
);
$this->load->library('email',$config);
$this->email->set_newline("\r\n");
$this->email->from('info#icode.info');
$this->email->to($this->input->post('user_email'));
$this->email->subject($subject);
$this->email->message($message);
if($this->email->send()){
$this->session->set_flashdata('message','Check in your email for verification mail');
redirect('register');
}
}
}
else{
$this->index();
}
I expected after submitting the form to give an alert or send the data I have filled in my form to the database,
Load the encryption libarary
$this->load->library('encryption');
$encrypted_password = $this->encrypt->encode($this->input->post('user_password'));
https://www.codeigniter.com/user_guide/libraries/encryption.html
Once loaded, the Encryption library object will be available using:
$this->encryption
Update:
As you mentioned your loading it in the constructor I took another glance at it:
It looks like a spelling error
$this->load->library('encryption');
...
$this->encrypt->encode($this->input->post('user_password'));
instead of
$this->load->library('encryption');
...
$this->encryption->encode($this->input->post('user_password'));
Also in the documentation it's presented this way
$ciphertext = $this->encryption->encrypt($plain_text);
Simple mistake, took me a few minutes of looking at it too see it too. I never type things out if I can avoid it, I copy/paste everything. Mostly that is because my spelling and stuff is bad (Dyslexia) but it can avoid some issues like this... lol
Glad we got it sorted for you!
I make API for send email using cakephp. This is my code:
App::uses('CakeEmail', 'Network/Email');
$this->autoLayout = false;
$this->autoRender = false;
$data = $this->request->data;
$title = $data['title'];
$content = $data['content'];
$Email = new CakeEmail('smtp');
$Email->from('myemail#gmail.com');
$Email->to($data['email'][0]);
$Email->subject($title);
$Email->send($content);
And it show error php_network_getaddresses: getaddrinfo failed: No address associated with hostname. Please help me in this case
The error message indicates that php cannot communicate with the host hostname - this comes from the configuration for that class:
class EmailConfig {
public $smtp = array(
'host' => 'hostname', // <---
...
);
}
Either it's badly configured, or the domain name does not resolve.
I have a email template onecode.mail.php
Which I call from
$body = $view->render(
'template',
compact('users','oneCode','username'),
array(
'controller' => 'users',
'template'=>'onecode',
'type' => 'mail',
'layout' => false
)
);
$transport = Swift_MailTransport::newInstance();
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance();
$message->setSubject("Sign in password");
$message->setFrom(array(NOREPLY => 'Sign in password'));
$message->setTo($email);
$message->setBody($body,'text/html');
$mailer->send($message);
onecode.mail.php contains
<?=$t('Login Email Password')?>
<?=$oneCode?>
I get an error while processing this request as:
<b>Fatal error</b>: Function name must be a string in <b>app\resources
\tmp\cache\templates
\template_views_users_onecode.mail_0_1460392715_2266.php</b> on line <b>1</b><br/>
Translation works perfect in all the .html.php files but not in the template of .email.php
What should be done? Any suggestions, thanks for the help.
Please add the following lines in your template:
<?php
use lithium\g11n\Message;
extract(Message::aliases());
?>
<?=$t('Login Email Password')?>
You should be able to get the translation in your desired language
I have been trying to get this Email code working in my CakePHP for ages now. It is meant gather data from a simple contact form and send an email to a specific address. For now I am simply trying to get the emailing sending.
When I load the page I get Could not send Email and when I look at the log I get [SocketException] Could not send email.
Also I have used the smtp settings in a different area for a reset password email that does work.
Any help would be greatly appreciated.
Here is my ContactController
<?php
App::uses('AppController', 'Controller');
App::uses('CakeEmail', 'Network/Email');
class ContactController extends AppController {
public function sendEmail()
{
/*$fname = $_POST['first_name'];
$lname = $_POST['last_name'];
$visitor_email = $_POST['email'];
$telephone = $_POST['telephone']; */
// $comments = $_POST['comments'];
$Email = new CakeEmail();
/* SMTP Options */
$Email->smtpOptions = array(
'transport' => 'Smtp',
'port'=>'465',
'timeout'=>'30',
'host' => 'ssl://smtp.gmail.com',
'username'=>'frankstoncsf#gmail.com',
'password'=>'xxxxxxx',
'log'=>true
);
$Email->template = 'resetpw';
$Email->from(array('frankstoncsf#gmail.com' => 'My Site'));
$Email->to('thomas.chambers5#gmail.com');
$Email->subject('Reset Your Frankson.net User Password');
$Email->sendAs = 'both';
$Email->delivery = 'Smtp';
// $Email->set('ms', 'hello');
$Email->send('hello');
set('smtp_errors', $Email->smtpError);
}
public function index() {
$this->sendEmail();
}
}
Why does setReplyTo($reply_to_mail) send email to $reply_to_mail? Shouldn't it just add email adress to reply-to field in the email message?
Currenyly if sending mail from website form and filling reply-to field, message sends to reply-to email and to our admin email.
Why does it duplicates email? Should send only to our admin email.
class Helper_Mail extends Zend_Controller_Action_Helper_Abstract
{
public function direct($email,$from,$message,$title,$replyto='')
{
$this->sendmail($email,$from,$message,$title,$replyto);
}
private function sendMail($email,$from,$message,$title,$replyto)
{
/* Configuring SMTP settings */
$config = array(
'auth' => 'login',
'ssl' => 'tls',
'username' => 'adminmail#gmail.com',
'password' => 'password',
'port' => 587);
$smtpHost = new Zend_Mail_Transport_Smtp('smtp.gmail.com',$config);
Zend_Mail::setDefaultTransport($smtpHost);
$mail = new Zend_Mail('UTF-8');
$mail->setBodyHtml($message);
$mail->setFrom('adminmail#gmail.com', $from);
$mail->addTo($email);
$mail->setSubject($title);
if(!empty($replyto))
{
$mail->setReplyTo($replyto);
}
try
{
$mail->send();
}
catch(Zend_Mail_Exception $e)
{
echo $e->getMessage();
}
}
}
You can use Zend_Mail::setReplyTo() if you are using a version of Zend > 1.8
If not (<= 1.8) you should use Zend_Mail::addHeader('Reply-To', 'replymail#example.com')
It was a bug, fixed in new versions. ;)