Simple PHP mail form not working on server - php

I've written an simple PHP mail form but it refuses to run on one server but will on another. Both servers are running PHP 5.4 and other PHP progs run on the problematic server but mail will not send.
I've pared the mailer back to the simplest code but am stumped as why one server wont send the mail. Is there a configuration I need to activate or a test I can perform to diagnose the problem?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Simple PHP Mailer</title>
</head>
<body>
<form action="" method="post">
First Name: <input type="text" name="first_name"><br>
Last Name: <input type="text" name="last_name"><br>
Email: <input type="text" name="sender_email"><br>
Message:<br><textarea rows="5" name="message" cols="30"></textarea><br>
<input type="submit" name="subdata" value="Submit">
</form>
<?php
//Taken from: http://stackoverflow.com/questions/18379238/send-email-with-php-from-html-form-on-submit-with-the-same-script
if(isset($_POST['subdata'])){
$to = 'myname#gmail.com'; // this is your Email address
$from = $_POST['sender_email']; // this is the sender's Email address
$fname = $_POST['first_name'];
$lname = $_POST['last_name'];
$subject = "PHP Email Form Test";
$message = $fname . ' ' . $lname . ' (' . $from . ') wrote the following:' . '\n\n' . $_POST['message'];
$headers = 'From: user#gmail.com' . "\r\n" .
'Reply-To: user#gmail.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
if (mail($to, $subject, $message, $headers)) {
echo 'Mail Sent. Thank you ' . $fname . ', we will contact you shortly.';
}else{
echo 'Mail Error';
}
}
?>
</body>
</html>
Any help would be appreciated, many thanks.
Kw

Related

Sending PDF File with PHP Email

I'm trying to use the following code to try and send a PHP Email with a PDF attachment.
I get the message 'Mail Sent. Thank You'. However, No email is being sent.
Please could someone show me / explain to me where I am going wrong?
Thanks
<?php
if(isset($_POST['submit'])){
$to = "example#example.com"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$subject = "Subject Here";
$message = $_POST['message'];
$file = $_POST['upload'];
$headers = 'From: someone#gmail.com ' . "\r\n" .
'Reply-To: someone#gmail.com ' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$headers2 = "From:" . $to;
mail($to, $subject, $message, $file, $email, $headers);
echo "Mail Sent. Thank you.";
// You can also use header('Location: thank_you.php'); to redirect to another page.
}
?>
<!DOCTYPE html>
<head>
<title>Form submission</title>
</head>
<body>
<form action="" method="post">
Message:<br><textarea rows="5" name="message" cols="30"></textarea><br>
<input type="file" name="upload" accept="application/pdf,application/vnd.ms-excel" />
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
There are lot of issues with your code, and it is recommended to first check the syntax and documentation on official site. For instance your are passing $file as parameter to mail function, but there is no FILE parameter in mail function (https://www.php.net/manual/en/function.mail.php)
Then you are not saving the uploaded PDF to any disk to send from. Uploaded files are captured through $_FILES variable not $_POST variable. You need to see that too.
Then sending any file is tricky job, and your job can be ease by using some library function such as PHPMailer so try to refer those.

trying to send a email from html with php. don't receive the email [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 3 years ago.
I'm trying to make a contact form in HTML page. I've linked it with a .php file and but both on a server but I don't receive any email. Someone can see my mistake?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Contact form tutorial</title>
</head>
<body>
<main>
<p>SEND EMAIL</p>
<form action="contactform.php" method="post">
<input type="text" name="name" placeholder="Full name">
<input type="text" name="mail" placeholder="Your e-mail">
<input type="text" name="subject" placeholder="subject">
<textarea name="message" placeholder="Message"></textarea>
<button type="submit" name="submit">SEND MAIL</button>
</form>
</main>
</body>
</html>
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$subject = $_POST['subject'];
$mailFrom = $_POST['mail'];
$message = $_POST['message'];
$mailTo = "MINE#EMAIL.COM";
$headers = "From: ".$mailFrom;
$txt = "You have received an e-mail from ".$name.".\n\n" . $message;
mail($mailTo, $subject, $txt, $headers);
header("Location: index.php?mailsend"); }
I think the issue is with your headers. I altered your script to conform the headers to the docs and it worked. I also sent a version with no headers and it worked as well.
$name = 'name';
$subject = 'subject';
$mailFrom = 'MINE#EMAIL.COM';
$message = 'message';
$mailTo = 'me#realaddress.com';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$txt = "You have received an e-mail from ".$name.".\n\n".$message;
mail($mailTo, $subject, $txt, $headers);

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).

Contact Form, how does it work?

