0

I'm working on small CodeIgniter application where I have to build my own contact us form, everything is working fine, receiving email but I need just add From Address in the mail function?

CodeIgniter Mail Function

public function form() {

 $this->load->library('form_validation');
 $this->form_validation->set_rules('name', 'Name', 'trim|required|alpha');
 $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
 $this->form_validation->set_rules('phone', 'Phone', 'trim|required|numeric');
 $this->form_validation->set_rules('message', 'Message', 'trim|required');
 $data['success'] = false;

if ($this->form_validation->run() == TRUE) {

 @mail(config('webmaster_email'), 'Contact Us from ABC',""
    . "Full Name: $_POST[name]\n"
    . "Email: $_POST[email]\n"
    . "Phone: $_POST[phone]\n"
    . "Message: $_POST[message]\n"
    . "");
    $data['success'] = true;


 }

    $this->load->view($this->module, $data);

 }

Need To Add This Line In The Form Mail Function

'From: webmaster@example.com' 

1 Answers1

0

You need to use mail headers to send email from your desired email address. if you need simple email without attachment and markup use mail function else i would prefer using PHP Mailer, see How to send mail using phpmailer

function sendmail($to, $subject, $message, $from)
{
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
    $headers .= 'From: ' . $from . "\r\n";
    $headers .= 'Reply-To: ' . $from . "\r\n";
    $headers .= 'X-Mailer: PHP/' . phpversion();
    @mail($to, $subject, $message, $headers);

    if ($result) return 'sent';
    else return 'error';
}

$to = config('webmaster_email');
$from = "webmaster@example.com";
$subject = "Contact Us from ABC";
$message =
    "Full Name: $_POST[name]\n"
    . "Email: $_POST[email]\n"
    . "Phone: $_POST[phone]\n"
    . "Message: $_POST[message]\n";

$result = sendmail($to, $subject, $message, $from);
var_dump($result);

PHP mail() 4th parameter used for headers.

Complete PHP Email headers:

$headers  = "From: Your Website < mail@yourwebsite.com >\n";
$headers .= "Cc: Your Website < mail@yourwebsite.com >\n"; 
$headers .= "X-Sender: Your Website < mail@yourwebsite.com >\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
$headers .= "X-Priority: 1\n"; // Urgent message!
$headers .= "Return-Path: mail@Your Website.com\n"; // Return path for errors
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\n";
Noman
  • 4,088
  • 1
  • 21
  • 36
  • can u create proper function with form validation thank you :) –  Mar 22 '18 at 14:36
  • You can add proper validation as i don't know what is your requirement and how does the validation should be, and as stack-overflow is not writing code but to help. cheers :) – Noman Mar 22 '18 at 14:38