-1

This is my code and it's return true but i didn't receive any mail. It's working on another website. I want to check my code is working or not.

$name = $this->input->post('name');
$email = $this->input->post('email');
$phone = $this->input->post('phone');
$message = $this->input->post('msg'); 
// Email content

$body = "Hi xxx<br /><br />";
$body.= "Name : " . $name . "<br />";
$body.= "Email : " . $email . "<br />";
$body.= "Phone No : " . $phone . "<br />";

$body.= "Message Placed: " . $message . "<br />";
$body.= "<br />For Enquiry Request</br> ";

    $subject = "Consultation email";
    $to ="xxxxx@xxxxx.in";
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
    $headers .= 'From: <'.$email.'>' . "\r\n";
    if(mail($to,$subject,$body,$headers))
    {
    echo '<script>alert("Your message has been sent, our associate will get back to you soon")</script>';
    echo '<script>window.location.href="' . base_url() . '"</script>';
    }
    else{
    
    echo '<script>alert("your message has been not sent")</script>';
    echo "<script>window.location.href='" . base_url() . "'</script>";
    }
halfer
  • 19,824
  • 17
  • 99
  • 186
Ricky
  • 3
  • 5
  • If you don't have a properly configured web server, your mail probably ends up in spams / is not even displayed on certain webmails. – Altherius Jul 15 '20 at 06:14
  • currently i want to check mail function working or or not,it's return true value but not received any mail not in spam .Is there any issue in code please tell @Altherius Thanks in Advance – Ricky Jul 15 '20 at 06:20
  • I don't see any problem with the code but I don't know the post values you're giving (`$this->input->post('...')`), you can try and dump them to see if they're correct. If you're still in a development environment you can use tools like mail-tester or mailtrap to check the reception of your emails. – Altherius Jul 15 '20 at 07:14
  • (Using `mail()` in a codeigniter environment, are you kidding? CI has its own email library already.) – CBroe Jul 15 '20 at 07:45
  • Dear @CBroe i'm also using CI email library i'm facing some issue (mail not received ) thats why i want to try this mail function .if i'm using email library i'm facing same issue – Ricky Jul 15 '20 at 08:17
  • Check the mentioned duplicate, and pay attention in particular to the section “Don't use a faux From: sender” – CBroe Jul 15 '20 at 08:19
  • $config = Array( 'protocol' => 'smtp','smtp_host' => 'host', 'smtp_port' => 465, 'smtp_user' => 'xxx', 'smtp_pass' => 'xxx', 'mailtype' => 'html', 'charset' => 'iso-8859-1' ) $this->load->library('email', $config); $this->email->set_mailtype("html"); $this->email->set_newline("\r\n"); $this->email->from('no-reply@domain.com', 'abc'); $this->email->to('abc@gmail.com'); $this->email->subject('abc'); $this->email->message('abc'); – Ricky Jul 15 '20 at 08:20
  • Dear @Altherius i'm using same code PHP mail function doesn't complete sending of e-mail but my issue is i'm not received any mail dont know but code response is true. if i'm using Email library or mail function facing same issue – Ricky Jul 15 '20 at 08:24
  • Look at CBroe's link, it explains in detail all the possible reasons why a mail can be sent and not received. – Altherius Jul 15 '20 at 08:37

1 Answers1

0

There are a few things to consider and one of them is that you cannot send emails from localhost. There may be a work around but generally no. Below is an article to assist you if you are sending the mail from a localhost environment

https://www.phpflow.com/php/how-to-send-email-from-localhost-using-php/#:~:text=We%20can%20send%20mail%20from,ini%20file.&text=The%20sendmail%20package%20is%20inbuilt,easily%20send%20mail%20from%20localhost.

And if you are not in a local environment, the below answer from a previous question seems to have enough answers to help you out.

PHP mail function doesn't complete sending of e-mail

Also, you have stated to be using codeigniter. I think you are missing some few things there. check out this other article to fix you up.

https://www.cloudways.com/blog/send-email-codeigniter-smtp/

Let me know which of these helped. If none let me know.

mw509
  • 1,957
  • 1
  • 19
  • 25