How do i add from address using CodeIgniter? - php

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'

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";

Related

Mail function always calling the else part

I have two domain name. For example https://example.com and https://example2.com.
I am sending test mail using mail function.
I am able to send the email from https://example.com and I am getting the success but the same code I am using for https://example2.com and I am not getting the email it's always calling the else part.
Test mail
<?PHP
$sender = 'xxx#xx.com';
$recipient = 'zzz#zz.com';
$subject = "php mail test";
$message = "php test message";
$headers = 'From: ' . $sender . "\r\n";
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html;charset=UTF-8" . "\r\n";
if (mail($recipient, $subject, $message, $headers))
{
echo "Message accepted";
}
else
{
echo "Error: Message not accepted";
$errorMessage = error_get_last()['message'];
echo $errorMessage;
}
?>
The From header is not valid as the next one is concatenated right after it:
$headers = 'From:' . $sender;
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
Should be something like:
// add \r\n
$headers = 'From: ' . $sender . "\r\n";
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html;charset=UTF-8" . "\r\n";
Try this simple php mail function
$to = $email_id;
$subject = "";
$message = "Your mail Body Content. you also use here HTML Tags for better performence. ";
$headers = 'From: xyz#domainname.com' . "\r\n" .
'Reply-To: xyz#domainname.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$headers.= "MIME-Version: 1.0" . "\r\n";
$headers.= "Content-type:text/html;charset=UTF-8" . "\r\n";
if(mail($to, $subject, $message, $headers)){
echo "Message accepted";
}else{
echo "Error: Message not accepted";
}
Check if mail function is enabled on example2.com by using below:
if ( function_exists( 'mail' ) )
{
echo 'mail() is available';
}
else
{
echo 'mail() has been disabled';
}
If disabled, enable it via php.ini or in cPanel.

Reply to won't work on php form submitting to itself

