How do i send email through Codeigniter on some event? - php

I am using Codeigniter. In a User management Module i want to send an email to user if status is activated. My Email code is working but there is problem with my condition check whether his status is changed or not. I mean i want to send email only if his status is changed to Active.
Below Is My Controller Code:
if($_POST['status'] == 'active')
{
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'Myhost',
'smtp_port' => 25,
'smtp_user' => 'user',
'smtp_pass' => 'pass',
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from('test#test.com', 'Rajan');
$this->email->to($_POST['email']);
$this->email->subject('Your Account Has Been SuccessFully Activated.');
$this->email->message('Hi, We have created your Account. Please Login ');
$this->email->send();
if ($this->email->send())
{
echo"Success";
}
else
{
echo '<p class="error_msg">That Email And Password Combination Does Not Exist !</p>';
}
}
When i edit a user and change his status it gets saved in database but the email is not fired. Please help me to solve this bug.

Please try with this if($_POST['status'] == 'Active') since your status post value is "Active" (according to our discussion). The double equal sign (==) is case sensitive when used to compare strings in PHP.

Related

How to fix "Undefined property: Register::$encrypt" in PHP codeigniter framework

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!

Codeigniter 3 SMTP with Office 365

I am having difficulties sending email via SMTP using CI3 and Office 365.
My website is hosted with BT.
I found this sample code which I am using, it's the minimum test code for successful email sending over SMTP with Office 365.
In my contact controller I have the following code;
class Contact extends CI_Controller {
public function test(){
$config = [
'protocol' => 'mail',
'smtp_host' => 'smtp.office365.com',
'smtp_user' => 'MY_EMAIL',
'smtp_pass' => 'MY_PASSWORD',
'smtp_crypto' => 'tls',
'newline' => "\r\n",
'smtp_port' => 587,
'mailtype' => 'html'
];
$this->load->library('email', $config);
$this->email->from('MY_FROM_EMAIL');
$this->email->to('MY_TO_EMAIL');
$this->email->subject('Test');
$this->email->message('SMTP sending test');
$sent = $this->email->send();
if ($sent) {
echo 'OK';
} else {
echo $this->email->print_debugger();
}
}
}
When I visit my URL example.com/contact/test I see the following error;
A PHP Error was encountered
Severity: Warning
Message: fsockopen(): unable to connect to smtp.office365.com:587
(Connection timed out)
Filename: libraries/Email.php
Line Number: 1990
Backtrace:
File: .../controllers/Contact.php Line: 118 Function: send
File: ../index.php Line: 341 Function: require_once
Then below that;
The following SMTP error was encountered: 110 Connection timed out
Unable to send email using PHP SMTP. Your server might not be
configured to send mail using this method.
User-Agent: CodeIgniter
Date: Tue, 14 Nov 2017 15:08:27 +0000
etc ...
etc ...
etc ...
I know the login details are correct for the email account as I am able to login and send/receive emails using various email clients.
Any ideas why this is happening, and how I can fix it? Are there any other tests I can carry put in order to help troubleshoot the issue.
I've been stuck at this for days. I don't want to use gmail or any other email provider.

CakePHP Paypal IPN Plugin email not working

I'm using this plugin in a CakePHP Application. Everything seems to work except sending emails.
I have the following code in AppController.php
function afterPaypalNotification($txnId)
{
//Here is where you can implement code to apply the transaction to your app.
//for example, you could now mark an order as paid, a subscription, or give the user premium access.
//retrieve the transaction using the txnId passed and apply whatever logic your site needs.
$transaction = ClassRegistry::init('PaypalIpn.InstantPaymentNotification')->findById($txnId);
$this->log($transaction['InstantPaymentNotification']['id'], 'paypal');
//Tip: be sure to check the payment_status is complete because failure
// are also saved to your database for review.
if ($transaction['InstantPaymentNotification']['payment_status'] == 'Completed')
{
//Yay! We have monies!
ClassRegistry::init('PaypalIpn.InstantPaymentNotification')->email(array(
'id' => $txnId,
'subject' => 'Thanks!',
'message' => 'Thank you for the transaction!'
));
}
else
{
//Oh no, better look at this transaction to determine what to do; like email a decline letter.
ClassRegistry::init('PaypalIpn.InstantPaymentNotification')->email(array(
'id' => $txnId,
'subject' => 'Failed!',
'message' => 'Please review your transaction'
));
}
}
But the data returned from Paypal is saved in the instant_payment_notifications table but for some reason the emails are not sent. Has anybody tried this plugin before and did the email fonctionality work?
Do I need to enable email.php in app/Config for the emails to work? I read somewhere on Cake's website that I don't need that file for emails to work, so I guess that's not where the problem is.
Any help will be appreciated.
Thanks.
In CakePhp 2.5 you should use CakeEmail
Create the file /app/Config/email.php with the class EmailConfig. The /app/Config/email.php.default has an example of this file.
Add following line in /app/Controller/AppController.php before class declaration
App::uses('CakeEmail', 'Network/Email');
In afterPaypalNotification function replace
ClassRegistry::init('PaypalIpn.InstantPaymentNotification')->email(array(
'id' => $txnId,
'subject' => 'Thanks!',
'message' => 'Thank you for the transaction!'
));
with (fast e-mail, no style)
CakeEmail::deliver('customer#example.com', 'Thanks!', 'Thank you for the transaction!', array('from' => 'you#example.com'));
or
$Email = new CakeEmail();
$Email->template('email_template', 'email_layout')
->emailFormat('html')
->to('customer#example.com')
->from('you#domain.com')
->viewVars(array('id' => $txnId))
->send();
Email template goes to /app/View/Emails/html/email_template.ctp
Email layouts goes to /app/View/Layouts/Emails/html/email_layout.ctp

