CodeIgniter Forums
trouble sending email controller - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: trouble sending email controller (/showthread.php?tid=74530)

Pages: 1 2


trouble sending email controller - pmcr - 10-05-2019

hi 
a bit of a novice, but can anyone give me an idea what might be wrong in this code? 
when submit button is pressed, there is no message or email sent ... 

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class ContactUs extends MY_Controller
{
function __construct()
{
parent::__construct();
//$this->load->model('front/homepage_model');
}

public function index()
{  
if(isset($_POST['submit']) && $_POST['submit'] == 'Contact1')
{
$name          = $this->input->post('name');
            $email        = $this->input->post('email');
            $message      = $this->input->post('msg');
            $subject      = $this->input->post('subject');
            $main_msg =`<b>Name : </b>`.$name.`  <br> <b>Email : </b> `.$email.` <br> Message : `.$message.``;
            $to      = '[email protected]';
            $subject      = 'Mail From [color=#333333][size=small][font=Tahoma, Verdana, Arial, sans-serif]@xxx.com[/font][/size][/color]';
            $headers = "From: sales@[color=#333333][size=small][font=Tahoma, Verdana, Arial, sans-serif]xxx.com[/font][/size][/color].biz \r\n";
            $headers .= "Reply-To: sales[color=#333333][size=small][font=Tahoma, Verdana, Arial, sans-serif]@xxx.com[/font][/size][/color] \r\n";
            $headers .= 'Content-type:text/html; charset=ISO-8859-1\r\n';
            $headers .= 'MIME-Version: 1.0' . "\r\n";
            $mail    = $this->send_mail($to, $subject, $main_msg, $headers,$email);           
            if ($mail)
            {
                $this->session->set_flashdata('message', '<div class="alert alert-danger alert-dismissable fade in" style="color: #f1f1f1; background-color: #f25c27;"><a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a><strong>Thank You</strong> Your order request has been submited successfully, We will notify you soon.</div>');
                redirect(base_url('contactUs'));
            }
            else
            {
                $this->session->set_flashdata('message', '<div style="color: #f1f1f1; background-color: #f25c27;" class="alert alert-success alert-dismissable"><i class="fa fa-check"></i><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button><b> <strong>Sorry!</strong> Try out after some time, we cant process your request for  this time.</b></div>');
                redirect(base_url('contactUs'));
            }
}
  $this->show_view_front('front/contact_us',$this->data);

    }

}
/* End of file */
?>

Thank you

**EDIT: code tags added for readability**


RE: trouble sending email controller - jreklund - 10-06-2019

Hi, What does $this->send_mail do? As the problem lies there.

Do you actually have this in your code, or are that just a copy and paste error?
Code:
[color=#333333][size=small][font=Tahoma, Verdana, Arial, sans-serif]



RE: trouble sending email controller - pmcr - 10-06-2019

(10-06-2019, 02:35 AM)jreklund Wrote: Hi, What does $this->send_mail do? As the problem lies there.

Do you actually have this in your code, or are that just a copy and paste error?
Code:
[color=#333333][size=small][font=Tahoma, Verdana, Arial, sans-serif]

I did change the original code which was also not working. 

$mail=mail($to, $subject, $main_msg, $headers);


RE: trouble sending email controller - jreklund - 10-06-2019

And are mail() configured on the server? If you are using a VPS you need to configure it yourself.


RE: trouble sending email controller - InsiteFX - 10-06-2019

And if you are on localhost you need to configure it in php.ini


RE: trouble sending email controller - pmcr - 10-08-2019

(10-06-2019, 08:04 AM)jreklund Wrote: And are mail() configured on the server? If you are using a VPS you need to configure it yourself.
Not using VPS 
I have another controller where email is sent out without any issue 

I do have set in core/my_controller the following, and the reason why i had used sent_email instead of email 

/* Mail Send */
public function send_mail($email, $subject, $message)
{
$config = array(
'protocol' => 'SMTP',
'smtp_host' => 'mail.xxx.biz',
'smtp_port' => 25,
'smtp_user' => '[email protected]',
'smtp_pass' => 'Test@123##',
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'wordwrap' => TRUE,
'charset'  => 'utf-8',
'priority' => '1',
);
$this->load->library('email',$config);
$this->email->set_newline("\n");
// $this->email->from('[email protected]',"title email");
$this->email->from('[email protected]',"subject");
$this->email->to($email); 
$this->email->subject($subject);
$this->email->message($message);
return $this->email->send();
}

or even this one

public function send_mail_cart($email, $subject, $message)
{
$this->load->library('PHPMailer/phpmailer');
$mail = new PHPMailer();
$mail->IsSMTP(); 
$mail->Host = "smtp.gmail.com";  // specify main and backup server
$mail->SMTPAuth = true;    // turn on SMTP authentication
$mail->Username = "[email protected]";  // SMTP username
$mail->Password = "xxxxx@1234"; // SMTP password
$mail->SMTPSecure = "tls";
$mail->Port = 587;
$mail->From = "[email protected]";
$mail->FromName = "title email ";
$mail->AddAddress($email);
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = $subject;
$mail->Body    = $message;
if(!$mail->Send())
{
  return false;
}
else
{
return true;


could i adapt any of the above??


RE: trouble sending email controller - pmcr - 10-08-2019

(10-06-2019, 08:11 AM)InsiteFX Wrote: And if you are on localhost you need to configure it in php.ini
i am running it from live server 
i can't find any php.ini file in the server 
Where should it be located? what should be the full command on it if really its the issue


RE: trouble sending email controller - InsiteFX - 10-08-2019

You only need to set it if your running on localhost.

Are you sure that your SMTP port number is correct?


RE: trouble sending email controller - jreklund - 10-08-2019

(10-08-2019, 12:29 AM)pmcr Wrote: I have another controller where email is sent out without any issue 

Okey, then you need to to debug it by passing in everything manually instead of input->post. Not relying on outside factors. And start from there.


RE: trouble sending email controller - pmcr - 10-08-2019

(10-08-2019, 08:55 AM)InsiteFX Wrote: You only need to set it if your running on localhost.

Are you sure that your SMTP port number is correct?
Yes. 
i use it on another controller in which emails are being sent out.