Mail() only sending first word from subject - php

When i send some email, it only sends the first word from the subject:
Example: I send an email with the name of "Send email", it only sends the word "Send", how can i prevent that?
Code:
The subject is selected in a select box
<td><a>
<?= ' '.$val['email'].' '; ?>
<input type="text" value="<?= $val['email']; ?>" name="to"/>
</a></td>
<td>
<select name="subject">
<?php
foreach($templatesarr as $val){
echo '<option value='.$val['name'].'>'.$val['name'].'</option>';
}
echo '</select></td>
<td><input type="submit" name="send" value="Send e-mail" /></td>
</td></tr>
';
?>
</form>
Check if the button was clicked
if(isset($_POST['send'])){
$sendername = 'Company';
$from = 'noreplay#compaty.com';
$to = safe($_POST['to']);
$subject = safe($_POST['subject']);
$message = 'teste';
$headers = 'MIME-Version: 1.0' . PHP_EOL;
$headers .= 'Content-type: text/html; charset=iso-8859-1' . PHP_EOL;
$headers .= "From: $sendername <$from>".PHP_EOL;
mail($to, $subject, $message, $headers);
}else{
$to = NULL;
}
Safe function
function safe($value) {
return mysql_real_escape_string($value);
}

The main problem is caused by not escaping variables and not properly encapsulating your attribute values; use this instead:
foreach($templatesarr as $val){
printf('<option value="%1$s">%1$s</option>',
htmlspecialchars($val['name'], ENT_QUOTES, 'UTF-8')
);
}
It now uses "" to delimit the value attribute of the option element.

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!

PHP email - want to send to different emails

