Simple php contact form issue, not sending - php

Building a website for a client but the contact form is refusing to work, I set the client up with an email account 'contact#eunicedcolvin.co.uk' but it doesn't appear to finish sending it off to load the success message, here is the code below. Im fairly new to PHP but can't figure out what the issue is.
<div id="contact-form" style="background: none;">
<div id="contact-header"></div>
<div id="contact-middle">
<?php
if ($_POST["email"]<>'') {
$ToEmail = 'contact#eunicedcolvin.co.uk';
$EmailSubject = 'Website Contact';
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$MESSAGE_BODY = "Name: ".$_POST["name"]."<br>";
$MESSAGE_BODY .= "Email: ".$_POST["email"]."<br>";
$MESSAGE_BODY .= "Comment: ".n12br($_POST["comment"])."<br>";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
?>
<div id="contact-success">
<span style="font-size:16px; font-weight:bold; ">Thanks! Your message was sent</span><br>
I will get back to you as soon as I can
</div>
</div>
<?php
} else {
?>
<form method="POST">
<table width="360" border="0" cellspacing="8" cellpadding="0" style="margin-left: 11px;">
<tbody><tr>
<td class="contact-label">Your name:<br></td>
<td class="contact-text-border"><input name="name" type="text" id="name" size="32" class="contact-name"></td>
</tr>
<tr>
<td class="contact-label">Your Email address:</td>
<td class="contact-text-border"><input name="email" type="text" id="email" size="32" class="contact-name"></td>
</tr>
<tr>
<td class="contact-label">Comment:</td>
<td class="contact-text-border" style="padding: 2px 2px;"><textarea name="comment" cols="45" rows="6" id="comment" class="contact-comment"></textarea></td>
</tr>
<tr>
<td height="33" class="contact-label"> </td>
<td align="left" valign="top">
<input type="submit" name="Submit" class="submit" value="."></td>
</tr>
</tbody></table>
</form>
<?php
};
?>
</div>
<div id="contact-bottom"></div>
</div>
</div>
if you'd like to see a sample of it live, its here: http://www.eunicedcolvin.co.uk/contact.html
link to website
The thing is, originally it was set up to post to my personal email to test, then when i swapped it over it refused to work! been racking my brains over this for a while now :S

Your email sending server is most likely not setup correctly.
If it is in fact sending, then its probably get spam filtered.
Also you should not set the from address to the submitted address in the form, as your server will not be authorized to send from that domain.

To try your server is configured mail or not :
if(#mail($emailRecipient, $subject, $message, $headers))
{
echo "Mail Sent Successfully";
}else{
echo "Mail Not Sent";
}
use this code, upload it to simple x.php file with just this code (mail etc is inserted),
you know that your server is just ok or not :)...
or might be it's put to spam folder of the receiver

Use SMTP.
Don't spend time trying to make mail work.... In my opinion it's just a waste of time, and most emails sent with it, end up in SPAM folder.

Related

