Sending values of a form to an email - php

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.

Related

php not sending response of html form in email [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 8 years ago.
I uploaded this php file on server and i want that when a user fills the html form present in this php file the user response should send to the email address that i mentioned in this php file .... but its not sending the response to the email address ... please help ... thank you
<?php
if ($_POST["email"]<>'') {
$ToEmail = 'abc#gmail.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're sending the email to the varible you defined earlier, $ToEmail. Change the value of that varible to the value you have from your form.
Try:
if ($_POST["email"]<>'') {
$ToEmail = $_POST['email'];
$EmailSubject = 'Site contact form';
$mailheader = "From: abc#gmail.com\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"])."";
Try using $mailheader = "From: admin#yourdomain"; for example email from asd.com must have $mailheader = "From: noreply#asd.com"; somilarly email from stackoverflow.com must have $mailheader = "From: admin#stackoverflow.com"; for authentic email sending. In some cases invalid email header causes failure to send email.

Why wont messages go through my contact form to my email? [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 8 years ago.
I'm trying to have a contact form on my page. Nothing crazy, just a simple form. I've tried multiple contact forms from the web, and they all look fine, they all work fine when filling it out on my site and even when you hit submit it brings you to the PHP page fine and everything, but I never get the message in my email. I've checked my spam and everything and I'm just not getting the message.
Here's the form html:
<form method="post" action="sendit.php">
<label for="Name">Name:</label>
<input type="text" name="Name" id="Name" />
<label for="Phone">Phone:</label>
<input type="text" name="Phone:" id="Phone" />
<label for="Email">Email:</label>
<input type="text" name="Email" id="Email" />
<label for="Message">Message:</label>
<textarea name="Message" rows="20" cols="20" id="Message"></textarea>
<input type="submit" name="submit" value="Submit" class="submit-button" />
</form>
Here's the PHP:
<?php
$EmailFrom = "mjd8079#yahoo.com";
$EmailTo = "mjd8079#yahoo.com";
$Subject = "Contact";
$Name = Trim(stripslashes($_POST['Name']));
$Tel = Trim(stripslashes($_POST['Tel']));
$Email = Trim(stripslashes($_POST['Email']));
$Message = Trim(stripslashes($_POST['Message']));
// validation
$validationOK=true;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
exit;
}
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Tel: ";
$Body .= $Tel;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=sendit.php\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>
This is the piece of the code which will help you to send the email as well as, it would ensure approx 80% that the mail will deliver in Inbox and not in Spam
require_once "Mail.php";
require_once('Mail/mime.php');
$to = "mjd8079#yahoo.com";
$subject = "Contact";
$header = "from: Some Name <mjd8079#yahoo.com> \r"."<br >";
$header .= "Content-Type: text/html; charset=ISO-8859-1 \r"."<br >";
$header .= "Return-Path: <mjd8079#yahoo.com> \r"."<br >";
$header .= "X-Priority: 1 (Highest) \r"."<br >";
$header .= "X-MSMail-Priority: High \r"."<br >";
$header .= "Importance: High \r"."<br >";
$header .= "MIME-Version: 1.0 \r"."<br >";
$htmlbody = "<html>
<table width='430' border='1' cellpadding='5' cellspacing='5' class='bg1' style='border-collapse:collapse;'>
<tr>
<td width='185' bgcolor='#003366'><font color='#ffffff' face='Verdana'>Your Name:</font></td>
<td width='210' valign='middle' bgcolor='#FFFFCC'><font color='#000000' face='Verdana' style='font-weight:bold'>$name</font></td>
</tr>
<tr>
<td bgcolor='#003366'><font color='#ffffff' face='Verdana'>E mail</font></td>
<td bgcolor='#FFFFCC'><font color='#000000' face='Verdana' style='font-weight:bold'>$Email</font></td>
</tr>
<tr>
<td bgcolor='#003366'><font color='#ffffff' face='Verdana'>Telephone Number</font></td>
<td bgcolor='#FFFFCC'><font color='#000000' face='Verdana' style='font-weight:bold'>$Tel</font></td>
</tr>
<tr>
<td bgcolor='#003366'><font color='#ffffff' face='Verdana'>Message</font></td>
<td bgcolor='#FFFFCC'><font color='#000000' face='Verdana' style='font-weight:bold'>$Message</font></td>
</tr>
</table>
</html>";
$from = "Some Name <mjd8079#yahoo.com>";
$host = "smtp.yahoo.com";
$port = "587";
$username1 = "mjd8079#yahoo.com";
$password1 = "yourpassword";
$crlf = chr(10);
$mime = new Mail_mime(array('eol' => $crlf));
$mime->setHTMLBody($htmlbody);
$body = $mime->getMessageBody();
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$headers = $mime->headers($headers);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username1,
'password' => $password1)
);
$mail = $smtp->send($to, $headers, $body );
There may be no issues with your code you may need to confirm that the server running PHP has an smtp server setup on it and that PHP knows about it.
You will have to check in you php.ini file to see in a server has been setup, the setting can be seen hear http://php.net/manual/en/mail.configuration.php.
if you also have access to the commandline on the server you can test that the server has access to send emails by doing the command
/usr/sbin/sendmail -t -i mjd8079#yahoo.com
This is a test
ctrl-d
if you don't get a message then it is most like the server not the code
tho in my experience Yahoo can be a bit grumpy on sending to may email, and can start blocking email if there is to many in a short period of time, so try another email address

Contact form throws errors when sending an email.