Currently:
I have the PHP form below, where users select one of the checkboxes a notification comes as to which location they are looking to book.
Goal:
I would like it to now be that when someone clicks Canterbury an email is sent to email1#gmail.com, when to Broadstairs - email2#gmail.com, and when to deal - email3#gmail.com
I have been going in circles trying to make the changes but I get in a mess.
FYI I don't mind changing the checkboxes into a dropdown, but I would rather keep it this way.
Current PHP section:
<?php
ini_set('display_errors',0);
error_reporting(E_ALL);
$error = '';
if(isset($_POST['send']))
{
$name = $_REQUEST['Name'];
$telephone = $_REQUEST['Telephone'];
$email = $_REQUEST['Email'];
$message = $_REQUEST['Message'];
$headers = "From: $email";
$subject = "Web Contact Data";
$selectedProjects = 'None';
if(isset($_POST['projects']) && is_array($_POST['projects']) && count($_POST['projects']) > 0){
$selectedProjects = implode(', ', $_POST['projects']);
}
$fields = array();
$fields["Name"] = $name;
$fields["Telephone"] = $telephone;
$fields["Email"] = $email;
$fields["Message"] = $message;
$fields["Location"] = $selectedProjects;
$to = "email4#gmail.com" ; // change all the following to $_POST
$body = "You have recieved the following information:\n\n";
foreach($fields as $key => $value)
{
$body .= sprintf("%20s: %s\n",$key,$value);
} // end foreach($fields as $key => $value)
$subject2 = "Thank you for contacting us.";
$autoreply = "<html><body><p>Dear " . $name . ",</p><p>Message goes here.</p></body></html>";
$headers2 = 'MIME-Version: 1.0' . "\r\n";
$headers2 .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers2 .= 'From: email4#gmail.com' . "\r\n";
$send=false;
if($name == '')
{
$error = "You did not enter your name, please try again.";
selected_values();
}
$send=false;
if($telephone == '')
{
$error = "You did not enter your telephone number, please try again.";
selected_values();
}
elseif(!preg_match("/^[[:alnum:]][a-z0-9_.'+-]*#[a-z0-9-]+(\.[a-z0-9-]{2,})+$/", $email))
{
$error = "You did not enter a valid email address, please try again.";
selected_values();
}
else
{
$send = mail($to, $subject, $body, $headers);
$send2 = mail($email, $subject2, $autoreply, $headers2);
if(!isset($error) && !$send)
{
$error = "We have encountered an error sending your mail, please notify [email]email4#gmail.com[/email].";
}
else
{
unset($_REQUEST['Name']);
unset($_REQUEST['Email']);
unset($_REQUEST['Telephone']);
unset($_REQUEST['Message']);
}
} // end else
}// end of if(isset($_POST['send']))
?>
And the HTML form is this:
<form method="post" action="./lead.php">
<ul>
<li>
<?php
if(isset($error))
echo '<div class="register_error">'.$error.'</div>';
if(isset($send) && $send== true){
=echo '<div class="contact-send-green">Thank you for your message.</div>';
}
if(!isset($_POST['send']) || isset($error))
?>
</li>
<li>
<input type="checkbox" name="projects[]" value="Broadstairs">
<label for="type2">Broadstairs</label>
<br /><br />
<input type="checkbox" name="projects[]" value="Canterbury">
<label for="type2">Canterbury</label>
<br /><br />
<input type="checkbox" name="projects[]" value="Deal">
<label for="type2">Deal</label>
</li>
<li>
<textarea name="Message" rows="5" cols="29"><?php if(isset($_REQUEST['Message'])) echo $_REQUEST['Message']; ?></textarea>
</li>
<li>
<input type="submit" name="send" value="Send Message" id="send-email">
</li>
</ul>
For single selection a simple switch(){} would do the magic.
switch($_POST['projects']){
case "Broadstairs":
$to = "email#email.com";
break;
case "Canterbury":
$to = "email#email.com";
break;
case "Deal":
$to = "email#email.com";
break;
}
And then remove the [] from the input names as you won't need them to be a array. (As such:)
<input type="checkbox" name="projects" value="Broadstairs">
And then perhaps considering using some JS/jQuery to check that only 1 is selected at a time?
There are 3 issues with the script you have provided
You want to select only a single email address however, you are using the name projects[] as the checkbox names - this will create an array, however since HTML5; if you use a name multiple times even without the [] it will create an array.
Given the above, you should most definitely as you mentioned change to a <select> box, or alternatively <radio> buttons.
You should not use $_REQUEST unless absolutely needed, you are providing an easy oppurtunity for sql injection.
Here is an example of how I would do it.
HTML
<form method="post" action="./lead.php">
<ul>
<li>
<?php
if(isset($error)) echo '<div class="register_error">'.$error.'</div>';
if(isset($send) && $send== true) echo '<div class="contact-send-green">Thank you for your message.</div>';
?>
</li>
<li>
<label for="Broadstairs"><input type="radio" name="projects" id='Broadstairs' value="Broadstairs"> Broadstairs</label>
<label for="Canterbury"><input type="radio" name="projects" id='Canterbury' value="Canterbury">Canterbury</label>
<label for="Deal"><input type="radio" name="projects" id='Deal' value="Deal">Deal</label>
</li>
<li>
<textarea name="Message" rows="5" cols="29"><?=(strlen($_POST['Message'])>0) ? $_POST['Message'] : ''; ?></textarea>
</li>
<li>
<button type='submit'>Send Message</button>
</li>
</ul>
And your PHP
<?php
if (!empty($_POST)) {
// Dispatch message to selected office
$name = $_POST['Name'];
$telephone = $_POST['Telephone'];
$email = $_POST['Email'];
$message = $_POST['Message'];
$subject = "Web Contact Data";
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: My website <my-email#example.com>' . "\r\n";
if ($_POST['projects'] == 'Canterbury') $to = "example#gmail.com" ;
elseif ($_POST['projects'] == 'Broadstairs') $to = "example#gmail.com" ;
else $to = "example#gmail.com" ;
mail($to, $subject, $message, $headers);
// Dispatch "Thank You" email
$subject = "Your message has been received!";
ob_start();
?>
<html>
<body>
<p>Dear <?=$name?></p>
<p>Thank you for contacting us,</p>
<p>We have received your message and will respond to you as soon as possible.</p>
<p>This is an automatic response to your inquiry and it is not necessary to reply.</p>
<p>Thank you,</p>
</body>
</html>
<?
$message = ob_get_flush();
mail($email, $subject, $message, $headers)
}
?>
I would recommend Google Recaptcha too.
Hope this helps
You need to add if, elseif, and else condition to your code.
Replace $to = "email1#email.com" ; with following code
if($_POST['projects'] == 'Broadstairs')
{
$to = "email2#email.com";
}
else if($_POST['projects'] == 'Canterbury')
{
$to = "email3#email.com";
}
else if($_POST['projects'] == 'Deal')
{
$to = "email4#email.com";
}
else
{
$to = "email5#email.com";
}

jquery submit contact form

