How to add BCC in a mail function php - php

I want to add bcc in my mail function php file.
if(isset($_POST['fname']) && isset($_POST['lname']) && isset($_POST['email']) && isset($_POST['phone']) && isset($_POST['message'])){
if(!empty($_POST['fname']) && !empty($_POST['lname']) && !empty($_POST['email']) && !empty($_POST['phone']) && !empty($_POST['message'])){
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$message = $_POST['message'];
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo'Please feel valid email';
}
else {
$to='kriti#vsbhotels.com, contact#vsbhotels.com, verun#vsbhotels.com';
$body = $fname."\n". $lname."\n" . $phone."\n" . $email."\n" . $message;
if(mail($to , $fname." ".$lname , $body ,'From: response#themerakihotel.com')) {
header('Location: thank-you.html');
exit();
}
else {
echo "Something went wrong!";
}
}
}
else {
echo 'Please Fill All Inputs';
}
}
else {
echo 'not okk';
}
echo "<meta http-equiv='refresh' content='0; url=http://merakihotels.in/demo/'>";

add in header:
$headers[] = 'Bcc: someone#example.com';
for your code:
mail($to , $fname." ".$lname , $body ,'From: response#themerakihotel.com' . "\r\n" . 'Bcc: someone#example.com')
Though not solved then try:
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: FromName<from#example.com>' . "\r\n";
$headers .= 'BCC: Someone<someone#example.com>' . "\r\n";
mail($to, $subject, $message, $headers);

Just add BCC:with your email id in headers parameter.
$headers="From: response#themerakihotel.com\r\n";
$headers.="BCC:WRITE HERE YOUR EMAIL ID";
mail($to , $fname." ".$lname , $body ,$headers);

Related

Can't send an email with PHP

