php email going to the 'from' address not the to address - php

I have been searching for a working email php script, I have everything working except -
The email that is entered into the contact form receives the message instead of the pre-set recipient?
I have tried all sorts of tricks to try and get this to work, the only way I can get the mail to arrive in my web-mail account is if I type that address into the form as the senders email.
This is the 'html' form
<html>
<body>
<form method="post" action="new-mail.php">
<input type="text" name="name" id="name" placeholder="Name" />
<input type="text" name="contact" id="contact" placeholder="Email" />
<input type="text" name="subject" id="subject" placeholder="Subject" />
<textarea name="message" id="message" placeholder="Message"></textarea>
<input type="submit" class="button" value="Send Message" />
</form>
</body>
</html>
and this is the php mailer -
<?php
$subject = $_POST['subject'];
$message = $_POST['message'];
$email = 'net#webmail.address';
$who_sent = $_POST['contact'];
$headers = 'From: ' . $who_sent . "\r\n" .
'Reply-To: ' . $who_sent . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$name = $_POST['name'];
mail($email, $subject, $message, $name, $headers);
?>
When the mail is received at the intended senders email address it list's the recipient as being the intended send to address, and the from address is the web host 'user#server.web-host.net'
I am finding this a little confusing, I have tried setting the form id and name for the email field to different text, I have tried putting the recipients address directly into the mail(), and a good many other combinations too. For some reason it makes no difference to the outcome.
The host is 000webhost.com which is a linux based server if that makes any difference to this.

Related

PHP contact form submits but does not send mail

I recently made a website using HTML, CSS, and JS. Since I don't know PHP, I am stuck at building the contact form where it is vital on the website. I learned a bit from YouTube tutorials and have the following HTML & PHP code:
<div class="contact_form">
<form action="/action_page.php">
<input type="text" id="name" name="name" placeholder="Name*">
<input class="contact_even" type="text" id="email" name="email" placeholder="Email id*">
<input type="text" id="phone" name="phone" placeholder="Phone No.">
<input class="contact_even" type="text" id="city" name="city" placeholder="City">
<textarea id="subject" name="subject" placeholder="How Can We Help You?"></textarea>
<input type="submit" value="Submit">
</form>
</div>
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$mailFrom = $_POST['email'];
$phone = $_POST['phone'];
$city = $_POST['city'];
$message = $_POST['message'];
$mailTo = 'example#something.in';
$headers = 'From: '.$mailFrom;
$txt = $name.'('.$phone.') from '.$city.' says:\n\n'.$message;
$headers = "MIME-VERSION: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
mail($mailTo, $headers, $txt);
header("Location: index.html?mailsent");
}
?>
Why do I need the MIME and content-type headers at the bottom as that bit I added from another tutorial.
When I use the form and try sending the message, I get the "?mailsent" after the URL but I receive no email which is a professional plan by GoDaddy.
They are also hosting my website. I contacted them to know whether the server allows me to make my contact form with the plan I have and they said yes. So, I must be missing something important here.
refer to the documentation of mail function
there are 3 required parameter : email destination (to), subject and the message, and two additional options are: headers and parameters.
your code didnt respect that, because you missing to add the subjuct as parameter.
and you get ?mailsent because you use header("Location: index.html?mailsent") without any test if the email send successfully or not.
i suggest you to replace the last two lines of your php code with this
$subject = "some subject"; // you can replace it with $subject = $_POST["subject"]
$result = mail($mailTo, $subject , $txt,$headers);
if ($result){
// mail send successfully
header("Location: index.html?mailsent");
} else {
// error
}
EDIT:
you can get the error message with error_get_last() function.
thanks to https://stackoverflow.com/a/20203870/195835
$subject = "some subject"; // you can replace it with $subject = $_POST["subject"]
$result = mail($mailTo, $subject , $txt,$headers);
if ($result){
// mail send successfully
header("Location: index.html?mailsent");
} else {
print_r(error_get_last());
}
You are missing the form action, So PHP doesn't know what to do with your variable data.Try adding method="post" inside <form> tag. Like this
<div class="contact_form">
<form action="/action_page.php" method="post">
<input type="text" id="name" name="name" placeholder="Name*">
<input class="contact_even" type="text" id="email" name="email" placeholder="Email id*">
<input type="text" id="phone" name="phone" placeholder="Phone No.">
<input class="contact_even" type="text" id="city" name="city" placeholder="City">
<textarea id="subject" name="subject" placeholder="How Can We Help You?"></textarea>
<input type="submit" value="Submit">
</form>
</div>
And also. If you use your computer as localhost(using xampp , wamp, or something without a hosting service) You have to make some changes to the config files.
Also try this modified php code
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$mailFrom = $_POST['email'];
$phone = $_POST['phone'];
$city = $_POST['city'];
$message = $_POST['message'];
$title = "replace this";
$mailTo = 'support#udichi.in';
$txt = $name.'('.$phone.') from '.$city.' says:\n\n'.$message;
$headers = 'From: '.$mailFrom . PHP_EOL .'Reply-To:' .$mailFrom . PHP_EOL . 'X-Mailer: PHP/' . phpversion();
mail($mailTo,$title,$txt,$headers);
header("Location: index.html?mailsent");
}
?>