I'm trying to hide the form and show the success alert after the mail is mailed.
But everything I do, is ignored by the code, I put header before the mail function, and the mail still sends, it should redirect before the mail sends, tried to break the code before the mail function, and the break functions works.
at list the mail doesn't send.
contact.php:
<div id="innerWrapper">
<h1 class="pageheading"><?=translate('contact_us') ?></h1>
<form class="form-horizontal" action="/ajax/contact.php" method="POST" id="contactForm">
<?php $email = $db->get_row("SELECT email,full_name FROM users WHERE userID = 1");?>
<label><?=translate('your_name') ?>:</label>
<input type="text" name="name" <?php if($usersObject->ID()) {echo 'value = "'. $email->full_name. '"';} else {echo 'placeholder = "First / Last"';}?> class="required input-xlarge"/>
<br/><br/>
<label><?=translate('email') ?>:</label>
<input type="email" name="email" <?php if($usersObject->ID()) {echo 'value = "'. $email->email . '"';} else {echo 'placeholder = "email#example.com"';}?> class="required input-xlarge"/>
<br/><br/>
<label>Subject:</label>
<input type="text" name="<?=translate('subject') ?>" placeholder="Hi" class="required input-xlarge"/>
<br/><br/>
<label><?=translate('message') ?>:</label>
<textarea name="message_to_us" class="input-xlarge required" rows="5" placeholder=""></textarea>
<br/><br/>
<input class="btn btn-large btn-warning" value="<?=translate('contact_btn') ?>" type="submit" name="sbc"/>
</form>
</div>
ajax/contact.php :
if(isset($_POST['sbc'])) {
foreach($_POST as $K=>$V) $_POST[$K] = trim(strip_tags($V));
extract($_POST);
foreach($_POST as $k=>$v) {
if(trim($v) == "")
{
print '<div class="alert alert-error">All fields are required</div>';
exit;
}
}
$body = 'New contact form message on ' . $_SERVER['SERVER_NAME'];
$body .= '<br/>';
$body .= 'Subject : ' . $Subject;
$body .= '<br/>';
$body .= 'Name : '.$name .' / Email : ' . $email;
$body .= '<br/>';
$body .= $message_to_us;
$headers = "From: contact#" . str_replace("www.", "", $_SERVER['SERVER_NAME']) . "\r\n";
$headers .= "Reply-To: ". $email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=utf-8\r\n";
mail(PAYPAL_EMAIL, str_replace("www.", '', $_SERVER["SERVER_NAME"]), $body, $headers);
?>
<script type="text/javascript">
$("#sbc").click(function() {
$("#contactForm").hide('slow');
});
</script>
<?php
print '<div class="alert alert-success">'.translate('thankyou_contact').'</div>';
}else{
print 'Emtpy request';
}
edit:
$headers .= "Content-Type: text/html; charset=utf-8\r\n";
header('Location: join');
mail(PAYPAL_EMAIL, str_replace("www.", '', $_SERVER["SERVER_NAME"]), $body, $headers);
tried to redirect before the mail just to check, but it just ignores the header and still mail the mail.
edit2 :
<script type="text/javascript">
$(document).ready (function() {
$("#sbc").click(function() {
$("#contactForm").hide('slow');
});
});
</script>
not working.
add id= sbc to
<input class="btn btn-large btn-warning" value="<?=translate('contact_btn') ?>" type="submit" name="sbc" id="sbc"/>

html showing in php mail

I'm trying to send a php mail but it seems that I have a error in my foreach because the mail shows html..
This is my code:
<form method="post">
<fieldset>
<legend>Contact Form</legend>
<label for="fullname">Votre Nom :
<input id="fullname" name="fullname" type="text" value="nelson" />
</label>
<label for="emailaddress" class="margin">Votre e-mail:
<input id="email" name="email" type="text" value="" />
</label>
<label for="message">Message:<br />
<textarea id="message" name="message" cols="40" rows="8"></textarea>
</label>
<p>
<input id="submit-button" class="button gray stripe" type="submit" name="submit" value="Envoyer le message" />
</p>
</fieldset>
</form>
<?php
foreach ($_POST as $value) {
$value = strip_tags($value);
$value = htmlspecialchars($value);
}
$name = $_POST["fullname"];
$email = "email:" .$_POST["email"];
$message = "Nom: <br/>" .$name. "email:<br/> " .$email. "message: " .$_POST["message"];
$to="email#hotmail.com";
$suject="site internet";
if (isset($_POST['submit'])) {
mail($to, $suject, $message);
echo"mail had been sent";
}
?>
Can anyone help me please
You need to set the Content-type header in your email message:
$name = $_POST["fullname"];
$email = "email:" .$_POST["email"];
$message = "Nom: <br/>" .$name. "email:<br/> " .$email. "message: " .$_POST["message"];
$to="email#hotmail.com";
$suject="site internet";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
if (isset($_POST['submit'])) {
mail($to, $suject, $message, $headers);
echo"mail had been sent";
Your message body also needs to be contained in <html> tags.
Your foreach is kind of useless, just do that. More fast than a loops
$name = strip_tags(htmlspecialchars($_POST["fullname"]));
$email = "email:" .strip_tags(htmlspecialchars($_POST["email"]));
$message = "Nom: <br/>" .$name. "email:<br/> " .$email. "message: " .strip_tags(htmlspecialchars($_POST["message";));
To send email containing HTML you must set the header so that the email client knows that the email contains HTML. You also have to make the body of your email an HTML document.
$header = "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html; charset: utf8\r\n";
mail($to, $suject, $message, $header);
And then in the message itself:
<html>
<head></head>
<body>
Content here
</body>
</html>

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