Difficulty with sending mail in php [duplicate] - php

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
I have searched stack overflow as well as the Ubuntu forums. I am writing a website and I am hosting it myself using a LAMP server. I have a form that a user fills out and want it to send an email once the user clicks the submit button. So far I have tried sendmail, ssmtp, phpmailer, and installing mailutils and using postfix. The reason I believe that it's an issue with the mail program is because after testing I'm getting no errors from the code and I am unable to send mail from the commandline. I would prefer not to use phpmailer as I don't want to hardcode any passwords in the website in the event it gets compromised. I would appreciate any help someone could give me. I have included the mail script if you need config files let me know Thank you in advance.
<?php
session_start();
if(isset($_POST['submit'])){
$to = "jmaggsdf#gmail.com"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$subject = "Website Email";
$message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];
$type = 'plain'; // or HTML
$charset = 'utf-8';
$_SESSION['from'] = $_POST['email'];
$_SESSION['first_name'] = $_POST['first_name'];
$_SESSION['last_name'] = $_POST['last_name'];
$_SESSION['message'] = $_POST['message'];
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/plain; charset=utf-8' . "\r\n";
$headers .= 'To: ' . $to . '<' . $to . '>' . "\r\n";
$headers .= 'From: ' . $from . '<' . $from . '>' . "\r\n";
if($first_name == NULL || $last_name == NULL || $from == NULL || $message == NULL){
header("Location: mailError.php");
die();
}
else{
//$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
header("Location: thanksPage.php");
//mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
//echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
}
}
?>

It you are sending mail using local host then it will not send mail.
It requires live server.

Related

I have created a contactform using mailfunction. But all the mails are going to spam.My code is given below

I have created a contactform using mailfunction. But all the mails are going to spam.My code is given below
<?php
// MAIL SEND PHP
if(isset($_POST['submit'])){
$to = "abc#gmail.com"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$email = $_POST['mail'];
$name = $_POST['name'];
$subject = "Form submission";
$subject2 = "Copy of your form submission";
$message = $email . " " . $name . " wrote the following:" . "\n\n" . $_POST['message'];
$message2 = "Here is a copy of your message " . $name . "\n\n" . $_POST['message'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
// You can also use header('Location: thank_you.php'); to redirect to another page.
}
// END MAIL SEND PHP
?>
as i previously facing this issue , you can use phpmailer for this problem. with smtp details, hope fully that will working correctly.
checkout this link- click here

how to send email to client and one one copy to me with html form [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I want to send an email without tml form when a user has finished filling in an HTML form and then emailing information from the form. I want to do it from the same script that displays the web page that has the form.
May be it's help you see this StackAnswer
just add cc and bcc in your mail function header.
Try This code :-
<?php
if(isset($_POST['submit'])){
$to = "*********#gmail.com";
$from = "client#email.com";
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$subject = "Form submission";
$message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];
$headers = "From: $from_email\r\nReply-To: $from_email";
$headers .= 'Cc:'.$yourmail.'\r\n';
mail($to,$subject,$message,$headers);
echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
}
?>
You can add CC in your email code headers
$headers .= 'Cc: yourmail#domain.net' . "\r\n";
So your email would be added to cc so whatever mail is send to user also a copy of that mail is send to you
<?php
if(isset($_POST['submit'])) {
$to = "********#gmail.com";
$from = email#mail.com;
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$subject = "Form submission";
$subject2 = "";
$message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];
$message2 = " " . $first_name . "\n\n" . $_POST['message']; $headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2);
}
?>

unable to send multiple bcc mail [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am trying to send Bcc mail .My mail function is working for single mail but when I am trying for bcc mail then I nothing happen .help me for possible solution .
<?php
if(isset($_POST['submit'])){
//$to = "example#com"; // this is your Email address
$to = "example#com";
$from = $_POST['email']; // this is the sender's Email address
$first_name = $_POST['name'];
$last_number = $_POST['number'];
$last_query = $_POST['query'];
$last_state = $_POST['state'];
$subject = "Form submission";
$subject2 = "Copy of your form submission";
$message = $first_name . " " . $last_name ." ".$last_number." ". $last_query." ".$last_state." ". " wrote the following:" . "\n\n" . $_POST['message'];
$message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
echo $mag="Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
// You can also use header('Location: thank_you.php'); to redirect to another page.
}
?>
You need to send Bcc in headers only like below:
headers .= "Bcc: addess1,addess2,etc \r\n";
you should try this format. also there is no bcc in your code
$headers = 'MIME-Version: 1.0' . "\\r\ ";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\\r\ ";
$headers .= 'To: test <test#example.com>, test1 <test1#example.com>' . "\\r\ ";
$headers .= 'From: test3 <test3#example.com>' . "\\r\ ";
$headers .= 'Cc: [email]test4#example.com[/email]' . "\\r\ ";
$headers .= 'Bcc: [email]test5#example.com,test6#myserver.com,test7#gmail.com,test8#email.com[/email]' . "\\r\ ";
mail($to, $subject, $message, $headers);

could anyone tell me what's wrong with my mail form although data is posted correctly but email is not sending. Why? [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
to submit a form using email functionality:
$to = "ayush#***.com";
$from = $_REQUEST['email'];
$name = $_REQUEST['name'];
$phone = $_REQUEST['phone'];
$message = $_REQUEST['message'];
$subject = "Form submission";
$message = $name . " " . $phone . " " .$message. " wrote the following:" . "\n\n" . $_REQUEST['message'];
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=isoo-8859-1\r\n";
$headers = "From:" . $from;
i am not able to send email
mail($to,$subject,$message,$headers);
header('Location: thank-you.php');
Your last header is broken.
$headers = "From:" . $from;
It requires the dot (concatenate) for it also.
$headers .= "From:" . $from;
Mail is rejected since there is no valid From and is caused by it being broken.

PHP - send a form to e-mail [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
I have a problem with sending the form to my e-mail.
Currently, when i send the form i get message from "emailtest#test.com" with $message2 but i don't get $message from 'inputEmail' to my e-mail "emailtest#test.com".
I would add that I am not PHP programmer and this is my first script in this language.
I would be very grateful for your help
<?php
$to = 'emailtest#test.com'; // this is your Email address
$from = $_POST['inputEmail']; // this is the sender's Email address
$first_name = $_POST['inputName'];
$inputCompany = $_POST['inputCompany'];
$inputPosition = $_POST['inputPosition'];
$inputProjects = $_POST['inputProjects'];
$inputOfficeProjects = $_POST['inputOfficeProjects'];
$inputPresentation = $_POST['inputPresentation'];
$inputMessage = $_POST['inputMessage'];
$number = $_POST['number'];
$subject = "Test";
$subject2 = "Test1";
$message = $first_name . " example " . $inputPosition . " wrote the following:" . "\n\n" . $_POST['message'];
$message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
// echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
header('Location: dziekujemy.html');
?>
Since your code works in a local environment, with mailtodisk, I suspect a "problem" with the mail server. You are setting the From: Header of the first email send attempt to the email address of the person that filled out the form. Most mail servers might reject that because it is not a valid address that you own. I am not an expert to this so someone might have a better, more detailed explanation.
Remove the headers from the first email or set the From: Header to a address which can actually sent over that mailserver. If this works you can use a Reply-to header which enables most clients to directly answer to the set email address.
Example below:
...
$headers = 'From: ' . $to . "\r\n" .
'Reply-To: '.$from;
...
I might suggest - even it it looks complicated at first - to use PHPMailer. It makes sending emails with PHP much easier.

Categories