I can't get the reply to function to work on a contact form that submits to itself.
The BCC is working fine. Any help is appreciated.
$to = 'email#email.com';
$subject = ''.$_POST['emailsubject'].'';
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1';
$headers .= 'Reply-To: '.$_POST['email'].'';
'X-Mailer: PHP/' . phpversion();
$headers .= 'Bcc: email#email.com' . "\r\n";
mail($to, $subject, $msg, $headers); $sendMessage = true; unset($_POST); } } ?> <?php if($invalidCaptcha) { ?>
<?php
$from_add = "test#gmail.com";
$to_add = "email#email.com";
$subject = "Test Msg";
$message="test msg.";
$headers = "From: $from_add \r\n";
$headers .= "Reply-To: $from_add \r\n";
$headers .= "Return-Path: $from_add\r\n";
$headers .= "X-Mailer: PHP \r\n";
if(mail($to_add,$subject,$message,$headers))
{
$msg = "Mail sent OK";
}
else
{
$msg = "Error sending email!";
}
?>

Php Mail() function: Headers was printed

I was trying to send mail in PHP with mail() function. The email sent successfull and it's exist in the inbox. The problem is the headers printed in the email header. I've tried the various code for the headers.
1.The first goes like this:
$headers = "From: My Example Email".'\r\n'.
"MIME-Version: 1.0".'\r\n'.
"Content-Type: text/html; charset=ISO-8859-1".'\r\n'.
'X-Mailer: PHP/' . phpversion();
Result: My Example EmailrnMIME-Version
2.Second headers code:
$headers = "From: My Example Email"."\r\n".
"MIME-Version: 1.0"."r\n".
"Content-Type: text/html; charset=ISO-8859-1"."\r\n".
'X-Mailer: PHP/' . phpversion();
Result: The email didn't sent
3.Third headers code:
$headers = "From: My Example Email".'"\r\n"'.
"MIME-Version: 1.0".'"\r\n"'.
"Content-Type: text/html; charset=ISO-8859-1".'"\r\n"'.
'X-Mailer: PHP/' . phpversion();
Result: My Example Emailrn
I use PHP 5.4.19. Any answer will really help.
UPDATE
This is my whole code:
class User{
function callname(){
$user = $_SESSION['id'];
$query = ("SELECT * FROM user WHERE user.id='$user'");
while ($result=mysql_fetch_array($query)){
echo ($result['username']);}}}
$user = new User;
if($_SERVER["REQUEST_METHOD"] == "POST"){
$username = mysql_real_escape_string(trim($_POST['username']));
$check = mysql_num_rows(mysql_query("SELECT * FROM user WHERE username='$username'"));
if ($check==TRUE){
$name = $user->callname();
$to = "myemail#domain.com";
$subject = "Example Subject";
$headers = "From: My Example Email".'"\r\n"'.
"MIME-Version: 1.0".'"\r\n"'.
"Content-Type: text/html; charset=ISO-8859-1".'"\r\n"'.
'X-Mailer: PHP/' . phpversion();
$message = "Hai $name, this is the new message.";
mail($to, $subject, $message, $headers);
} else {
?>
<script type="text/javascript">
alert("Sorry, username not exist !");
</script>
<?php }}
New UPDATE:
After a long trial and help with everyone here, finally found the solution. But maybe it's unusual.
$headers = 'From: My Example Email'.'""'.
'MIME-Version: 1.0'.'""'.
'Content-Type: text/html; charset=ISO-8859-1'.'""'.
'X-Mailer: PHP/' . phpversion();
but I'm not understand yet. Some literature said that every part shoul be glue by "\r\n", but that's not work in my code.
Thanks for every help. Thanks a lot. Thats all really helpful.
You could try my function for php mailing. This function will generete RFC compatible body and header part for your email.
function buildMime($msg){
$num = md5(time());
$num = "_001_".$num."_";
$headers = "From: SenderName<senderEmail#adress.com>\n";
$headers .= "Return-Path: <senderEmail#adress.com>\n";
$headers .= "Reply-To: <senderEmail#adress.com>\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/alternative;\n";
$headers .= " boundary=\"".$num."\"\n";
$headers .= "X-Mailer: PHP v".phpversion()."\n";
$body = "This is a multi-part message in MIME format.\n\n";
$body1 = "--".$num."\n";
$body1 .= "Content-Type: text/plain; charset=utf-8\n";
$body1 .= "Content-Transfer-Encoding: 8bit\n\n";
$body1 .= trim(strip_tags($msg))."\n";
$body1 .= "\n";
$body1 .= "--".$num."\n";
$body1 .= "Content-Type: text/html; charset=utf-8\n";
$body1 .= "Content-Transfer-Encoding: 8bit\n\n";
$body1 .= $msg;
$body1 .= "\n";
$bodyx = "--".$num."--\n";
return array('body' => $body.$body1.$bodyx, 'headers' => $headers);
}
$mime = buildMime("<h1>Hello</h1><p>this is my firs test message</p>");
mail('whereToSend#email.com', 'Your subject', $mime[body], $mime[headers]);
?>
$headers = "From: My Example Email"."\r\n".
"MIME-Version: 1.0"."\r\n".
"Content-Type: text/html; charset=ISO-8859-1"."\r\n".
'X-Mailer: PHP/' . phpversion();
Try this
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: ' . "\r\n";
$headers .= 'Cc: myboss#example.com' . "\r\n";
mail($to,$subject,$message,$headers);
You are not properly encapsulating the quotes.
The right way to do...
$headers = 'From: My Example Email'."\r\n".
'MIME-Version: 1.0'."\r\n".
'Content-Type: text/html; charset=ISO-8859-1'."\r\n".
'X-Mailer: PHP/' . phpversion();
EDITED CODE
<?php
class User{
function callname(){
$user = $_SESSION['id'];
$query = ("SELECT * FROM user WHERE user.id='$user'");
while ($result=mysql_fetch_array($query)){
return $result['username'];}}}
$user = new User;
if($_SERVER["REQUEST_METHOD"] == "POST"){
$username = mysql_real_escape_string(trim($_POST['username']));
$check = mysql_num_rows(mysql_query("SELECT * FROM user WHERE username='$username'"));
if ($check==TRUE){
$name = $user->callname();
$to = "myemail#domain.com";
$subject = "Example Subject";
$headers = "From: My Example Email"."\r\n".
"MIME-Version: 1.0"."\r\n".
"Content-Type: text/html; charset=ISO-8859-1"."\r\n".
"X-Mailer: PHP/" . phpversion();
$message = "Hai $name, this is the new message.";
mail($to, $subject, $message, $headers);
} else {
?>
<script type="text/javascript">
alert("Sorry, username not exist !");
</script>
<?php }}

Contact php mail page, only sending blank emails.

I am having problems with this contact page, emails are being sent fine but are blank! I cant seem to find a solution. I would of thought $_POST would need to be added, however, the web hosting companies says it is not necessary in this php script Thankyou for your time and help. Code snippet below.
<?php
$EmailFrom = "sales#ibdengland.co.uk";
$EmailTo = "kent.collins.uk#gmail.com";
$Subject = "online form";
$Name = Trim(stripslashes($_POST['Name']));
$Email = Trim(stripslashes($_POST['Email']));
$Tel = Trim(stripslashes($_POST['Tel']));
$Message = Trim(stripslashes($_POST['Description of project']));
// validation
$validationOK=true;
if (!$validationOK) {
echo "please check your details";
header("Location: http://www.ibdengland.co.uk/thankyou.html");
exit;
}
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Tel: ";
$Body .= $Tel;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Description of project: ";
$Body .= $Message;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"1;URL=thankyou.html\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"1;URL=thankyou.html\">";
}
?>
You forgot to add the headers.
Example:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$headers .= 'From: '. $EmailFrom . "\r\n" .
'X-Mailer: PHP/' . phpversion();
Try changing:
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
To:
$success = mail($EmailTo, $Subject, $Body, "From: <".$EmailFrom.">");
you have not use any header.
take help from here. and use it. I think it will help you.
post header like below
$headers = 'From:'.$EmailFrom . "\r\n" .
'Reply-To: '.$EmailFrom . "\r\n" .
'X-Mailer: PHP/' . phpversion();
then replace below
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
with the below code
$success = mail($EmailTo, $Subject, $Body, $headers);
plz try this one. and inform me if this work or not.

