Been trying to get this working and sending to my email from the website but the emails aren't coming through.
It is probably something really easy. I also blocked my actual sending email (for both areas)
Thanks for any help!
My php code in index.php
<?php
$field_name = $_POST['name'];
$field_email = $_POST['email'];
$field_message = $_POST['message'];
ini_set("SMTP","ssl://smtp.gmail.com");
ini_set("smtp_port","465");
$mail_to = 'coverupemail#email.com';
$subject = 'Message from a portfolio visitor '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$email."\r\n";
$headers .= 'Reply-To: '.$email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message.');
window.location = 'index.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email manually to coverupemail#email.com');
window.location = 'index.html';
</script>
<?php
}
?>
My HTML code linking to index.php
<form method="post" action="index.php">
<p class="contact">
<input type="text" name="name" id="name" value="" size="22" />
<label for="name"><small>Name (required)</small></label>
</p>
<p class="contact">
<input type="email" name="email" id="email" value="" size="22" />
<label for="email"><small>Mail (required)</small></label>
</p>
<p class="contact">
<textarea name="message" id="message" value="" rows="10"></textarea>
<label for="message"><small>Message (required)</small></label>
</p>
<p class="contact">
<input id="submit" name="submit" type="submit" value="Submit" />
<input name="reset" type="reset" id="reset" tabindex="5" value="Reset Form" />
</p>
</form>
What is the $mail_status value ? Even if its "1" it does not guarantee the sending of mail :( it then depends upon the server PHP returns 1 the moment the message is handed over to the mail sending part of the server.
Cases 1 : if the script is running on server then try a sample code to verify that actually mail are been sent create a page mailtest.php with the following code
<?php mail("coverupemail#email.com","Test Msg","Hello this is just test message");?>
this will verify that yes its working.
Case 2 : if you are working on a localhost then u will have to have some SMTP server or mail server to make it easy you can use gmail to send via your localhost ( i did this i use my gmail account )
Also see this link Email sending
btw
a. ini_set() i think is not used.
b. also window.location = 'index.html'; will just navigate to the index page wat if the person navigates back ? the mail wud be sent again maybe... so i wud suggest to use
location.replace('index.html');
Thx
Related
This question already has answers here:
PHP code is not being executed, but the code shows in the browser source code
(35 answers)
Closed 7 years ago.
I'm completely new to HTML and PHP and am trying to build a simple contact form, so that users can send me a message on the contact page, which then gets directed to my email. Any ideas what I'm doing wrong here:
contact.html:
<section id="secondary">
<form action="contact.php" method="post" class="contactform">
Your Name<br>
<input type="text" maxlength="100" name="cf_name" style="width: 300px" class="contactboxes"><br>
Your E-mail<br>
<input type="text" maxlength="100" name="cf_email" style="width: 300px" class="contactboxes"><br>
The Reason for Contact<br>
<textarea name="cf_message" maxlength="500" style="width: 300px" class="contactboxes"></textarea><br>
<input type="submit" value="Send" class="sendclearbutton">
<input type="reset" value="Clear" class="sendclearbutton">
</form>
</section>
contact.php:
<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_message = $_POST['cf_message'];
$mail_to = 'sharan#hotmail.co.uk';
$subject = 'Message from a site visitor '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. We will contact you shortly.');
window.location = 'contact.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to sharan#hotmail.co.uk.');
window.location = 'contact.html';
</script>
<?php
}
?>
It looks fine to me, as Lance said, the only reason I can think that it wont be working is that you don't have PHP installed. Download Apache or whatever suits your computer best or use web hosting online. Do say if other code is working, though.
I have three email forms, which is allowing the user to send a message to one of my three emails, without having to write their own email. The forms are working and is sending the email, the problem is that the information such as the email sender and the message for the email, only works on my "support" form but not for the two other forms ("business","other").
I am not sure what exactly is wrong.
Important note, the reason there is three forms like I have done it, is because I have made three buttons called "business" "support" "other" and then when you click one of the buttons, the specific form appears.
html script with forms inside.
<!-- SUPPORT CONTACT FORM START-->
<div class="contactSupportButton"><input type="image" src="supportContactButtonNew.png" id="contactSupportBut" alt="contact support button" style="height: 40px; width: 100px" onClick="showSupForm()"/>
<div id="contactSupportForm">
<form action="supFormSend.php" method="post" id="contactForms">
<div id="nameLabelForm">
<label for="name">Name:</label><br>
<input type="text" id="nameInput" name="nameInput"/>
</div>
<div id="emailLabelForm">
<label for="mail">E-mail:</label><br>
<input type="email" id="mailInput" name="mailInput"/>
</div>
<div id="messageLabelForm">
<label for="msg">Support Message:</label><br>
<textarea id="messageInput" name="messageInput"></textarea>
</div>
<div class="submitEmailButton">
<button type="submit" id="submitButton">Send message</button>
</div>
</form>
</div>
</div>
<!-- SUPPORT CONTACT FORM ENDING-->
<!-- BUSINESS CONTACT FORM START-->
<div class="contactBusinessButton"><input type="image" src="businessContactButtonNew.png" id="contactBusinessBut" alt="contact business button" style="height: 40px; width: 110px" onClick="showBusForm()"/>
<div id="contactBusinessForm">
<form action="busFormSend.php" method="post" id="contactForms">
<div id="nameLabelForm">
<label for="name">Name:</label><br>
<input type="text" id="nameInput"/>
</div>
<div id="emailLabelForm">
<label for="mail">E-mail:</label><br>
<input type="email" id="mailInput" />
</div>
<div id="messageLabelForm">
<label for="msg">Business Message:</label><br>
<textarea id="messageInput"></textarea>
</div>
<div class="submitEmailButton">
<button type="submit" id="submitButton">Send message</button>
</div>
</form>
</div>
</div>
<!-- BUSINESS CONTACT FORM ENDING-->
<!-- OTHER CONTACT FORM START-->
<div class="contactOtherButton"><input type="image" src="otherContactButtonNew.png" id="contactOtherBut" alt="contact other button" style="height: 40px; width: 110px" onClick="showOtherForm()"/>
<div id="contactOtherForm">
<form action="otherFormSend.php" method="post" id="contactForms">
<div id="nameLabelForm">
<label for="name">Name:</label><br>
<input type="text" id="nameInput"/>
</div>
<div id="emailLabelForm">
<label for="mail">E-mail:</label><br>
<input type="email" id="mailInput" />
</div>
<div id="messageLabelForm">
<label for="msg">Other Message:</label><br>
<textarea id="messageInput"></textarea>
</div>
<div class="submitEmailButton">
<button type="submit" id="submitButton">Send message</button>
</div>
</form>
</div>
</div>
<!-- OTHER CONTACT FORM ENDING-->
php script's which sends the message to my emails.
Support form (supFormSend.php):
<?php
$field_name = $_POST['nameInput'];
$field_email = $_POST['mailInput'];
$field_message = $_POST['messageInput'];
$mail_to = 'support#myemail.com';
$subject = 'Message regarding support from '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. We will contact you shortly.');
window.location = 'contact.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to support#myemail.com');
window.location = 'contact.html';
</script>
<?php
}
header('Location: index.html');
exit;
?>
Business form (busFormSend.php):
<?php
$field_name = $_POST['nameInput'];
$field_email = $_POST['mailInput'];
$field_message = $_POST['messageInput'];
$mail_to = 'business#myemail.com';
$subject = 'Message regarding business from '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. We will contact you shortly.');
window.location = 'contact.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to business#myemail.com');
window.location = 'contact.html';
</script>
<?php
}
header('Location: index.html');
exit;
?>
Other form (otherFormSend.php):
<?php
$field_name = $_POST['nameInput'];
$field_email = $_POST['mailInput'];
$field_message = $_POST['messageInput'];
$mail_to = 'other#myemail.com';
$subject = 'Message regarding other from '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. We will contact you shortly.');
window.location = 'contact.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to other#myemail.com');
window.location = 'contact.html';
</script>
<?php
}
header('Location: index.html');
exit;
?>
HTML forms rely on the name attribute to post the data to the backend (in this case PHP).
When you reference the variable $_POST['nameInput'] in PHP, it's value will take the value of the field in the form where the name attribute is 'nameInput'. For example,
<input type="text" id="nameInput" name="nameInput"/>.
Your form #contactSupportForm is working because the name attributes are set for the the values you are referencing in your PHP.
To get the other two forms working add the name attribute to all your inputs in the HTML (and make sure the value of the name attribute matches what you reference in your PHP) just like you did in the contactSupportForm.
Jon Stirling gave the answer to your problem. Additionally, if the forms are on one page, you should change the id values of all the different html elements. An id should be unique on a page. It doesn't have to give you an error, but it's wrong and if you start using jquery/javascript it might lead to problems in the future.
For example you have three times id="nameInput"
Since you set the From: to be from your visitor, your webserver will originate an email with a From: for just about any address out there.
This is a problem as the anti-spam measures in place will prevent messages from getting delivered if your email server or service checks SPF records.
Better to send the email from a noreply#example.com and have those that would want to react use the reply-to: header ... (not that hard).
The main difference: now it's your domain's SPF record that matters and more importantly, the originating domain won't start to complain about bounces they get for mail they didn't originate.
Use "name" on all your forms to identify input fields, ie not "id"
correct in first form:
<input type="text" id="nameInput" name="nameInput"/>
incorrect in other forms:
<input type="email" id="mailInput" />
I have been working with this for a couple days and have read thru MANY previously posed questions, as well as several tutorial sites... The Contact Form submits, confirms and redirects... It sends the email, with headers and subject line, to correct address... just NO inputs... no "FROM SENDER EMAIL", no FROM NAME INPUT, no EMAIL INPUT, no MESSAGE INPUT... HELP! So close, and yet so far... any advice is greatly appreciated ~ I write HTML & CSS, novice at JS and practically NO PHP experience... Thank You in Advance! :)
Here's the HTML contact-form:
<form action= "contact-form.php" id="contact-form" method="post">
<fieldset>
<label><input name="cf-name" type="text" value="Your Name" onBlur="if(this.value=='') this.value='Your Name'" onFocus="if(this.value =='Your Name' ) this.value=''" /></label>
<label>
<input name="cf-email" type="text" value="Email" onBlur="if(this.value=='') this.value='Email'" onFocus="if(this.value =='Email' ) this.value=''" />
</label>
<textarea name="cf-message" onBlur="if(this.value=='') this.value='Message'" onFocus="if(this.value =='Message' ) this.value=''">Message</textarea>
<div class="buttons">
<p>Clear
Send</p>
<p>
</p>
</div>
</fieldset>
</form>
and here is the PHP (contact-form.php):
<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_message = $_POST['cf_message'];
$mail_to = 'info#anelectrician.net';
$subject = 'Message from a website visitor...'.$field_name;
$body_message = 'From:'.$field_name."\n";
$body_message .= 'E-mail:'.$field_email."\n";
$body_message .= 'Message:'.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for your correspondence. We will contact you shortly.');
window.location = 'index.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to info#anelectrician.net');
window.location = 'contact.html';
</script>
<?php
}
?>
You seem to have a difference here... In PHP you are using underscores and in HTML you are using dashes.
You can always test what is going wrong and where your code is by debugging with:
print_r($_POST);
That should help you figure out if you are just not using the correct keys.
Change these
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_message = $_POST['cf_message'];
to these
$field_name = $_POST['cf-name'];
$field_email = $_POST['cf-email'];
$field_message = $_POST['cf-message'];
The items in your $_POST need to match the name attributes of the inputs. Your inputs have dashes, but your $_POST references have underscores. For example:
<input name="cf-name" ... > should be <input name="cf_name" ...>
Your html inputs have dashes while your PHP name inputs have underscores
I'm trying to add a contact form to my website but each time I click send it takes me to a black window and the address bar says contact.php. Also it doesn't redirect back.
My website is hosted as an application using the google app engine.
I tried literally everything but I can't get it to work.
Below is my HTML code:
<form action="contact.php" method="post">
<p>Your name</p>
<input type="text" name="name"/>
<p>Your e-mail</p>
<input type="text" name="email"/>
<p>Message</p>
<textarea name="message"></textarea>
<input type="submit" value="Send"/>
<input type="reset" value="Clear"/>
</form>
and here is my PHP code:
<?php
$field_name = $_POST['name'];
$field_email = $_POST['email'];
$field_message = $_POST['message'];
$mail_to = 'email#here.com';
$subject = 'Message from a site visitor '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message.');
window.location = 'contact.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed.');
window.location = 'contact.html';
</script>
<?php
}
?>
You need a mail server, PHP doesn't sent mails by itself. Install one first. And yea, text area is missing close tag.
You're missing the closing textarea tag in your form. I tried the code as contact.php on my server and it works. I agree with itachi, you need to turn on error reporting.
Why not make contact.php a full page and get rid of the redirect altogether?
Working fine for me. Your if else condition is not working properly if fields has no values then still it gives thank you message.
I created a form that is linked to a PHP page like this:
<form method="get" action="contact.php">
<p>Nome:</p> <input type="text" name="name" class="name" /><br>
<p>E-mail :</p> <input type="text" name="mail" class="mail" /><br>
<p>Website:</p> <input type="text" name="website" class="website" /><br>
Mensagem: <textarea name="message" class="message" rows="2" cols="20"></textarea>
<input type="submit" value="Enviar" class="submit" />
</form>
The PHP page:
<?php
$field_name = $_GET['name'];
$field_email = $_GET['mail'];
$field_website = $_GET['website'];
$field_message = $_GET['message'];
$mail_to = '__munged__#gmail.com';
$subject = 'Message from a site visitor ' . $field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;
$headers = "From: $field_email\r\n";
$headers .= "Reply-To: $field_email\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. We will contact you shortly.');
window.location = 'index.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
// Print a message
alert('Message failed. Please, send an email to __munged__#template-help.com');
window.location = 'index.html';
</script>
<?php
}?>
I can't seem to figure out the problem. Thanks in advance.
If you had access to the web host I would say try to find the apache2.log file on an Ubuntu server this is located in /var/log/ but I don't know where this would be for you. Anyways, if you find this you can look at the last records in the log to see if apache is throwing an error during the execution of this command that is otherwise hidden from the user. It sounds like there is probably a problem with the mail server. Do you have one setup? are you using sendmail or some variant?
I recently had some related problems with sending mail, this might help you (but as I said before it is for linux)
php5 mail() function sendmail error
Check the smtp server settings in [mail function] section of php.ini file. The mail server probably received the message successfully otherwise it would return an error. I would try to use different mail server.
Its a simple method to do.
Remove Unused Spaces from subject.
Do Not use dynamic variable with subject like $field_name.
use php5 #mail function