• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
trouble sending email controller

#1
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      = 'sales@xxx.com';
            $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**
Reply

#2
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]
Reply

#3
(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);
Reply

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

#5
And if you are on localhost you need to configure it in php.ini
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#6
(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' => 'help@biz.com',
'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('xxxx@gmail.com',"title email");
$this->email->from('help@xxxt.com',"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 = "xxxx@gmail.com";  // SMTP username
$mail->Password = "xxxxx@1234"; // SMTP password
$mail->SMTPSecure = "tls";
$mail->Port = 587;
$mail->From = "xxxxxxxx@gmail.com";
$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??
Reply

#7
(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
Reply

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

Are you sure that your SMTP port number is correct?
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#9
(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.
Reply

#10
(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.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.