php mail function

I got a strange behavior from the mail function in php
here is the code :
$header = "From: aa#aa.com\n";
$header .= "Reply-To: bb#bb.com\n";
$header .= "Content-Type: multipart/alternative; boundary=$alt_boundary\n";
$header .= "Mime-Version: 1.0\n";
$header .= "X-Mailer: PHP/".phpversion()."\n";
$header .= "Content-Type: text/plain;charset=utf-8\n";
$send = mail($to,$subject,$message,$headers);
but the email i receive have a from address from the main admin of the server like : user123#s12panelboxmanage.com
why ?
Maybe it's because you set a variable $header, but pass to mail() variable $headers. If it's not the cause, try inserting \r\n instead of \n.
You should use the -f option in the mail function too to set the (valid) sender:
$header = 'MIME-Version: 1.0'."\n";
$header .= 'Content-type: text/'.$contentType.'; charset=iso-8859-1'."\n";
$header .= 'From: '.$from."\n";
$header .= 'Reply-To: '.$mailFrom."\n";
$header .= 'X-Mailer: PHP '.phpversion()."\n";
$header .= 'X-Sender-IP: '.$_SERVER['REMOTE_ADDR']."\n";
mail($to,$subject,$message,$header, "-f aa#aa.com");
<?php
$to = 'user#domain.com';
$subject = 'Subject';
$message = 'This is a test';
$headers = 'From: webmaster#yourdot.com' . "\r\n" .
'Reply-To: webmaster#yourdot.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>

Categories