Send email issue - php

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

Related

PHP code is working, but doesn't show who is the sender. Just showing noreply#example.com

My PHP code for sending email its working good, but there is a problem that doesn't show who is the sender just show noreply#example.com. I hosted my website at ecowebhosting.
<?php
$error = "";
$successMessage = "";
if ($_POST) {
if (!$_POST["email"]) {
$error .= "An email address is required.<br>";
}
if (!$_POST["message"]) {
$error .= "The message field is required.<br>";
}
if (!$_POST["subject"]) {
$error .= "The subject is required.<br>";
}
if ($_POST['email'] && filter_var($_POST["email"], FILTER_VALIDATE_EMAIL) === false) {
$error .= "The email address is invalid.<br>";
}
if ($error != "") {
$error = '<div><p>There were error(s) in your form:</p>' . $error . '</div>';
} else {
$to = "example#gmail.com";
$subject = $_POST['subject'];
$message = $_POST['message'];
$headers = "MIME-VERSION: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers = "From: ".$_POST['email'] . "\r\n";
$headers = "Reply-To: ".$_POST['email'] . "\r\n";
$headers = "Subject: ".$_POST['subject'] . "\r\n";
$headers = "Message : ".$_POST['message'] . "\r\n";
if (mail($to, $subject, $message, $headers)) {
echo $successMessage = '<div>Your message was sent, we\'ll get back to you ASAP!</div>';
} else {
echo $error = '<div><p><strong>Your message couldn\'t be sent - please try again later</div>';
}
}
}
?>

How to add BCC in a mail function 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);

$error is undefined in php form

As the title implies, I have a simple form in php that keeps giving me an undefined error for the object $error. I have tried declaring it globally and setting it to null but all that does is prevent any error logs. Code below, thanks!
<?php
$siteOwnersEmail = 'myemail#yahoo.com';
if($_POST) {
$name = trim(stripslashes($_POST['contactName']));
$email = trim(stripslashes($_POST['contactEmail']));
$subject = trim(stripslashes($_POST['contactSubject']));
$contact_message = trim(stripslashes($_POST['contactMessage']));
// Check Name
if (strlen($name) < 2) {
$error['name'] = "Please enter your name.";
}
// Check Email
if (!preg_match('/^[a-z0-9&\'\.\-_\+]+#[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) {
$error['email'] = "Please enter a valid email address.";
}
// Check Message
if (strlen($contact_message) < 15) {
$error['message'] = "Please enter your message. It should have at least 15 characters.";
}
// Subject
if ($subject == '') { $subject = "Contact Form Submission"; }
// Set Message
$message .= "Email from: " . $name . "<br />";
$message .= "Email address: " . $email . "<br />";
$message .= "Message: <br />";
$message .= $contact_message;
$message .= "<br /> ----- <br /> This email was sent from your site's contact form. <br />";
// Set From: header
$from = $name . " <" . $email . ">";
// Email Headers
$headers = "From: " . $from . "\r\n";
$headers .= "Reply-To: ". $email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
if (!$error) {
ini_set("sendmail_from", $siteOwnersEmail); // for windows server
$mail = mail($siteOwnersEmail, $subject, $message, $headers);
if ($mail) { echo "OK"; }
else { echo "Something went wrong. Please try again."; }
} # end if - no validation error
else {
$response = (isset($error['name'])) ? $error['name'] . "<br /> \n" : null;
$response .= (isset($error['email'])) ? $error['email'] . "<br /> \n" : null;
$response .= (isset($error['message'])) ? $error['message'] . "<br />" : null;
echo $response;
} # end if - there was a validation error
}
?>
Declare the error variable as an array. Add these new lines to your code to fix
if($_POST) {
$error = array();#declare error variable
if (!isset($error)) {#check if we have an error message of some kind
#proceed
Create $error as blank array.Use below code.
<?php
$siteOwnersEmail = 'myemail#yahoo.com';
$error=array();
if(!empty($_POST)) {
$name = trim(stripslashes($_POST['contactName']));
$email = trim(stripslashes($_POST['contactEmail']));
$subject = trim(stripslashes($_POST['contactSubject']));
$contact_message = trim(stripslashes($_POST['contactMessage']));
// Check Name
if (strlen($name) < 2) {
$error['name'] = "Please enter your name.";
}
// Check Email
if (!preg_match('/^[a-z0-9&\'\.\-_\+]+#[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) {
$error['email'] = "Please enter a valid email address.";
}
// Check Message
if (strlen($contact_message) < 15) {
$error['message'] = "Please enter your message. It should have at least 15 characters.";
}
// Subject
if ($subject == '') { $subject = "Contact Form Submission"; }
// Set Message
$message .= "Email from: " . $name . "<br />";
$message .= "Email address: " . $email . "<br />";
$message .= "Message: <br />";
$message .= $contact_message;
$message .= "<br /> ----- <br /> This email was sent from your site's contact form. <br />";
// Set From: header
$from = $name . " <" . $email . ">";
// Email Headers
$headers = "From: " . $from . "\r\n";
$headers .= "Reply-To: ". $email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
if (!$error) {
ini_set("sendmail_from", $siteOwnersEmail); // for windows server
$mail = mail($siteOwnersEmail, $subject, $message, $headers);
if ($mail) { echo "OK"; }
else { echo "Something went wrong. Please try again."; }
} # end if - no validation error
else {
$response = (isset($error['name'])) ? $error['name'] . "<br /> \n" : null;
$response .= (isset($error['email'])) ? $error['email'] . "<br /> \n" : null;
$response .= (isset($error['message'])) ? $error['message'] . "<br />" : null;
echo $response;
} # end if - there was a validation error
}
?>

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

PHP - why my email send code doesnt work? [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
you can see on the title.
i will post here my code.. i please if i done the code worng please
<?php
$name = $_REQUEST["name"];
$email = $_REQUEST["email"];
$mobile = $_REQUEST["mobile"];
//$msg = $_REQUEST["msg"];
$to = "email#email.com"; // <--- Change email ID here
if (isset($email) && isset($name)) {
$subject = "Cadastro para Pos Graduacao em Lideranca e Gestao Sustentavel";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= "From: <".$email.">\r\n"."Reply-To: ".$email."\r\n" ;
$msg = "From: Email: $email <br/>Mobile: $mobile" ; //<br/>Message: $msg
$mail = mail($to, $subject, $msg, $headers);
if($mail)
{
echo 'success';
}
else
{
echo 'failed';
}
}
?>
well.. this codes works on other projets.. so i dont know what im doing wrong..
if(isset($_REQUEST['email']) && !empty($_REQUEST['email'])) {
$name = $_REQUEST["name"];
$email = $_REQUEST["email"];
$mobile = $_REQUEST["mobile"];
$headers = "From: information <".$email.">\n";
$headers .= 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\n";
$to='email#email.com';
$subject = 'subjenct';
$message = "<html>
<body>
<p style='font-family:verdana; font-size:12px; margin:30px 0 0 30px; align=center'>
<strong> Information </strong> <br>
<strong>Full Name: </strong> ".$name."<br>
<strong>Email: </strong> ".$email."<br>
<strong>Mobile: </strong> ".$mobile."<br>
</p>
</body>
</html>";
$res= mail( $to, $subject, $message, $headers );
}
if($res)
{
echo 'success';
}
else
{
echo 'failed';
}

Categories