I am trying to send an confirmation email after user makes an account. The email doesn't send anything. I did test it and the email is not even going to the spam or inbox folder. Am I missing something? Thank you for your help.
$status = "";
if (isset($_POST["sign_up"])) {
$first_name = (isset($_POST['first_name']) ? $_POST['first_name'] : null);
$last_name = (isset($_POST['last_name']) ? $_POST['last_name'] : null);
$username = (isset($_POST['username']) ? $_POST['username'] : null);
$password = (isset($_POST['password']) ? $_POST['password'] : null);
$email = (isset($_POST['email']) ? $_POST['email'] : null);
$phone = (isset($_POST['phone']) ? $_POST['phone'] : null);
if ($first_name == "" || $last_name == "" || $username == "" || $password == "" || $email == "" || $phone == "") {
$status = '<p style="color:#FF0000;">Please fill out all the field';
} else if (strlen($password) < 6) {
$status = '<p style="color:#FF0000;">Password must more than 6 characters';
} else{
$sql = "INSERT INTO members(
first_name,
last_name,
username,
password,
email,
phone
) VALUES (
'$first_name',
'$last_name',
'$username',
'$password',
'$email',
'$phone'
)";
$res = mysqli_query($mysqli,$sql) or die(mysqli_error());
if ($res) {
$to = $email;
$subject = "Confirmation from Yu Fong to $username";
$from = "abc#yahoo.com";
$message = "Please click the link below to verify and activate your account. \r\n";
$message .= "Testing";
$header = "From: " .$from. "\r\n";
$header .="Reply-To: ".$from. "\r\n";
$header .= "CC: abc#yahoo.com\r\n";
$header .= "Return-Path: ".$from. "\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$header .= "X-Priority: 3\r\n";
$header .= "X-Mailer: PHP". phpversion() ."\r\n";
$sentmail = mail($to, $subject, $message, $header);
if ($sentmail == true) {
echo $status = '<p style="color:#FF0000;">Congratulations! Your account has been created. An confirmation email has been sent!';
} else {
echo "Eror!";
}
}
}
}
I just tested your email form,it works perfectly,i got the email.
$to = "Myemail#derp.derp";
$subject = "Confirmation from Yu Fong to $username";
$from = "derpington";
$message = "Please click the link below to verify and activate your account. \r\n";
$message .= "Testing";
$header = "From: " .$from. "\r\n";
$header .="Reply-To: ".$from. "\r\n";
$header .= "CC: abc#yahoo.com\r\n";
$header .= "Return-Path: ".$from. "\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$header .= "X-Priority: 3\r\n";
$header .= "X-Mailer: PHP". phpversion() ."\r\n";
$sentmail = mail($to, $subject, $message, $header);
if ($sentmail == true) {
echo $status = '<p style="color:#FF0000;">Congratulations! Your account has been created. An confirmation email has been sent!';
} else {
echo "Eror!";
}
This is just your form,and it works so i am assuming the issue is coming from another place,try changing
if ($res) {
to
if (1+1 == 2) {
just to check if your If statement actually prints true,and go from there.
Try changing
if ($sentmail == true) { ...
to
if ($sentmail) { ...
Or to shorten it a bit try
if(#mail($to, $subject, $message, $headers))
{
echo "Mail Sent Successfully";
}else{
echo "Mail Not Sent";
}

Changing form php sendmail to SMTP?

We created this contact form a long time ago, but we have recently had issues with our sendmail, now i need to change this to use SMTP and that i haven't done before. Is it much work or just a matter of changing few lines? Any tips are welcome.
You can see our whole script here, it's very simple...
<?php
if(!$_POST) exit;
function tommus_email_validate($email) {
return filter_var($email, FILTER_VALIDATE_EMAIL) && preg_match('/#.+\./', $email);
}
$name = $_POST['name']; $email = $_POST['email']; $phone = $_POST['phone']; $comments = $_POST['comments'];
if(trim($name) == '') {
exit('<div class="error_message">You must enter your name.</div>');
} else if(trim($name) == 'Name') {
exit('<div class="error_message">You must enter your name.</div>');
} else if(trim($email) == '') {
exit('<div class="error_message">Please enter a valid email address.</div>');
} else if(!tommus_email_validate($email)) {
exit('<div class="error_message">You have entered an invalid e-mail address.</div>');
} else if(trim($comments) == 'Tell us what you think!') {
exit('<div class="error_message">Please enter your message.</div>');
} else if(trim($comments) == '') {
exit('<div class="error_message">Please enter your message.</div>');
} else if( strpos($comments, 'href') !== false ) {
exit('<div class="error_message">Please leave links as plain text.</div>');
} else if( strpos($comments, '[url') !== false ) {
exit('<div class="error_message">Please leave links as plain text.</div>');
} if(get_magic_quotes_gpc()) { $comments = stripslashes($comments); }
$address = 'hello#basicagency.com';
$e_subject = 'You\'ve been contacted by ' . $name . '.';
$e_body = "You have been contacted by $name from your contact form, their additional message is as follows." . "\r\n" . "\r\n";
$e_content = "\"$comments\"" . "\r\n" . "\r\n";
$e_reply = "You can contact $name via email, $email (or by phone if supplied: $phone)";
$msg = wordwrap( $e_body . $e_content . $e_reply, 70 );
$headers = "From: $email" . "\r\n";
$headers .= "Reply-To: $email" . "\r\n";
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/plain; charset=utf-8" . "\r\n";
$headers .= "Content-Transfer-Encoding: quoted-printable" . "\r\n";
if(mail($address, $e_subject, $msg, $headers)) {
echo "<fieldset><div id='success_page'><p>Thank you $name, your message has been submitted to us.</p></div></fieldset>";
}
You can use a package for handling e-mails, such as PHPMailer, just use the included methods to build your message, instead of the $headers variable. Once you have downloaded PHPMailer and have it somewhere accessable by your script, replace this:
$headers = "From: $email" . "\r\n";
$headers .= "Reply-To: $email" . "\r\n";
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/plain; charset=utf-8" . "\r\n";
$headers .= "Content-Transfer-Encoding: quoted-printable" . "\r\n";
if(mail($address, $e_subject, $msg, $headers)) {
echo "<fieldset><div id='success_page'><p>Thank you $name, your message has been submitted to us.</p></div></fieldset>";
}
with something like this:
require '/path/to/PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
// SMTP server details
$mail->Host = "mail.example.com";
$mail->Port = 25;
$mail->SMTPAuth = true;
$mail->Username = "yourname#example.com";
$mail->Password = "yourpassword";
// message details
$mail->setFrom($email, $email);
$mail->addReplyTo($email, $email);
$mail->addAddress($address, $address);
$mail->Subject = $e_subject;
$mail->Body = $msg;
// send
if($mail->send()) {
echo "<fieldset><div id='success_page'><p>Thank you $name, your message has been submitted to us.</p></div></fieldset>";
}
else{
echo 'Mailer Error: ' . $mail->ErrorInfo;
}

PHP: Mail not sent

I am trying on the following code but it keeps saying Mail not sent. How do I find the real issue? Code given below:
$full_name = htmlspecialchars(trim($_POST['full_name']));
$email = htmlspecialchars(trim($_POST['email']));
$phone = htmlspecialchars(trim($_POST['phone']));
$message = htmlspecialchars(trim($_POST['message']));
$to = "mail#example.com";
$subject = $full_name . ' is interested to have a business discussion with you';
$headers = "From: " . strip_tags($_POST['email']) . "\r\n";
$headers .= "Reply-To: " . strip_tags($_POST['email']) . "\r\n";
// $headers .= "CC: susan#example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = '<html><body>';
$message .= '<h3>Full Name: </h3>: ' . $full_name . '<br>';
$message .= '<h3>Phone: </h3>: ' . $phone . '<br>';
$message .= '<h3>Message: </h3>: ' . $message . '<br><br>=======================<br>';
$message .= '<h3>IP Address: </h3>: ' . $ip . '<br>';
$message .= '</body></html>';
if(mail($to, $subject, $message, $headers))
{
echo "Mail Sent Successfully";
}else{
echo " Mail Not Sent";
}
Try this code hope this helpful.
<?php
//print_r($_POST);
//$fname = $_POST['fname'];
//$lname = $_POST['lname'];
//$email = $_POST['email'];
//$message = $_POST['message'];
if(isset($_POST['fname']) && $_POST['fname'] != ''){
$fname = $_POST['fname'];
}
if(isset($_POST['lname']) && $_POST['lname'] != ''){
$lname = $_POST['lname'];//phone number
}
if(isset($_POST['email']) && $_POST['email'] != ''){
$email = $_POST['email'];
}
if(isset($_POST['message']) && $_POST['message'] != ''){
$com = $_POST['message'];
}
$to = 'noreply#noreply.com';
$subject = 'Site Visiter.';
// message
$message = sprintf("Site visiter details: <br>Name:- %s <br> Phone:- %s <br> Email:- %s<br> Message:- %s",$fname,$lname,$email,$com);
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: Mary <nb158f#gmail.com>' . "\r\n";
$headers .= 'From: mysite.com <admin#site.com>' . "\r\n";
//$headers .= 'Cc: divakar.k#gmail.com' . "\r\n";
// Mail it
$flag = mail($to, $subject, $message, $headers);
echo '<script>alert("Mail Sent :");</script>';
echo '<script>window.location.href="index.php";</script>';

Contact form php script sending spam to Gmail

I created a php script (for a contact form) to send emails to my Gmail account.
If I use the sender email in the header ($headers = "From: " . $email;), Gmail reports the received message as spam.
If I don't use the email in the header (e.g. the sender name $headers = "From: " . $name;) the message is not reported as spam.
Do you have any suggestion to let me use the email in the header?
Thanks!
<?php
/* Check if the url field is empty (antispam) */
if ($_POST['leaveblank'] != '' or $_POST['dontchange'] != 'http://') {
$name = $_POST['name'];
$faillink = "xxx.php";
header("Location: $faillink");
} else {
$name = $_POST['name'];
$email = $_POST['email'];
$subject_prefix = "[ContactForm]: ";
$subject = $subject_prefix . $_POST['subject'];
$message = $_POST['message'];
$to = "myemail#gmail.com";
$body = "From: " . $name . "\n";
$body .= "Email: " . $email . "\n";
$body .= "Message: " . $message . "\n";
$headers = "From: " . $email;
$oklink = "yyy.php";
$faillink = "xxx.php";
if ( preg_match( "/[\r\n]/", $name ) || preg_match( "/[\r\n]/", $email ) ) {
header("Location: $faillink");
}
$retmail = mail($to, $subject, $body, $headers);
if ($retmail) {
header("Location: $oklink");
} else {
header("Location: $faillink");
}
}
?>
I solved the issue as Iain suggested so I replaced the mail headers as follows:
$headers = "From: " . "noreplay#mydomain.com" . "\r\n";
$headres .= "Reply-To: " . $email . "\r\n";

Send email issue

I am noob with php so pardon my ignorance. I have created php form and it is working fine except one thing. When i get my mail it says that its send from nobody , i have no idea how can i solve this issue so i am asking for little help! Thank you!!! Here's my code:
<?php
if (isset($_POST['Submit'])) {
if (!empty($_POST['name'])) {
$_POST['name'] = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
if ($_POST['name'] == "") {
$errors .= 'Molimo unesite Vaše ispravno ime.';
}
} else {
$errors .= '<p>Molimo unesite Vaše ime.</p>';
}
if (!empty($_POST['email'])) {
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors .= "$email is <strong>NIJE</strong> valjana email adresa.<br/><br/>";
}
} else {
$errors .= '<p>Molimo unesite email adresu.</p>';
}
if ($_POST['message'] != "") {
$_POST['message'] = filter_var($_POST['message'], FILTER_SANITIZE_STRING);
}
if (!$errors) {
$mail_to = 'dejo.dekic#yahoo.com';
$subject = 'Kontakt';
$tema = 'Info';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers = 'Od: ' . $_POST['name'] . "\n";
$headers .= 'Email: ' . $_POST['email'] . "\n";
$headers .= "Poruka:\n" . $_POST['message'] . "\n\n";
$user = $_POST['email'];
$poruka ='Vaš kontakt je uspjesno zaprimljen! Odgovorit ću vam u najkraćem mogućem roku. Hvala! Molimo ne odgovarajte na ovu poruku. Ova poruka je automatska.';
mail($mail_to, $subject, $headers, "Content-Type: text/plain; charset=UTF-8;");
mail($user, $tema, $poruka, "Content-Type: text/plain; charset=UTF-8;");
echo "<div style='color:white;margin:0px auto;padding-top:20px;width:290px;background-color:white;font-weight:bold;text-align:center;'><p>Hvala Vam na kontaktu!</p></div>";
} else {
echo '<div class="errors">' . $errors . '<br/></div>';
}
}
?>
And this is how it looks in my yahoo mail:http://www.homepagepays.robertpeic.com/yahoo.png
$from = 'yourdesiredemail#here.com';
$headers .= "From: " . $from . "\r\n";
Just need an addition to the header.
After little struggle i have managed to solve this with your help:) So i am posting a solution if someone had the same problem. Thx! Notice: $add_headers and inside mail function: -fmail#robertpeic.com Here my full code:
if (isset($_POST['Submit'])) {
if (!empty($_POST['name'])) {
$_POST['name'] = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
if ($_POST['name'] == "") {
$errors .= 'Molimo unesite Vaše ispravno ime.';
}
} else {
$errors .= '<p>Molimo unesite Vaše ime.</p>';
}
if (!empty($_POST['email'])) {
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors .= "$email is <strong>NIJE</strong> valjana email adresa.<br/><br/>";
}
} else {
$errors .= '<p>Molimo unesite email adresu.</p>';
}
if ($_POST['message'] != "") {
$_POST['message'] = filter_var($_POST['message'], FILTER_SANITIZE_STRING);
}
if (!$errors) {
$mail_to = 'dejo.dekic#yahoo.com';
$subject = 'Kontakt';
$tema = 'Info';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= "Content-Type: text/plain; charset=UTF-8" . "\r\n";
$headers .= 'Od: ' . $_POST['name'] . "\n";
$headers .= 'Email: ' . $_POST['email'] . "\n";
$headers .= "Poruka:\n" . $_POST['message'] . "\n\n";
$add_headers = 'From: mail#robertpeic.com' . "\r\n". "Return-path: mail#robertpeic.com" . "\r\n";
$user = $_POST['email'];
$poruka ='Vaš kontakt je uspjesno zaprimljen! Odgovorit ću vam u najkraćem mogućem roku. Hvala! Molimo ne odgovarajte na ovu poruku. Ova poruka je automatska.';
mail($mail_to, $subject, $headers, $add_headers, "-fmail#robertpeic.com");
mail($user, $tema, $poruka, $add_headers, "-fmail#robertpeic.com");
echo "<div style='color:white;margin:0px auto;padding-top:20px;width:290px;background-color:white;font-weight:bold;text-align:center;'><p>Hvala Vam na kontaktu!</p></div>";
} else {
echo '<div class="errors">' . $errors . '<br/></div>';
}
}
?>
This is how it look's NOW in my yahoo mail: http://www.homepagepays.robertpeic.com/yahoo2.png

Categories