email body appears multiple times - php

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

Related

Tell a friend script not sending email php [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
I am trying to execute "Tell a friend" php script, but that is not sending any emails, not to admin, friends, sender. Other emails on same server working fine.
I don't understand why this is not working, as other emails pages like contact us, registration (which send confimation email) all working on same server, please help me..
Html Code:
<table>
<tr>
<td>
<span>Complete the details below to send this link to a friend:</span>
<?php
$refurl = $_SERVER['HTTP_REFERER'];?>
<span><? print $refurl;?></span>
<form name="tellafriend" action="send_group.php" method="post" onSubmit="return checkfields()">
<table>
<tr>
<td> Your name*:</td>
<td> <input name="name" size="30" maxlength="45"> </td>
</tr>
<tr>
<td>Your email*:</td>
<td><input name="email" size="30" maxlength="45"></td>
</tr>
<tr>
<td colspan="2"><p align="center">Enter your friend's email addresses:</p>
</td>
</tr>
<tr>
<td>Email 1*:</td>
<td><input name="fmail1" class="bordesolid1" size="30" maxlength="50"></td>
</tr>
<tr>
<td>Email 2*:</td>
<td><input name="fmail2" size="30" maxlength="50"></td>
</tr>
<tr>
<td>Email 3*:</td>
<td><input name="fmail3" size="30" maxlength="50"></td>
</tr>
<tr>
<td colspan="2"><p align="center"><span>This message will contain your name & email address.</span>
<br>
<input onClick="validate();" type="button" value="click once to send">
<input type=hidden name=refurl value="<? print $refurl;?>">
</td>
</tr>
</table>
</form>
</td>
</tr>
</table>
PHP Code:
<?php
if(count($_POST)) {
foreach(array('fmail1','fmail2','fmail3','email','name') as $key) $_POST[$key] = strip_tags($_POST[$key]);
if(!is_secure($_POST)) {
die("Peace People! Stop Spamming!");
}
$name = $_POST[name];
$email = $_POST[email];
$fmail1 = $_POST[fmail1];
$fmail2 = $_POST[fmail2];
$fmail3 = $_POST[fmail3];
$refurl = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
$to = "arvindsri123#yahoo.com";
$subject = "Recommendation form submission";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: '.$email."\r\n";
$headers .= 'Reply-To: '.$email."\r\n".
'X-Mailer: PHP/' . phpversion();
$message = '<html><body>';
$message.='<p style="margin-top:10px;">'.$name.' has used your recommendation form using an email address of '.$email.' </p>';
$message.='<p style="margin-top:10px;">The people the recommendation has been submitted to are: </p>';
$message.='<p style="margin-top:10px;">'.$fmail1.' </p>';
$message.='<p style="margin-top:10px;">'.$fmail2.' </p>';
$message.='<p style="margin-top:10px;">'.$fmail3.' </p>';
$message.='<p style="margin-top:10px;">The page recommended:</p>';
$message.='<p style="margin-top:10px;">'.$refurl.'</p>';
$message .= '</body></html>';
$sentmail = mail($to, $subject, $message, $headers);
// $thankyoupage = "thankyou.htm";
//echo $sentmail;
if($sentmail) {
$name = $_POST[name];
$email = $_POST[email];
$fmail1 = $_POST[fmail1];
$fmail2 = $_POST[fmail2];
$fmail3 = $_POST[fmail3];
$refurl = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
$tsubject = "A web page recommendation from $_POST[name]";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: '.$email."\r\n";
$headers .= 'Reply-To: '.$email."\r\n".
'X-Mailer: PHP/' . phpversion();
$message = '<html><body>';
$message.='<p style="margin-top:10px;">Hi, '.$name.' whose email address is $_POST[email] thought you may be interested in this web page. '.$email.' </p>';
$message.='<p style="margin-top:10px;">'.$refurl.'</p>';
$message .= '</body></html>';
$sentmail = mail($fmail1,$fmail2,$fmail3, $tsubject $message, $headers);
echo '<h4>You have sent emails...</h4>';
//header("Location: $thankyoupage");
exit;
}
function is_secure($ar) {
$reg = "/(Content-Type|Bcc|MIME-Version|Content-Transfer-Encoding)/i";
if(!is_array($ar)) {
return preg_match($reg,$ar);
}
$incoming = array_values_recursive($ar);
foreach($incoming as $k=>$v) if(preg_match($reg,$v)) return false;
return true;
}
function array_values_recursive($array) {
$arrayValues = array();
foreach ($array as $key=>$value) {
if (is_scalar($value) || is_resource($value)) {
$arrayValues[] = $value;
$arrayValues[] = $key;
}
elseif (is_array($value)) {
$arrayValues[] = $key;
$arrayValues = array_merge($arrayValues, array_values_recursive($value));
}
}
return $arrayValues;
}
?>
I am using Bluehost web hosting, and other emails properly working as I said Contact us, Registration confirmation emails.. etc.
Thanks!
If you are running on localhost then it won't send email.. to do so you need to host on server
Try to host on free hosting server.. it will work!

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

Form submission and mail send

I have made a form for registration by php .The form has submitter email address field.I want that after form submission , form content will send to both form submitter and admin via submitter email address and admin email address. Submitter email address will get from submitter email address field.Admin email address is fixed .
For Submitter
headers= "from : no-reply#eschool.com"
messagebody same as it is
For admin
headers=" from :$email"
messagebody same as it is
I tried to do this:
Html code:
<form action="action.php" method="post">
<table>
<tr>
<td>Name</td>
<td>:</td>
<td><input type="text" name="name" width="400" /></td>
</tr>
<tr>
<td>Address</td>
<td>:</td>
<td><input type="text" name="address" width="400" /></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td><input type="text" name="email" width="400" /></td>
</tr>
<tr>
<td>Password</td>
<td rowspan="2"> </td>
<td>
<p><input type="text" name="pass" width="400" /></p>
<p> </p>
</td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="insert" value="Insert" /></td>
</tr>
</table>
<input type="submit" value="submit" />
</form>
action.php
$name = $_POST["name"];
$address = $_POST["address"];
$email = $_POST["email"];
$password = $_POST['pass'];
$subject = "Thank you for your registration.";
$admin = "info#editor.com";
$to = $email . "," . $admin;
$email_message .= "Name: ". $name."\n";
$email_message .= "Address: ".$address."\n";
$email_message .= "Email: ".$email."\n";
$email_message .= "password: ".$password."\n";
$headers = "MIME-Version: 1.0" . "\n";
$headers .= "Content-type:text/plain;charset=UTF-8;" . "\n";
$headers .= "content-Transfer-encoding: 8bit" ."\n";
$headers .= "From: no-reply#eschool.com ". "\n";
mail($to, $subject, $email_message, $headers);
Thanks
Your code is perfect, what is your are facing problem, plz write your problem.
Make following change in action.php for your convenience :
<?php
$name = $_POST["name"];
$address = $_POST["address"];
$email = $_POST["email"];
$password = $_POST['pass'];
$subject = "Thank you for your registration.";
$admin = "info#editor.com";
//$to = $email $admin;
$email_message = '';
$email_message .= "Name: ". $name."\n";
$email_message .= "Address: ".$address."\n";
$email_message .= "Email: ".$email."\n";
$email_message .= "password: ".$password."\n";
$headers1 = 'MIME-Version: 1.0' . "\r\n";
$headers1 .= 'Content-type: text/plain; charset=UTF-8' . "\r\n";
$headers1 .= "From: no-reply#eschool.com ". "\n";
$headers2 = 'MIME-Version: 1.0' . "\r\n";
$headers2 .= 'Content-type: text/plain; charset=UTF-8' . "\r\n";
$headers2 .= "From: ". $email . "\n";
if(#mail( $email, $subject, $email_message , $headers1 )) {
#mail( $admin, $subject, $email_message , $headers2 )
echo "Mail Sent.";
} else {
echo "Mail Not Sent.";
}
?>
You can simply make two different versions of your headers and execute mail(...) twice:
<?php
$name = $_POST["name"];
$address = $_POST["address"];
$email = $_POST["email"];
$password = $_POST['pass'];
$subject = "Thank you for your registration.";
$admin = "info#editor.com";
$email_message = "Name: ".$name."\n";
$email_message .= "Address: ".$address."\n";
$email_message .= "Email: ".$email."\n";
$email_message .= "password: ".$password."\n";
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type:text/plain;charset=UTF-8;\n";
$headers .= "content-Transfer-encoding: 8bit\n";
$poster_headers = $headers . "From: no-reply#eschool.com\n";
$admin_headers = $headers . "From: ".$email."\n";
mail( $admin, $subject, $email_message, $admin_headers );
mail( $email, $subject, $email_message, $poster_headers );
?>

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.

SQL Form submit result

I want to display a message something like: "Congrats! you have been registered".
But the message appears on the top left corner of the page. I want to display it in the same page below the "Submit Button".
Here's my code:
if($result)
{
//send the email
$to = "xyz#abc.com";
$subject = "New order for Weaving Hope";
//headers and subject
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: ".$name." <".$email.">\r\n";
$body = "New contact<br />";
$body .= "Name: ".$name."<br />";
$body .= "Email: ".$email."<br />";
$body .= "Contact No.: ".$contact."<br />";
$body .= "Item Id: ".$itemid."<br />";
$body .= "Quantity: ".$itemquantity."<br />";
$body .= "Comment: ".$comment."<br />";
$body .= "IP: ".$ip."<br />";
mail($to, $subject, $body, $headers);
//ok message
echo "Congrats!";
}
This page should be easy to follow, it tells you exactly what to do.
http://net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/
If you need to do this without AJAX, then you need to process the form data in the same PHP page that sends the form to the browser, and set a flag to determine whether or not the data submission was successful.
For example:
<?php // Call this page register.php
$success = false;
if( $result)
{
// .. Your code from above
$success = true;
}
?>
<form action="register.php" method="post">
<!-- Output the form for submission here -->
<input type="submit" name="register" value="Register" />
<?php if( $success): ?>
<span>Congrats, you are registered!</span>
<?php endif; ?>
</form>

Categories