PHP mail function ignoring additional headers - php

I'm trying to send a mail using the php mail() function
but for some reason I don't get, additional headers are ignored.
This is my code:
$errors = '';
$myemail = 'my#email.com';
$myemail_bcc = 'my_bbc#email.com';
if(empty($_POST['nome']) || empty($_POST['email']))
{
$errors .= "Error: You must fill Name and Email fields";
}
$name = $_POST['nome'];
$email_address = $_POST['email'];
$telefono = $_POST['telefono'];
$message = $_POST['messaggio'];
if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address))
{
$errors .= "<br />Error: You must use a valid email address";
}
// spediamo la mail
if( empty($errors))
{
$to = $myemail;
$email_subject = "New Request from: $name";
$email_body = "User data:.\n\n";
$email_body .= "Nome: $name \nEmail: $email_address \nTelefono: $telefono \n";
$email_body .= "Messaggio: \n$message";
$headers = "From: John Smith <$myemail>\r\n";
$headers .= "Reply-To: $name <$email_address>\r\n";
$headers .= "Bcc: $myemail_bcc";
mail($to,$email_subject,$email_body,$headers);
}
I've tried to vary the way I declare headers without names, just with emails:
$headers = "From: $myemail \r\n";
$headers .= "Reply-To: $email_address\r\n";
$headers .= "Bcc: $myemail_bcc";
and even just on one line
$headers = "From: $myemail\r\nReply-To: $name <$email_address>\r\nBcc: $myemail_bcc";
But in no case the mail is sento to the Bcc address, and the "reply" function in client email does not use the Reply-to address.
where am I wrong?

Related

how to hide reply-to header when received by mail box?

How can i hide reply to header , from mail boxes,
For example , i send a mail from test#domain.com with reply to as notification#domain.com, and receiver open the mail and click on reply, i want the receiver to see from email which is test#domain.com in reply, instead of reply to which is notification#domain.com, and also the reply will come notification#domain.com .
Any help. My code below
<?php
if(#isset($_POST['send'])){
$From = $_POST['From'];
$Subject = $_POST['Name'];
//$Message = $_POST['Message'];
$Message = "";
$Reply= "notification#domain.com";
$Emails = $_POST['Emails'];
$toname = $_POST['toname'];
$Name = $_POST['Name'];
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type:text/html;charset=UTF-8\r\n";
$headers .= "Reply-To: ".$Reply."\r\n";
$headers .= "From: ".$From."\r\n";
$headers .= "Hi<br>".$toname."\r\n";
$headers .= "<br><br>Are you today? <br><br><br><br>Thanks,<br>".$Name."<br><br>
Sent from my work office\r\n";
$Emails = explode("\r\n", $_POST['Emails']);
foreach($Emails as $email) {
mail($email,$Subject,$Message,$headers);
echo "<br>Sending Email from : name:".$Name.", email: ".$From.", to name: ".$toname.", email:".$email." => was successfully sent";
//echo "<br>Sending Email To : ".$email." => Done";
}
}
?>

Mail() - message body with utf-8 and html tags

Contact form on site use script below. It send 2 messages - to me and to the person filling in the form. In second mail I have problem with characters like "ąźćęś"....I would like to use utf-8. I set it in header, but it doesn't work. I need to use in message body html tags also.
What wrong in the code?
<?php
// Check for empty fields
if(empty($_POST['email']) ||
empty($_POST['phone']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
echo "No arguments Provided!";
return false;
}
$email_address = $_POST['email'];
$phone = $_POST['phone'];
// Create the email and send the message
$to = 'me#test.com';
$email_subject = "Mail to you";
$email_body = "Hello\n\n"."Test\n\nEmail: $email_address\n\nPhone: $phone";
$headers = "From: test#test.com\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
$to2 = "$email_address";
$email_subject2 = "=?UTF-8?B?".base64_encode("Thanks for you message")."?=";
$email_body2 = "Test mail ążźćęśó\n\nBest regards\nTester";
$headers2 .= "Content-Type: text/html; charset=UTF-8";
$headers2 = 'From: Tester <test#test.com>' . "\r\n";
mail($to2,$email_subject2,$email_body2,$headers2);
return true;
?>

How to add a cc in a php contact form?

I have a contact form for a HTML page. Right now the recipient is only one. How can I add multiple recipient. Any help on this would be a great help.
Code is below.
Thanks.
<?php
$to = "support#domain.in";
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$subject = "Message from $name ($email)";
$replyto = $email;
$headers="";
$headers = "From: test <domain#one.com>\n";
$headers .= "Reply-To: $replyto";
$result = mail($to,$subject,$message,$headers);
?>
Here is your sample code
$to = "xyz#somedomain.com";
$subject = "This is subject";
$message = "<b>This is HTML message.</b>";
$message .= "<h1>This is headline.</h1>";
$header = "From:abc#somedomain.com \r\n";
$header .= "Cc:afgh#somedomain.com \r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html\r\n";
$retval = mail ($to,$subject,$message,$header);
if( $retval == true ) {
echo "Message sent successfully...";
}else {
echo "Message could not be sent...";
}

Send BCC in PHP

Need to send a BCC copy to a user2#gmail.com
Not familiar with PHP and I'm missing something.
<?php
if(empty($_POST['name2']) || empty($_POST['email2']) || empty($_POST['message2']))
{
return false;
}
$name2 = $_POST['name2'];
$email2 = $_POST['email2'];
$message2 = $_POST['message2'];
$to = 'user1#gmail.com'; // Email submissions are sent to this email
// Create email
$email_subject = "Message from Website;
$email_body = "You have received a new message. \n\n".
"Name2: $name2 \nEmail2: $email2 \nMessage2: $message2 \n";
$headers = "From: user1#gmail.com\n";
$headers .= "Reply-To: $email2";
$headers .= "Bcc: user2#gmail.com" . "\r\n";
mail($to,$email_subject,$email_body,$headers); // Post message
return true;
?>
at the end of any $header line fix that end with "\r\n"
$headers = "From: user1#gmail.com\r\n";
$headers .= "Reply-To: $email2\r\n";
$headers .= "Bcc: user2#gmail.com\r\n";
try now

Php email send copy to sender

I have a contact form and I am using the below PHP Script to get emails from my contact form. I want to send a copy of the same email to the sender email also. Can anyone help me?. Thank you!
<?php
$errors = '';
$myemail = 'chocolatehills_adventurepark#yahoo.com, chocolatehills88#yahoo.com';//<-----Put Your email address here.
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['subject']) ||
empty($_POST['message']))
{
$errors .= "\n Error: all fields are required";
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address))
{
$errors .= "\n Error: Invalid email address";
}
if( empty($errors))
{
$to = $myemail;
$email_subject = "Message from: $name";
$email_body = "New message received. ".
" Here are the details:\n \n NAME: $name \n
SUBJECT: $subject \n
EMAIL ADD: $email_address \n
MESSAGE: $message";
$headers = "From: $email_address\n";
//$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: contact-us.php');
}
?>
You can use cc n header
$headers .= 'Cc: somebody#domain.com' . "\r\n";
More idea
http://php.net/manual/en/function.mail.php
Examples of using CC and BCC.
Example #4 Sending HTML email
// Additional headers
$headers .= 'To: Mary <mary#example.com>, Kelly <kelly#example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";
You can also try to add another mail function like this
mail($email_address,$email_subject,$email_body,$headers);

Categories