PHP mail function is sending blank message body

I am aware there are other subjects with similar questions however their answered didn't solve my problem.
I developed a website and temporarily decided to go ahead and use PHP mail as opposed to other routes as it was the quickest and easiest (I am an amateur developer). It was working flawlessly for the past month with no issues, however 3 days ago, I started receiving emails with no message contents (these are not spam or just empty messages, I am aware that I should use validation etc).
I still successfully receive the email to my email account, it also contains the subject header being "Contact Form Request", however the body is empty i.e. $msg that should be populated aren't?
I switched over the placement of "Contact Form Request" and $msg and the $msg contents would be received in the email subject header so it is being populated, however the message body remains empty.
Any help would be appreciate.
contact.html
<div id="contact-form">
<form action="contact.php" method="post">
<div class="form-group">
<label for="inputName">Name *</label>
<input type="text" class="form-control" name="name" placeholder="Enter name" required>
</div>
<div class="form-group">
<label for="inputNumber">Contact Number *</label>
<input type="tel" class="form-control" name="phone" placeholder="Enter contact number" required inputMode="tel">
</div>
<div class="form-group">
<label for="inputEmail">Email *</label>
<input type="email" class="form-control" name="email" placeholder="Enter email" required>
</div>
<div class="form-group">
<label for="inputSubject">Subject *</label>
<input type="text" class="form-control" name="subject" placeholder="Enter subject" required>
</div>
<div class="form-group">
<label for="inputMessage">Message *</label>
<textarea type="text" class="form-control" name="message" placeholder="Enter message" rows="3" required maxlength="300"></textarea>
</div>
<p><small><strong>*</strong> These fields are required.</small></p>
<button type="submit" class="btn btn-send">Send</button>
</form>
</div>
contact.php
<?php
$webmaster_email = "test#drivingschool.co.uk";
$success_page = "thankyoucontact.html";
$name = $_REQUEST['name'];
$phone = $_REQUEST['phone'];
$email = $_REQUEST['email'];
$subject = $_REQUEST['subject'];
$message = $_REQUEST['message'];
$msg =
"You have received a message from " . $name . "\r\n\n" .
"Subject: " . $subject . "\r\n\n" .
"Message: " . $message . "\r\n\n" .
"Name: " . $name . "\r\n" .
"Phone: " . $phone . "\r\n" .
"Email: " . $email . "\r\n\n\n\n" .
"DISCLAIMER:\r\n\nThis e-mail and any attachments is a confidential correspondence intended only for use of the individual or entity named above. If you are not the intended recipient or the agent responsible for delivering the message to the intended recipient, you are hereby notified that any disclosure, distribution or copying of this communication is strictly prohibited. If you have received this communication in error, please notify the sender by phone or by replying this message, and then delete this message from your system.";
mail($webmaster_email, "Contact Form Request", $msg);
header("Location: $success_page");
?>
Your code looks OK on first inspection (not SAFE, but working).
My advise:
FIRST: Debug. Just show the message instead of actual mailing. Like hereunder:
<?php
$webmaster_email = "test#drivingschool.co.uk";
$success_page = "thankyoucontact.html";
$name = $_REQUEST['name'];
$phone = $_REQUEST['phone'];
$email = $_REQUEST['email'];
$subject = $_REQUEST['subject'];
$message = $_REQUEST['message'];
$msg =
"You have received a message from " . $name . "\r\n\n" .
"Subject: " . $subject . "\r\n\n" .
"Message: " . $message . "\r\n\n" .
"Name: " . $name . "\r\n" .
"Phone: " . $phone . "\r\n" .
"Email: " . $email . "\r\n\n\n\n" .
"DISCLAIMER:\r\n\nThis e-mail and any attachments is a confidential correspondence intended only for use of the individual or entity named above. If you are not the intended recipient or the agent responsible for delivering the message to the intended recipient, you are hereby notified that any disclosure, distribution or copying of this communication is strictly prohibited. If you have received this communication in error, please notify the sender by phone or by replying this message, and then delete this message from your system.";
echo "The message contains: $msg";
exit;
// mail($webmaster_email, "Contact Form Request", $msg);
// header("Location: $success_page");
?>
If your message contains what you expect, you must look into the email gateway. This is much harder.
I would start by making a script like this, call it testmail.php with only this inside, and see if that works.
<?php
mail('test#drivingschool.co.uk', "testing", "testing.\nDoes this arrive?");
?>
EDIT: Third thing to check:
You are sending a plain text mail. Maybe your email client only displays HTML email? Please check.
EDIT: Fourth suggestion:
I would try the mail utility itself from commandline to check if your PHP is acting strange.
So if you have SHELL access (BASH or whatever) try something like this:
mail -s "Testing mail" test#drivingschool.co.uk <<< 'Testing. Is this body visible?'
If THAT fails too, go have a chat with your hosting provider.
If that works: Something is wrong with your PHP in combination with sendmail.
Here is some background:
https://www.binarytides.com/linux-mail-command-examples/
Set from email with the header. You can also use content-type text/html if message in HTML format
$from ='testmail#mail.com';
$headers = "From:" . $from . "\r\n";
$headers .= "Content-type: text/plain; charset=UTF-8" . "\r\n";
#mail($webmaster_email,$subject,$message,$headers);
Hope this work
I have the same error, I think that the mail function interprets the text:
Message: $msg
as header, this may be a bug in PHP or the way SMTP works.
Here is my code:
function send_email($to, $subject, $message) {
$headers[] = 'from: kurs#jcubic.pl';
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, implode(PHP_EOL, $headers));
}
send_email("mail#example.com", 'Subscription', "Email: $email");
it works fine when I remove the colon from the message.
send_email("mail#example.com", 'Subscription', "Email $email");