Emails sent by mail function is not being sent [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 4 years ago.
I am trying to make a PHP script which sends mail from a html contact form, the script doesn't throw any error, but it doesn't send mails.
The code is below.
mail.php
<?php
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
$email_from = 'stronka#obabie.com'; //<== update the email address
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n" .
"Here is the message:\n $message" .
$to = "bruno.kedzierski#wp.pl";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
mail($to, $email_subject, $email_body, $headers);
header('Location: index.html');
?>
My HTML file:
<div class="container" style="width: 200px; float: left; margin-left: 300px; margin-top: 25px">
<form action="mail.php" method="post">
<div class="form-group" style="margin-left: auto;margin-right: auto;">
<label>Imie i nazwisko</label>
<input type="text" class="form-control" placeholder="Imie i nazwisko " name="name">
</label>
</div>
<div class="form-group" style="margin-left: auto;margin-right: auto;">
<label>E-mail</label>
<input type="email" class="form-control" placeholder="E-mail" name="email" required>
</label>
</div>
<div class="form-group" style="margin-left: auto;margin-right: auto;">
<label>Numer telefonu</label>
<input class="form-control" placeholder="Twoj numer" type="tel">
</label>
</div>
<div class="form-group" style="margin-left: auto;margin-right: auto; width: 400px;">
<label>Wiadomosc</label>
<textarea class="form-control" placeholder="Twoja wiadomosc" style="height: 100px" name="message"> </textarea>
</label>
</div>
<div class="form-group" style="margin-left: auto;margin-right: auto;">
<label>Plec
<select style="form-control">
<option value="chlop">Chlop</option>
<option value="chlop">Baba</option>
<option value="inny">Inna</option>
</select>
</label>
</div>
<div class="radio">
<label style="display: block;">
<input type="radio" name="optradio"> kradne</label>
<label style="display: block;">
<input type="radio" name="optradio"> nie kradne</label>
</div>
<button type="submit" class="btn btn-default" value="submit">Wyslij</button>
</form>
So I added attribute action="mail.php" and method="post", so that the PHP start when I press submit. Can anybody tell why it doesn't work?
First of all add this attribute in form tag
enctype="multipart/form-data"
then in php code try to send mail without header like this one
mail($to,$email_subject,$email_body);
if still you get error then try to print error like this one after mail function
print_r(error_get_last())
Sending mail isn't rocket science. A simple example that Just Works -
<?php
$to_address = "someone#example.com";
$from_name = "From Name";
$from_address = "no.reply#example.com";
$reply_to_name="Reply to name";
$reply_to_address="reply-to#example.com";
$subject = "Hello!";
$headers = "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "From: $from_name <$from_address>\r\n";
$headers .= "Date: " . date("Ymd H:i:s") . "\r\n";
$headers .= "Reply-To: $reply_to_name <$reply_to_address>r\n";
$headers .= "X-Priority: 1\r\n";
$headers .= "X-MSMail-Priority: High\r\n";
$headers .= "X-Mailer: Some PHP Script\r\n";
$message_body = "Message goes here. Be polite and wrap it every 70 lines
or so, otherwise some mail clients will display very long annoying
lines of text. There are functions that can do this automagically
for you ... or write you own.";
mail($to_address, $subject, $message_body, $headers);
?>
HOWEVER... the good old days of anyone being able to send mail anywhere from any machine running a script out there on the interwebz are over. Between not wanting to be seen as a source of spam, and not wanting to receive spam, dealing with SPF records and DKIM and ... it becomes a lot to deal with.
In order to really send mail and know it is your code that isn't working vs. the email system you need to have properly configured PHP and properly configured whatever mail server PHP is working with.
I see mail questions come up often enough, with no answers that have no concern for server configuration. I think I may set up a VM that has working mail to itself, post it somewhere, and post a self-answered question on setting up things for dev work when needing to work with mail()...

PHP Form - not receiving email

I'm not understanding why Im not receiving email from the form after filling out the simple form. After clicking the submit button it redirected to the thank you page with no problem, but no email.
HTML
<form class="action" name="form1" method="POST" action="_sendmail2.php" onSubmit="return CheckAll(this);">
<label class="nick-2">Full Name:</label><br>
<input type="text" class="name" name="full_name">
<label class="nick">Email Address:</label><br>
<input type="text" class="email" name="email"><br>
<div class="radio-toolbar">
<input type="radio" id="radio1" name="agent_type" value="Buyer" checked>
<label for="radio1">Buyer</label>
<input type="radio" id="radio2" name="agent_type" value="Seller">
<label for="radio2">Seller</label>
<input type="radio" id="radio3" name="agent_type" value="Investor">
<label for="radio3">Investor</label>
</div><br>
<input type="submit" class="btn" value="SUBMIT" name="Submit">
</form>
PHP (
<?php
$to = "cluneborg#hotmail.com";
$subject = "New Agent Inquries";
$full_name = $_POST['full_name'];
$email = $_POST['email'];
$agent_type = $_POST['agent_type'];
if($_SERVER['REQUEST_METHOD']=="POST") {
$full_name=str_replace ( array("\n"), array("<br>"),trim($_REQUEST['full_name']));
$email=str_replace ( array("\n"), array("<br>"),trim($_REQUEST['email']));
$agent_type=str_replace ( array("\n"), array(" <br>"),trim($_REQUEST['agent_type']));
$contentmsg=stripslashes("<br><b><font style=color:#CC3300>$subject</font></b><br>
<table width=708 border=0 cellpadding=2 cellspacing=1 bgcolor=#CCCCCC>
<tr>
<td width=165 align=right valign=top bgcolor=#FFFFFF><B>Full Name: </b> </td>
<td width=565 align=left valign=top bgcolor=#FFFFFF> $full_name</td>
</tr>
<tr>
<td width=165 align=right valign=top bgcolor=#FFFFFF><B>Email Address: </b> </td>
<td width=565 align=left valign=top bgcolor=#FFFFFF> $email</td>
</tr>
<tr>
<td width=165 align=right valign=top bgcolor=#FFFFFF><B>Type of Agent:</b> </td>
<td width=565 align=left valign=top bgcolor=#FFFFFF> $agent_type</td>
</tr>
</table>
");
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$headers .= 'To: Eric <eluneborg#gmail.com>' . "\r\n";
$headers .= 'From: Texas Real Estate Agent Website' . "\r\n";
if(mail($to,$subject,$contentmsg,$headers)){
header("Location: http://www.magnixsolutions.com/clients/tas/thanks.html");
}
else
{
echo "Mail was not sent!";
}
}
?>
Sometimes it sends email to my hotmail and most of time it get this (checked on cpanel)
This message was created automatically by mail delivery software.
A message that you sent could not be delivered to one or more of its
recipients. This is a permanent error. The following address(es) failed:
cluneborg#hotmail.com
Domain magnixsolutions.com has exceeded the max defers and failures per hour (5/5 (55%)) allowed. Message discarded.
(Tested) - There were a few issues with your code.
The most important thing, is the # symbol in #mail - This will not execute, it needs to be removed.
Now, this line: (in PHP)
$_REQUEST['type_agent']
should be:
$_REQUEST['agent_type']
as per: (in HTML form)
<input type="radio" id="radio3" name="agent_type" value="Investor">
Then your headers were incorrect, where I added a few \r\n
One of your headers (in PHP)
$headers .= "From: ".$from."";
has been changed to:
$headers .= "From: $full_name <$email>\r\n";
Sidenote: It could be replaced with
$headers .= "From: $fromemail <$email>\r\n";
if you want the name to appear as "New Agent" in mail, instead of the person's name sending the Email.
Using this $fromemail="New Agent"; in conjunction with $from=$fromemail; and $headers .= "From: ".$from."";
would have resulted in mail going to SPAM, being it's not an actual Email address.
Plus, upon testing your original code, it did not come in as proper HTML, but the codes themselves showed up in the Email; that has been corrected.
If you want the Email and the name, you need to use two different variables.
I.e.:
$headers .= 'From: YourName <YourName#domain.com>' . "\r\n";
and in your case:
$headers .= "From: $full_name <$email>\r\n";
Rewrite: (PHP)
<?php ob_start();
// commented out - is not needed for the time being
// $fromemail="New Agent"; // change here if you want
$toemail="email#example.com"; // change here if you want
$sub="Agent Inquiries"; // change here if you want
$success_page_name="thanks.html";
////// do not change in following
if($_SERVER['REQUEST_METHOD']=="POST")
{
$full_name=str_replace ( array("\n"), array("<br>"),trim($_REQUEST['full_name']));
$email=str_replace ( array("\n"), array("<br>"),trim($_REQUEST['email']));
$type_agent=str_replace ( array("\n"), array("<br>"),trim($_REQUEST['agent_type']));
$contentmsg=stripslashes("<br><b><font style=color:#CC3300>$sub</font></b><br>
<table width=708 border=0 cellpadding=2 cellspacing=1 bgcolor=#CCCCCC>
<tr>
<td width=165 align=right valign=top bgcolor=#FFFFFF><B>Full Name: </b> </td>
<td width=565 align=left valign=top bgcolor=#FFFFFF>$full_name</td>
</tr>
<tr>
<td width=165 align=right valign=top bgcolor=#FFFFFF><B>Email Address: </b> </td>
<td width=565 align=left valign=top bgcolor=#FFFFFF>$email</td>
</tr>
<tr>
<td width=165 align=right valign=top bgcolor=#FFFFFF><B>Type of Agent:</b> </td>
<td width=565 align=left valign=top bgcolor=#FFFFFF>$type_agent</td>
</tr>
</table>
");
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$headers .= "From: $full_name <$email>\r\n";
#mail($toemail,$sub,$contentmsg,$headers);
header("Location:$success_page_name");
}
?>
Footnotes:
Including the # symbol in #mail suppresses errors and does not execute the function, so you will want to remove it..
In my testing, I removed onSubmit="return CheckAll(this); since your full code didn't include that function. Should it fail, then you may need to remove it also.
Remove the # sign from the #mail command and it might give you a helpful error. The # sign there is suppressing errors:
http://www.php.net/manual/en/language.operators.errorcontrol.php
You need to add EOL character \n in headers to separate. Don't know whether this is the solution, but it is at least one problem that needs attention.
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$from=$fromemail;
$headers .= "From: ".$from."\n";
In addition to the error suppression answers/comments, you can also make sure that mail() returns true indicating your server has accepted it and will attempt delivery.
$success = mail($toemail,$sub,$contentmsg,$headers);
var_dump( $success ); // should be true

Process order form with php to send email

I have the following code:
<table class="table table-striped" id="itemsTable">
<thead>
<tr>
<th></th>
<th>Item Code</th>
<th>Description</th>
<th>Qty</th>
<th>Price</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<tr class="item-row">
<td></td>
<td><input type="text" name="itemCode[]" value="" class="input-medium" id="itemCode"
tabindex="1"/>
</td>
<td><input type="text" name="itemDesc[]" value="" class="input-large" id="itemDesc"
readonly="readonly"/></td>
<td><input type="text" name="itemQty[]" value="" class="input-mini" id="itemQty" tabindex="2"/>
</td>
<td>
<div class="input-prepend input-append"><span class="add-on">€</span>
<input
name="itemPrice[]"
class=" input-small"
id="itemPrice"
type="text"></div>
</td>
<td>
<div class="input-prepend input-append"><span class="add-on">€</span><input
name="itemLineTotal[]" class=" input-small" id="itemLineTotal" type="text"
readonly="readonly"></div>
</td>
</tr>
</tbody>
</table>
What is the best way to process the inputs via php to send the order via email nicley formated into a table? This is an order form and I need to to simply be sent to an email once complete
Here is my processing code:
<?php
$to = $_REQUEST['xxx'] ;
$from = $_REQUEST['Email'] ;
$name = $_REQUEST['Name'] ;
$headers = "From: $from";
$subject = "Web Contact Data";
$fields = array();
$fields{"itemCode"} = "Code";
$fields{"itemDesc"} = "Description";
$fields{"itemPrice"} = "Price";
$body = "We have received the following information:\n\n";
foreach($fields as $a => $b){
$body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]);
}
$headers2 = "From: noreply#example.com";
$subject2 = "Thank you for contacting us";
$autoreply = "Thank you for contacting us. Somebody will get back to you as soon as possible, usualy within 48 hours. If you have any more questions, please consult our website at www.oursite.com";
$send = mail($to, $subject, $body);
if($send){
header( "Location:index.php" );
} else {
print "We encountered an error sending your mail, please try again";
}
?>
This code is not working please help
To process a form this way you need to have a form element somewhere in your markup to process.
<form method="POST" action="yourSecondScript.php">
your first markup here
<input type="submit">
<form>
Then to make the email nice with tables you need to set the email headers to html.
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: " . $_REQUEST['Name'] . ">\r\n";
mail($to, $subject, $body, $headers);
Where is
$to = $_REQUEST['xxx'] ;
coming from? My guess is that you can set it to fixed as you probably don't need a dynamic e-mail to address, so something like:
$to = 'myname#mydomain.com';
But as Michael mentioned in a reaction to your question, we cannot be sure until you tell us which part is not working / what errors you receive and so on..

Sending values of a form to an email

Hello i am fensing a problem with my form sending to email. I have created a form to send values to my emal, when in press Send button it tells me that message is sent but i can't see at my yahoo or gmail email, i am receiving nothing ...
here is my form with php code:
<?php
$ToEmail = 'mr_sergios#yahoo.com';
$EmailSubject = 'Site contact form';
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$MESSAGE_BODY = "Name: ".$_POST["name"]."";
$MESSAGE_BODY .= "Email: ".$_POST["email"]."";
$MESSAGE_BODY .= "Comment: ".nl2br($_POST["comment"])."";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
?>
<?php
if ($_POST["email"]<>'') {
$ToEmail = 'mr_sergios#yahoo.com';
$EmailSubject = 'Site contact form';
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$MESSAGE_BODY = "Name: ".$_POST["name"]."";
$MESSAGE_BODY .= "Email: ".$_POST["email"]."";
$MESSAGE_BODY .= "Comment: ".nl2br($_POST["comment"])."";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
?>
Your message was sent
<?php
} else {
?>
<form action="test.php" method="post">
<table width="400" border="0" cellspacing="2" cellpadding="0">
<tr>
<td width="29%" class="bodytext">Your name:</td>
<td width="71%"><input name="name" type="text" id="name" size="32"></td>
</tr>
<tr>
<td class="bodytext">Email address:</td>
<td><input name="email" type="text" id="email" size="32"></td>
</tr>
<tr>
<td class="bodytext">Comment:</td>
<td><textarea name="comment" cols="45" rows="6" id="comment"
class="bodytext">
</textarea></td>
</tr>
<tr>
<td class="bodytext"> </td>
<td align="left" valign="top"><input type="submit" name="Submit" value="Send"></td>
</tr>
</table>
</form>
<?php
};
?>
You probably don't have the correct SMTP settings set up in your php.ini. I'd recommend using something other than mail() anyway, as it's more likely to be reliable. Try something like this instead.
Either that, or it's just landed in your spam folder.
Are you sending it from Linux? If yes, please check /var/log/mail.err and see if the SMTP throws any errors.
For Windows it might be necessary to install a SMTP server, like Mercury SMTP
Have you considered using PHP Mailer Class?
The example below is a form that submits to itself on the same page just paste the below at the very top of your contact page. Download and include your class.
require_once('class.phpmailer.php');
$address = "you#youremail.com";
$name = htmlspecialchars($_POST['name']);
$email = htmlspecialchars($_POST['email']);
$subject = htmlspecialchars($_POST['subject']);
$comment = htmlspecialchars($_POST['comment']);
$mail = new PHPMailer(); // defaults to using php "mail()"
$body = $comment;
$mail->AddReplyTo($email,$name);
$mail->SetFrom($email,$name);
$mail->AddReplyTo($email,$name);
$mail->AddAddress($address, "Your Name");
$mail->Subject = $subject;
$mail->MsgHTML($body);
if(isset($_POST['submit']))
{
$mail->Send();
}
Consider using this library: http://code.google.com/a/apache-extras.org/p/phpmailer/
It's extremely easy to setup and use, also gives you details in case of an error.

Form with multiple email fields to multiple recipients

This is my html form. The user will input the email addresses he/she would like to send the html email to.
<form id="form1" name="form1" method="post" action="">
<table width="400">
<tr>
<td>Please enter your email address:</td>
<td<input type="text" name="email" id="email" /></td>
</tr>
<tr>
<td>Please enter the email addresses you would like to notify below:</td>
<td>
</td>
</tr>
<tr>
<td>Email:</td>
<td>
<input type="text" name="email1" id="email1" />
</td>
</tr>
<tr>
<td>Email:</td>
<td><input type="text" name="email2" id="email2" />
</td>
</tr>
<tr>
<td>Email:</td>
<td><input type="text" name="email3" id="email3" />
</td>
</tr>
<tr>
<td>Email:</td>
<td><input type="text" name="email4" id="email4" />
</td>
</tr>
</table>
</form>
This is somewhat the php code.
<?php
$ToEmail = '["email1"]["email2"]["email3"]["email4"]';
$EmailSubject = 'Check this out guys!';
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader .= "Reply-To: "noreply#domain.com"\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
mail(......) or die ("Failure");
?>
<script type="text/javascript">
alert("Success! You have sent the notification to the emails you have entered.");
<!--
window.location = "form.html"
//-->
</script>
How do I:
1. Modify the PHP code so that it will send to the emails inputed by the user?
2. The body message of the notification is a html email. How do I go about adding it to the PHP code?
Your help is highly appreciated. Thanks in advance!
Looks like you need to $_POST the email1, email2 etc. values to a variable then use that as your value for $to in the mail() function - just make sure you add a comma after each:
$to = $_POST['email1'] . ', ';
$to .= $_POST['email2'] . ', ';
$to .= $_POST['email3'];
etc. Leave off the comma for the last email and you should be ready to go.
Regarding the content of your email, you should be able to send html no problem - just store it in a variable for ease of use later, e.g:
$message = '
<html>
<head>
<title>This is the HTML Email</title>
</head>
<body>
<div id="container">
<p>Welcome to the html!</p>
<img src="../img/some_image.jpg" alt="some image"/>
</div>
</body>
</html>
';
then make sure you add the relevant HTML headers:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
Along with any other headers e.g.:
$headers .= 'From: HTML Email <you#example.com>' . "\r\n";
Then call mail() with your defined variables:
mail($to, $subject, $message, $headers);
Hope that helps.
p.s. its all available on the mail function definition: mail()
Look at the php manual for mail
Example:
$toemails = "user#example.com, anotheruser#example.com";

Categories