I am developing a contact form for sending an email to user data,but its not working.
Code:
<?php
if ($_POST["email"]<>'') {
$ToEmail = 'youremail#site.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="mail.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
};
?>
It throws an errors like:
Undefined index: commen in line 10.
header missing in line 11.
Here are some mistakes on your code..
1.at the line no 2 change the code from (i don't know why you didn't mention about this error)
if ($_POST["email"]<>'') {
to
if (isset($_POST["email"]) && $_POST["email"]<>'') {
2.change the name of the textarea from "Comment" to "comment"
and finally follow the instruction from the previous answers of this post to solve the "header missing" problem.
something like
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
As everybody told your first mistake, I am not going to repeat it but another mistake is:
You should join the mail headers and mail body:
Your previous code:
$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"])."";
New Code:
$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"])."";
Look at the dots given before the equalto signs.
see you need to concatenate the mailHeader var same goes for body message
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
You have done the two mistakes.
In form you have given name="Comment" for please give name="comment" or $_POST["comment"] to $_POST["Comment"]
put the . after mail header.like
$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"])."";
you are getting this error 10th line because you are using $_POST["comment"]" but in the form you have written id="Comment". To remove the error take care of upar and lower case in the both places..
Thank you.

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

email body appears multiple times

I'm trying to send a small batch of emails in a loop using PHP mail(). The script to send the emails works fine. There is, however, a slight glitch. Whilst the recipients all receive only one email, the first person on the list receives the email body ($MESSAGE_BODY) once, the second person gets the body twice and the third person gets it 3 times (and on it goes). I cannot for the life of me work out why it's doing it.
The form from which the emails are sent is:
<p>Message Text:
<br />
<textarea name="thebody" id="thebody" cols="65" rows="12"><?php echo $row_email['emailtext'];?></textarea>
<script type="text/javascript">CKEDITOR.replace( 'thebody' );</script>
</p>
<table >
<tr>
<th>Site</th>
<th>Email Address</th>
<th colspan="2">Email Now?</th>
</tr>
<?php
$b = 0;
$q = 1;
while ($row_selfdo = mysql_fetch_assoc($selfdo)) { ?>
<tr>
<td><?php echo $row_seldo[‘sitename’];?></td>
<td><input type="text" name="emailto[]" style="font-size:9px;" size="20" value="<?php echo $row_selfdo['eaddress']; ?>"/></td>
<td valign="middle">Yes:<input type="radio" name="emailnow[<?php echo $b;?>]" value="Yes" <?php if (isset($mailed) && ($mailed=="Not Yet")) { echo ""; } else echo "disabled='disabled'"; ?> /></td>
<td>No:<input name="emailnow[<?php echo $b;?>]" type="radio" value="No" checked="checked" <?php if (isset($mailed) && ($mailed=="Not Yet")) { echo ""; } else echo "disabled='disabled'"; ?>? /></td>
</tr>
<?php $b++; $q++; } ?>
</table>
And here's the script to send the mail
$numb = count($_POST['emailto']);
$num = $numb -1;
$subject=$_POST['subject'];
$thisrecipient = $_POST['emailto'];
$sendtothemnow = $_POST['emailnow'];
for ($a=0;$a<=$num;$a++) {
$emailthemnow = $sendtothemnow[$a];
if ((isset($emailthemnow))&&(($emailthemnow)=="Yes")) {
$recipient = $thisrecipient[$a];
$ToEmail = $recipient;
$EmailSubject = $subject;
$mailheader = 'From: me#mydomain.com'."\r\n";
$mailheader .= 'Reply-To: me#mydomain.com'."\r\n";
$mailheader .= 'MIME-Version: 1.0'."\r\n";
$mailheader .= 'Content-type: text/html; charset=iso-8859-1'."\r\n";
$MESSAGE_BODY .= '<p>'.$_POST['thebody'].'</p>';
$MESSAGE_BODY .= '<p>Kind Regards</p>';
$MESSAGE_BODY .= '<p>The Environment Team</p>';
$MESSAGE_BODY .= 'email footer bits here ';
$MESSAGE_BODY .='<p style="color:#0C0;">Please consider the environment - do you really need to print this email?';
$MESSAGE_BODY=wordwrap($MESSAGE_BODY,70);
$mailsent= mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Not Sent");
if($mailsent){
//update a table to record date email was sent
}
}//end email send loop
Any suggestions??
Many thanks in advance
On your first line where you use the message body, set it instead of appending it:
$MESSAGE_BODY = '<p>'.$_POST['thebody'].'</p>';
(The dot has been removed)
Only change the variable name which have conflict.
if ((isset($emailthemnow))&&(($emailthemnow)=="Yes")) {
$recipient = $thisrecipient[$a];
$ToEmail = $recipient;
$EmailSubject = $subject;
$mailheader = 'From: me#mydomain.com'."\r\n";
$mailheader .= 'Reply-To: me#mydomain.com'."\r\n";
$mailheader .= 'MIME-Version: 1.0'."\r\n";
$mailheader .= 'Content-type: text/html; charset=iso-8859-1'."\r\n";
$MESSAGE_BODY .= '<p>'.$_POST['thebody'].'</p>';
$MESSAGE_BODY .= '<p>Kind Regards</p>';
$MESSAGE_BODY .= '<p>The Environment Team</p>';
$MESSAGE_BODY .= 'email footer bits here ';
$MESSAGE_BODY .='<p style="color:#0C0;">Please consider the environment - do you really need to print this email?';
$MESSAGE_BODY_FINAL=wordwrap($MESSAGE_BODY,70);
$mailsent= mail($ToEmail, $EmailSubject, $MESSAGE_BODY_FINAL, $mailheader) or die ("Not Sent");
if($mailsent){
//update a table to record date email was sent
}
}

Categories