I'm right about to launch my site, but first I need to finish my contact form, to make sure users can contact me. I'm not an expert in PHP or anything close, so please to understand my ignorance.
I have my server ready and running. I have an email. I have the site. I have two documents, one contact.html and the other send.php.
My contact.html has the following content:
<!doctype html>
<html>
<head>
<title>Spotnight - About</title>
<link rel="stylesheet" href="style.css">
<link rel="Favicon" href="f.ico" type="image/x-icon">
</head>
<body>
<div class="ham_container">
<svg id="hamburger" title="Menu">
<path d="M0,5 30,5" stroke="#eeeeee" stroke-width="5"/>
<path d="M0,14 30,14" stroke="#eeeeee" stroke-width="5"/>
<path d="M0,23 30,23" stroke="#eeeeee" stroke-width="5"/>
</svg>
</div>
<nav>
<ul>
<li>Our Home</li>
<li>About Us</li>
<li>Hybon Sites</li>
<li>Orion Interactives</li>
<li>Spotnight Studios</li>
<li>Spotnight Shop</li>
<br>
<li>Contact Us</li>
</ul>
</nav>
<main>
<h1>Contact us! :)</h1>
<h5>Spotnight</h5>
<div>
<form action="send.php" method="POST">
<input type="text" name="first_name" placeholder="Your first name, eg: 'John'">
<input type="text" name="last_name" placeholder="Your last name, eg: 'Smith'">
<input type="email" name="email" placeholder="Your Email, eg: 'yourname#example.com'">
<select name="subj" size="1">
<option value="new_website">I need a website</option>
<option value="new_game">I want a game</option>
<option value="new_employee">I want to be part of the team</option>
<option value="new_employee_game">I want to participate making games</option>
<option value="new_employee_site">I want to participate building sites</option>
<option value="new_artist">I'm an artist and need help</option>
<option value="something_else">Something else!</option>
</select>
<textarea name="message" placeholder="Your Message Goes Here!"></textarea>
<input type="submit" value="Send!">
</form>
</div>
</main>
<footer>
© <span id="year">2015</span> : Spotnight.io : Proudly created by Hybon.
</footer>
</body>
<script src="//code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="script.js"></script>
</html>
My send.php document has the following content:
<!doctype html>
<html>
<head>
</head>
<body>
<?php
$first_name = $_POST["first_name"];
$last_name = $_POST["last_name"];
$email = $_POST["email"];
$subj = $_POST["subj"];
$message = $_POST["message"];
$formcontent="From: " . $first_name . $last_name . ", " . $email . "\n Subject: " . $subj . "\n Message: " . $message;
$recipient = "julian-avar#spotnight.io";
$subject = "Contact Form - " . $subj . " - Spotnight.io";
$mailheader = $email . " has tryed to reach you! \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You! Your message has been recived, we&apos;ll answer as soon as possible!" . $first_name . $last_name . " -" . "Go Back!";
?>
</body>
</html>
This is all literal, meaning, the code is exactly as right there. I'm having trouble understanding it, and I still don't understand how it works.
So if you are a super duper awesome php expert, please help me understand and fix this code so I can deal with it later, and so other people understand how it works.
I have searched many places, but the simplest form of contact form doesn't seen to appear in the world archives. Thank you very much!
echo "Thank You! Your message has been recived, we&apos;ll answer as soon as possible!" . $first_name . $last_name . " -" . "Go Back!";
Here's the problem. The SO's code highliter highlights it badly because of it.
Use this instead:
echo /* part of code missing */ $first_name . $last_name . " -" . "<a href='contact.html'>Go Back!</a>";
// mind the ' signs here ^^^^^^^^^^^^^
You cannot include a quote in another quote without escaping the "'s or using a single-quote (I might have been mistaken in terminology) sign.
Your 'send.php' script mixes html with php so unless you are using output buffering you will encounter an error when it tries to send the email due to html content already being sent to the client/browser. I would suggest that the send.php script is pure php only. Also, the mailheader variable - this looks wrong to me - I think it should contain, at the very least, the From: email#domain.com field. From the PHP manual:
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
<?php
/* send.php */
$first_name = $_POST["first_name"];
$last_name = $_POST["last_name"];
$email = $_POST["email"];
$subj = $_POST["subj"];
$message = $_POST["message"];
$formcontent="From: " . $first_name . $last_name . ", " . $email . "\n Subject: " . $subj . "\n Message: " . $message;
$recipient = "julian-avar#spotnight.io";
$subject = "Contact Form - " . $subj . " - Spotnight.io";
$mailheader = "From: ".$email . "\r\n";
$res = mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
if( $res ) header( 'location:/index.php?mailsent=true' );
else header( 'location:/index.php?mailsent=false' );
?>

PHP not sending email from html

I'm trying to make a form where the user introduces data and it gets submitted to e-mail.
However, it gets me to a blank page (file.php page), and the e-mail is not sent.
HTML code:
<form action="./sendmail.php" method="post">
<div class="log">
<input type="text" id="name" value="name" name="studname" class="form">
<input type="email" id="mail" value="mail#gmail.com" name="studmail" class="form">
<input type="tel" id="age" value="age" class="form" name="studage">
<input type="submit" value="submit!" id="submit">
</div>
</form>
And now here is my "sendmail.php" code:
<?php
/* STUDENT PARAMETERS */
$studentName = $_POST['studname'];
$studentMail = $_POST['studmail'];
$studentAge = $_POST['studage'];
mail('code#gmail.com', 'Message',
'NAME: ' . $studentName . '\nMAIL: ' . $studentMail . '\nAge: ' . $studentAge);
?>
How can I solve this and why is this not working? Thank you for your time.
I think you can use phpmailer class
#Telmo Vaz try this
<?php
$studentName = $_POST['studname'];
$studentMail = $_POST['studmail'];
$studentAge = $_POST['studage'];
$recipient='code#gmail.com';
$subject="Message";
$content = "New Message Form Contact us\n From: ".$studentName .", \n Email: ".$studentMail .",\n Age: ".$studentAge;
$headers = 'From: code#gmail.com' . "\r\n" .
'Reply-To: code#gmail.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$retval=mail($recipient, $subject, $content, $headers);
if( $retval == true ){do something;}
else{echo "Error Sending Message";}
?>
You need an email address to send the email from. The email won't be send from this address, but the selected address will be shown in the "From" section. Try this:
/* STUDENT PARAMETERS */
$studentName = $_POST['studname'];
$studentMail = $_POST['studmail'];
$studentAge = $_POST['studage'];
$to = "code#gmail.com";
$subject = "Message";
$message = "NAME: " . $studentName . "\nMAIL: " . $studentMail . "\nAge: " . $studentAge;
$from = $to;
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";

Categories