My Mails Is Always Going To Spam Folder Even Delivering Very Late In Codeigniter Php

I'm Trying To Send Mails From My Web Server Using SMTP PROTOCOL in codeigniter frame work.my mails was always delivering late and going to spam.i had follow many past conservations,still i didn't solve this problem.help me folks to solve this problem.here is my code:
$config = Array('protocol' => 'smtp',
'smtp_host' => 'http://smtp.tfas.net/',
'smtp_port' => 465,
'smtp_user' => 'info#tfas.net',
'smtp_pass' => 'xxxxxxx',
'mailtype' => 'html',
'mailpath' => '/usr/sbin/sendmail',
'charset' => 'utf-8'
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
if (file_exists($attachment_path))
{
$this->email->attach("$attachment_path");
$this->email->from('info#tfas.net','TFAS MEMBER PORTAL');
$this->email->reply_to('info#tfas.net','Help Center');
$this->email->message($message);
$this->email->to($to);
$this->email->subject('Greetings From TFAS Member Portal');
}
else
{
$this->email->from('info#tfas.net','TFAS MEMBER PORTAL');
$this->email->reply_to('info#tfas.net','Help Center');
$this->email->message($message);
$this->email->to($to);
$this->email->subject('Greetings From TFAS Member Portal');
}
if($this->email->send())
{
return true;
}
else
{
return false;
}
// Even I had gone through this link https://github.com/ivantcholakov/codeigniter-phpmailer which is a tutorial to send mails with phpmailer library in codeigniter but same problem araises..
I don't think this is an issue with codeigniter's mailer. Usually, emails are marked as spam by email service providers based on the content or an IP filter. In your case it's possible that your content appear spam-like due to not having relevant text and images. Google and most providers has a list of spam words that they look out for when evaluating the spam-ness of your emails. Check this out as it may be help you to find a useful lead: http://blog.hubspot.com/blog/tabid/6307/bid/30684/The-Ultimate-List-of-Email-SPAM-Trigger-Words.aspx

Accept email address from user and send email to that address

public function send_mail() {
$this->email->from('your#email.com', 'My Name');
$this->email->to('to#email.com');
$this->email->cc('cc#email.com');
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');
$this->email->send();
echo $this->email->print_debugger();
}
i'm trying out the codeigniter email func but i cant seem to receive the test mail, but it saids that it has successfully send the mail but there's none,
new to ci so i'm confused about this problem, $this->load->library('email'); is also loaded,
anyhelp thanks!
EDIT
ok guys thanks for the answer i have added the ff code and it now works,
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'your email',
'smtp_pass' => 'your password'
);
Now my question is how will I edit the codes above to accept the "to" email from the user?
okay i did the "to" part already, and its like this
$this->email->to($point->email);
need help again with the "message" part, i need to put data from the user in the "message" part, i tried what i did in the "to" part but it only accepts one item, i need multiple items, thanks
In the section where you're calling the function send_mail(), pass the email address and other data as well, as arguments to the function; something like this:
$to = $point->email;
$msg = $point->message; // the message part
$phone = $point->phone; // phone number
$address = $point->address; // postal address
$this->send_email($to, $msg, $phone, $address);
And then slightly modify the send_mail() function as:
// $to_address will contain the value stored in $to, i.e. "abc#def.com" in this case
public function send_mail($to_address, $body_message, $phone_no, $addr) {
// can make use of extra data like phone number, address in here
$this->email->from('your#email.com', 'My Name');
$this->email->to($to_address);
$this->email->message($body_message);
.
.
.
}
It works fine in my linux server without any $config.
Please read this to set another protocol

Categories