This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
I've tried to built a php form. However, it won't send.
html (index.html):
<div class="row">
<div id="Contact" class="container">
<h1>Meer weten?</h1>
<h2>Neem vrijblijvend contact op</h2>
<div id="ContactBlock">
<form class="contactform" method="post" action="submitform.php">
<div class="FormTop">
<div class="half-left-cf">
<input type="text" id="input-name" name="name" placeholder="Naam (verplicht)">
<input type="text" id="input-email" name="email" placeholder="Email address (verplicht)">
<input type="text" id="input-telnr" name="telnr" placeholder="Telefoonnummer">
</div>
<div class="half-right-cf">
<textarea name="message" type="text" id="input-message" name="message" placeholder="Uw bericht"></textarea>
</div>
</div>
<label>*Hoeveel is is 2+2? (Anti-spam)</label>
<input name="human" placeholder="Antwoord hier">
<input type="submit" value="Verzenden" id="input-submit">
</form>
</div>
</div>
php (submitform.php):
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$telnr = $_POST['telnr'];
$message = $_POST['message'];
$from = 'From: Contact Form';
$to = 'e-mail';
$subject = 'Hello';
$human = $_POST['human'];
$body = "Afkomstig van: $name\n E-Mail: $email\n Telnr:\n $telnr Bericht:\n $message";
if ($_POST['submit']) {
if ($name != '' && $email != '') {
if ($human == '4') {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Uw bericht werd goed verzonden. Wij contacteren u zo snel mogelijk.</p>';
} else {
echo '<p>Er is iets misgegaan, probeer a.u.b. opnieuw!</p>';
}
} else if ($_POST['submit'] && $human != '4') {
echo '<p>U heeft de anti-spam vraag niet correct ingevuld</p>';
}
} else {
echo '<p>U heeft niet alle verplichte velden ingevuld</p>';
}
}
?>
When I press submit the browser returns a blanc page with /.../submitform.php in the url-bar.
This happens both in an online environment as well as on localhost.
Am I doing something wrong? Thanks
If you not sure about your folder structure just use this simple trick.
Change post url to absolute one like http://www.example.com/xyz/submitform.php
In local
http://www.localhost/xyz/submitform.php
Instead of trying simple submitform.php
Related
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
this is my site:
http://feriapixel.cl...nway/index.html
I've been trying other stuff and now i have the php code and the form in the same index.php file.
The form now looks like this
<form action="index.php" method="post">
<div class="form-group">
<input type="text" class="form-control" id="nombre" placeholder="Nombre" name="nombre">
</div>
<div class="form-group">
<input type="text" class="form-control" id="apellido" placeholder="Apellido" name="apellido">
</div>
<div class="form-group">
<input type="text" class="form-control" id="telefono" placeholder="Número Móvil" name="telefono">
</div>
<div class="form-group">
<input type="mail" class="form-control" id="email" placeholder="Email" name="email">
</div>
<button type="submit" name="submit" id="submit" class="btn btn-ganarplata">QUIERO GANAR MÁS PLATA<br> VENDIENDO CLEAN WAY</button>
</form>
And i have the php at the beginning of the file like this:
<?php
$nombre = $_POST['nombre'];
$apellido = $_POST['apellido'];
$telefono = $_POST['telefono'];
$mail = $_POST['mail'];
$from = 'From: Cleanway';
$to = 'alteizen#gmail.com';
$subject = 'Formulario de contacto Cleanway';
$body ="De: $nombre\n $apellido\n E-Mail: $mail\n Fono: $telefono\n ";
?>
<?
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Su mensaje ha sido enviado</p>';
} else {
echo '<p>No se pudo mandar su mensaje</p>';
}
}
?>
And now i have checked out and it is not a hosting issue, because if i take out the following if statement:
if ($_POST['submit']) {
The mail is automatically sent when i load the page. So the problem is with the if statement, something is wrong there.
Problem solved, this was missing:
QUIERO GANAR MÁS PLATA VENDIENDO CLEAN WAY
The button needed a "submit" value for sending the mail. That was all, thanks to me.
You need to add an email address to $headers so that when you mail, the from address can be seen. Actually, your web server needs an MX record. Use it like this.
NOTE: If you use an email that is not your website's email(like xxx#gmail.com) it won't gonna work.
$headers .= "From: Name <email#example.com>";
So I think this will gonna work.
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
I have a PHP mail script set up and keep hitting the error "Something went wrong, go back and try again!" all the form fields have been checked and all the names match etc so I am wondering if there is something wrong with my script?
<form method="post" action="contact.php" id="contactForm">
<label for="name">Name</label>
<input type="text" id="name" name="name" class="name" />
<label for="email">Email</label>
<input type="text" id="email" name="email" class="email" />
<label for="phone">Phone</label>
<input type="text" id="phone" name="phone" class="phone" />
<label for="iam">I Am</label>
<select name="iam" class="iam" id="iam">
<option>a recruiter looking to recruit staff</option>
<option>a construction worker looking for work</option>
<option>requesting information</option>
</select>
<label for="message">Message</label>
<textarea name="message" id="message" class="message"></textarea>
<label for="captcha">What is 3+4?</label>
<input type="text" id="captcha" name="captcha" class="captcha" />
<input type="submit" name="submit" id="submit" class="submit" />
</form>
<?php
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$iam = $_POST['iam'];
$human = $_POST['captcha'];
$message = $_POST['message'];
$from = 'From: Test';
$to = 'sales#test.com';
$headers = "From: $email";
$subject = 'Tradeline Contact';
$body = "From: $name\n E-Mail: $email\n Phone Number:\n $phone I Am:\n $iam Message:\n $message";
if ($_POST['submit'] && $human == '7') {
if (mail($to, $subject, $body, $headers, "-f " . $from)) {
echo '<p>Your message has been sent!</p>';
header( 'Location: http://urlhere.com/thankyou.html' ) ;
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else if ($_POST['submit'] && $human != '7') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
?>
Any help is appreciated.
You might want to change:
mail($to, $subject, $body, $headers, "-f " . $from)
to:
mail($to, $subject, $body, $headers."\r\n")
That way your mail headers will be in compliance.
Also, turn on error reporting. I happen to use error_reporting(7); right under the <?php line to turn on all common errors with the exception of catching undefined variables, and that will tell me if the mail function has problems.
Another thing you can do is check the mail server logs to see if mail is actually being sent.
I'm sure you already did this, but in case you haven't, make sure you use valid email addresses.
what do I need to change in the following code that the user enters his email address in the form. In the mail I receive, the user's email should automatically be the sender - so that I can easily reply.
Thanks and regards
<?php
$zieladresse = 'IhrName#EXAMPLE.com';
$absenderadresse = 'ihrAndererName#EXAMPLE.com';
// Welcher Absendername soll verwendet werden?
$absendername = 'Formmailer';
$betreff = 'Feedback';
$urlDankeSeite = 'http://www.EXAMPLE.com/IhrName/IhreSeite.htm';
$trenner = ":\t"; // Doppelpunkt + Tabulator
require_once "Swift-4.0.7/lib/swift_required.php"; // Swift initialisieren
if ($_SERVER['REQUEST_METHOD'] === "POST") {
$message = Swift_Message::newInstance(); // Ein Objekt für die Mailnachricht.
$message
->setFrom(array($absenderadresse => $absendername))
->setTo(array($zieladresse)) // alternativ existiert setCc() und setBcc()
->setSubject($betreff);
$mailtext = "";
foreach ($_POST as $name => $wert) {
if (is_array($wert)) {
foreach ($wert as $einzelwert) {
$mailtext .= $name.$trenner.$einzelwert."\n";
}
} else {
$mailtext .= $name.$trenner.$wert."\n";
}
}
$message->setBody($mailtext, 'text/plain');
$mailer = Swift_Mailer::newInstance(Swift_MailTransport::newInstance());
$result = $mailer->send($message);
if ($result == 0) {
die("Mail konnte nicht versandt werden.");
}
header("Location: $urlDankeSeite");
exit;
}
header("Content-type: text/html; charset=utf-8");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de">
<head>
<title>Einfacher PHP-Formmailer</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<h1>Beispielformular</h1>
<form action="" method="post">
<!-- Hier die eigentlichen Formularfelder eintragen. Die folgenden sind Beispielangaben. -->
<dl>
<dt>Ihr Name:</dt>
<dd><input type="text" name="Versender" /></dd>
<dt>Ihre E-Mail:</dt>
<dd><input type="text" name="E-Mail" /></dd>
<dt>Sie können:</dt>
<dd><input type="checkbox" name="kannwas[]" value="HTML" />HTML <input type="checkbox" name="kannwas[]" value="PHP" />PHP</dd>
<dt>Sie sind:</dt>
<dd><input type="radio" name="sexus" value="M" />Mann <input type="radio" name="sexus" value="Frau" />Frau</dd>
<dt>Sie mögen:</dt>
<dd><select name="Browser"><option value="Opera">Opera</option><option value="Mozilla">Mozilla</option></select></dd>
<dt>Bemerkungen:</dt>
<dd><textarea name="Bemerkungen" rows="3" cols="20">Bemerkungen</textarea></dd>
</dl>
<!-- Ende der Beispielangaben -->
<p>
<input type="submit" value="Senden" />
<input type="reset" value="Zurücksetzen" />
</p>
</form>
</body>
</html>
What about the following code, it also works, seems not too complicated. What is to be adapted here so that the sender's email is automatically displayed in email program as the sender to uickly reply?
html/php:
<form method="post" action="Untitled-6.php">
<label>Name*</label>
<input name="name" placeholder="Type Here">
<label>Email*</label>
<input name="email" type="email" placeholder="Type Here">
<label>Message</label>
<textarea name="message" placeholder="Type Here"></textarea>
<label>*What is 2+2? (Anti-spam)</label>
<input name="human" placeholder="Type Here">
<input id="submit" name="submit" type="submit" value="Submit">
</form>
php:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: TangledDemo';
$to = 'ceyca82#gmail.com';
$subject = 'Hello';
$human = $_POST['human'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit'] && $human == '4') {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else if ($_POST['submit'] && $human != '4') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
?>
$absenderadresse = $_POST["E-Mail"];
But there's several problems here:
1) You shouldn't post this kind of questions in stack overflow (https://stackoverflow.com/tour)
2) It's plenty of form processing tutorials out there. (Read at least one)
3) That var... "E-Mail"... You should consider calling it "email" (by convention)
I've searched stack but couldn't find something similar. I found a nice php script with a robot attached to the form. It works when I send a message and I do receive it. BUT, the email bit doesn't show up in the mail. Maybe you can find something more that is wrong. It's all in Swedish so nevermind the text :P
<?php
if($_POST){
$to = 'my#mail.com';
$subject = 'Portfolio-mail';
$from_name = $_POST['name'];
$from_email = $_POST['email'];
$message = $_POST['message'];
$robotest = $_POST['robotest'];
if($robotest)
$error = "* Misstänkt för att vara en robot, vad god försök igen!";
else{
if($from_name && $from_email && $message){
$header = "Från: $from_name, $from_email";
if(mail($to, $from_email, $from_name, $message))
$success = "Ditt meddelande har skickats!";
else
$error = "* Du är mänsklig, men det var ett fel med din förfrågan!";
}else
$error = "* Alla fält måste vara ifyllda!";
}
}
?>
And for the input bit:
<form action="" method="POST" autocomplete="off">
<div class="formLeft">
<label>För- & Efternamn:</label>
<input type="text" id="name" name="name" placeholder="John Doe" required="required">
<label>Email-adress:</label>
<input type="text" id="email" name="email" placeholder="Fyll i din email-adress!" required="required">
</div>
<div class="formRight">
<label>Skriv gärna in några ord angående projektet.</label>
<textarea id="textarea" name="message" placeholder="Klicka i denna ruta för att börja skriva..." required="required"></textarea>
<p class="robotic" id="pot">
<label>If you're human leave this blank:</label>
<input name="robotest" type="text" id="robotest" class="robotest" />
</p>
<input type="submit" value="Skicka!" />
</div>
</form>
That's it! If you need more information, don't hesitate to ask!
You are using the php mail function incorrectly:
mail($to,$subject,$message,$headers);
You have
if(mail($to, $from_email, $from_name, $message))
With your code you have many of these variables you just aren't using them, it should be
if(mail($to, $subject, $message, $header))
In the future please at least look at the php documentation before posting a question. http://php.net/manual/en/function.mail.php
I'm building a php contact form that checks if some fields are filled it or not (no ajax).
The form is not working for 100%. I will try to explain. When I submit my form, The user navigates to http://mydomain.com/send.php
There is receive my error messages (if there are any). When I refresh the send.php page I receive the following errors: Notice: Undefined index: name in /Library/WebServer/Documents/~aledvertising/send.php
Here is my code
html form
<form method="post" action="send.php">
<div id="form-top">
</div>
<div id="form-left">
<label>Naam:<span class="star">*</span></label>
<input name="name" placeholder="Uw naam">
<label>Email:<span class="star">*</span></label>
<input name="email" type="email" placeholder="Uw email">
<label>Hoeveel is 2+2? (Anti-spam)<span class="star">*</span></label>
<input name="human" placeholder="Uw antwoord">
</div>
<div id="form-right">
<label>Uw bericht:<span class="star">*</span></label>
<textarea name="message" placeholder="Uw bericht"></textarea>
</div>
<input id="submit" name="submit" type="submit" value="Verzenden">
</form>
SEND.PHP
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: ' . $_POST['email'];
$to = 'myemail#mail.com' . ', ';
$to .= $_POST['email'];
$subject = 'Uw vraag op www.aledvertising.be';
$human = $_POST['human'];
$body = "From: $name\n E-Mail: $email\nMessage:\n $message";
?>
<?php
if ($_POST['submit']) {
if ($name != '' && $email != '' && $message != '') {
if ($human == '4') {
if (mail ($to, $subject, $body, $from)) {
echo '<p class="green">Uw bericht is succesvol verzonden.</p>';
} else {
echo '<p>Er iets misgelopen, probeer opnieuw aub.</p>';
}
} else if ($_POST['submit'] && $human != '4') {
echo '<p>2+2 is niet gelijk aan het getal dat u hebt ingevoerd.</p>';
}
} else {
echo '<p>Alle velden met een * zijn verplicht in te vullen.</p>';
}
}
?>
Can somebody please help me to get a full working form? Thank you
You should see isset method from php.
<?php
if(isset($_POST["name"]))
{
//code here
}
?>
The problem is that when you reload the page, the server side code looks for the index name and it does not find it because name does not have any value yet. The value is received once you submit the page. So that's why the error is there.
update
The error is that when you submit the form, there are some fields that may be send empty, so once that the post values goes to send.php there are $_POST that will be expecting values. if some of them do not recive does values, it will tell you: "Hey, i do not have values, so i im null". That why you have to check that the fields are all filled or make a validation on server side.
<?php
if(isset($_POST["name"],$_POST["last_name"],etc,etc))
{
//if everything ok, ill go on.
}
else{
//if there are empty field, go back
}
?>
UPDATE 2
if (isset($_POST['name'],$_POST['email'],$_POST['message'])){
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: ' . $_POST['email'];
$to = 'myemail#mail.com' . ', ';
$to .= $_POST['email'];
$subject = 'Uw vraag op www.aledvertising.be';
$human = $_POST['human'];
$body = "From: $name\n E-Mail: $email\nMessage:\n $message";
if ($name != '' && $email != '' && $message != '') {
if ($human == '4') {
if (mail ($to, $subject, $body, $from)) {
echo '<p class="green">Uw bericht is succesvol verzonden.</p>';
} else {
echo '<p>Er iets misgelopen, probeer opnieuw aub.</p>';
}
} else if ($_POST['submit'] && $human != '4') {
echo '<p>2+2 is niet gelijk aan het getal dat u hebt ingevoerd.</p>';
}
} else {
echo '<p>Alle velden met een * zijn verplicht in te vullen.</p>';
}
}
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: ' . $_POST['email'];
$to = 'myemail#mail.com' . ', ';
$to .= $_POST['email'];
$subject = 'Uw vraag op www.aledvertising.be';
$human = $_POST['human'];
$body = "From: $name\n E-Mail: $email\nMessage:\n $message";
if ($name != '' && $email != '' && $message != '') {
if ($human == '4') {
if (mail ($to, $subject, $body, $from)) {
echo '<p class="green">Uw bericht is succesvol verzonden.</p>';
} else {
echo '<p>Er iets misgelopen, probeer opnieuw aub.</p>';
}
} else if ($_POST['submit'] && $human != '4') {
echo '<p>2+2 is niet gelijk aan het getal dat u hebt ingevoerd.</p>';
}
} else {
echo '<p>Alle velden met een * zijn verplicht in te vullen.</p>';
}
}
?>
setting the isset() function and then declaring the variables worked for me, tested it in my server, not giving any errors and form is submitted successfully...
please check it once...