PHP message sends "undefined"

I have mail.php which sends out an email. This is the code I'm using.
<?php
if(isset($_POST['submit'])){
$to = $_POST['email'];
$subject = 'Your Results!';
$message = $_POST['message'];
$headers = 'From: example#example.com' . "\r\n" .
'Reply-To: example#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
//mail($to, $subject, $message, $headers);
//header("Location: thankyou.php");
echo $message;
}
?>
The contents of the email is supposed to the .innerText of of a div I have on my index.php page. If I echo out the message, then the output appears the page (mail.php) but when I comment/uncomment the necessary parts to email myself the message I receive is just "undefined".
Is $message not defined?
Here is the form and javascript I'm using
<form action="mail.php" method="post" onsubmit="this.message.value = document.getElementById('box').innerText;">
<input type="email" name="email" required>
<input id="content" type="hidden" name="message" value="">
<input type="submit" name="submit">
</form>
Change .innerText to .textContent. .innerText is a nonstandard property that doesn't exist in Firefox.
I don't know why you didn't have the same problem when used echo $message in the PHP script, unless you tested that version of the script with a different browser (always try to minimize the number of differences when you're testing like this).

PHP mail only reaching certain clients

I've written a simple PHP HTML email form. That's all fine, it sends the email, the email doesn't get blocked. The problem with it is that only certain mail clients are receiving the emails: Gmail gets them fine, but another email account (used through a webmail client) doesn't, and neither do email apps I try. It appeared to work for a while after adding an X-Mailer header, but then stopped again; it also worked briefly when the recipients line was strangely formatted deliberately.
The form:
<form name="email" action="send.php" method="POST" id="mailform">
* From: <input type="text" name="from" required/><br/>
* To: <input type="text" name="to" required/><br/>
* Reply-To: <input type="text" name="reply" required/><br/>
Subject: <input type="text" name="subject" /><br/>
* Message:<br/>
<textarea name="email" rows="10" cols="100" id="message" required></textarea><br/>
<input type="button" name="convert" id="convert" value="Convert and Check" />
</form>
(the 'Convert and Check' button is there because I use Markdown to format the HTML email; that part is again working fine. It changes to a Send button once the MD is converted to HTML.)
The PHP:
$from = $_POST["from"];
$to = $_POST["to"];
$reply = $_POST["reply"];
$subject = $_POST["subject"];
$message = $_POST["email"];
$headers = "Content-Type: text/html" . "\r\n"
. "Reply-To: ".$reply . "\r\n"
. "From: ".$from . "\r\n"
. "X-Mailer: PHP/".phpversion() . "\r\n";
echo "<h3>Preview</h3><div class='content-container'>";
echo "<b>To:</b> ".htmlspecialchars($to)."<br/>";
echo "<b>Headers:</b> ".htmlspecialchars($headers)."<br/><hr/>";
echo "<div class='frame'>".$message."</div></div>";
echo "<br/><h3>Status</h3>";
$send = mail($to, $subject, $message, $headers);
if($send) {
echo "Your mail was successfully accepted for delivery.";
}
else {
echo "Sending of the email failed.";
}
Any ideas? It's got me confused - why do only some clients receive this?
because most of the email providers block emails from dynamic ips.

How to send form data to multiple email addresses using PHP?

I am a web designer, and dont really know much about PHP. I have a form, and I want the values to be sent to three email addresses.
Here is my HTML:
<form id="player" method="post" action="process.php">
<label for="name">Your Name</label>
<input type="text" name="name" title="Enter your name" class="required">
<label for="phone">Daytime Phone</label>
<input type="tel" name="phone" class="required">
<label for="email">Email</label>
<input type="email" name="email" title="Enter your e-mail address" class="required email">
<input type="submit" name="submit" class="button" id="submit" value="I'd like to join Now" />
</form>
I have somehow found a PHP code that should send the data to ONE email address only, but I dont even know if it works or not.
Here is the code for that:
<?php
// Get Data
$name = strip_tags($_POST['name']);
$email = strip_tags($_POST['email']);
$phone = strip_tags($_POST['phone']);
$url = strip_tags($_POST['url']);
$message = strip_tags($_POST['message']);
// Send Message
mail( "you#youremail.com", "Contact Form Submission",
"Name: $name\nEmail: $email\nPhone: $phone\nWebsite: $url\nMessage: $message\n",
"From: Forms <forms#example.net>" );
?>
Thanks
Add headers
<?php
// Get Data
$name = strip_tags($_POST['name']);
$email = strip_tags($_POST['email']);
$phone = strip_tags($_POST['phone']);
$url = strip_tags($_POST['url']);
$message = strip_tags($_POST['message']);
$headers .="From: Forms <forms#example.net>";
$headers .="CC: Mail1 <Mail1#example.net>";
$headers .=", Mail2 <Mail2#example.net>";
// Send Message
mail( "you#youremail.com", "Contact Form Submission",
"Name: $name\nEmail: $email\nPhone: $phone\nWebsite: $url\nMessage: $message\n",
$headers );
?>
You should be able to separate email addresses with commas in the first parameter of the mail() function, i.e.
mail('email1#example.com, email2#example.com', $subject, $message, $headers);
Or sepcific CC and optionally BCC addresses as per Ahmad's answer.
The mail function (which is used in the code that you posted) allows you to specify multiple recipients. See the PHP documentation of that function for details: http://php.net/manual/en/function.mail.php
Edit: You basically need to replace the "you#youremail.com" part with a list of addresses, separated by commas, e.g.:
mail("you#youremail.com,somebody#domain.com,anotherone#domain.com", ...
use
$to = "email#email.com"
$to .= ", anotheremail#email.com";
this will help you to create multiple recipient.

Categories