Copy link to clipboard
Copied
i have made an online form that selects users email addresses from a PHP mysql DB and then emails them information that is inputted into a form. the form is updating the DB but the email is not being sent. can anyone help ?. the code is below
<?php require_once('../Connections/prop.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form2")) {
$updateSQL = sprintf("UPDATE plus_signup SET GuName=%s, GuPhoneEmail=%s, GuEmailerSubject=%s, GuEmailerContent=%s WHERE userid=%s",
GetSQLValueString($_POST['GuName'], "text"),
GetSQLValueString($_POST['GuPhoneEmail'], "text"),
GetSQLValueString($_POST['GuEmailerSubject'], "text"),
GetSQLValueString($_POST['GuEmailerContent'], "text"),
GetSQLValueString($_POST['userid'], "text"));
mysql_select_db($database_hostprop, $hostprop);
$Result1 = mysql_query($updateSQL, $hostprop) or die(mysql_error());
// Email Guarantor
$to = $_POST['GuPhoneEmail'];
$subject = "EMail From Host Student Property";
$message = "
<html>
<head>
<title>New Email</title>
</head>
<body>
<img src=\"http://www.student.combeta/images/hostlogo.gif\" alt=\"www.thestudent.com\" />
<h2>An Email From Students</h2>
<br /><br />
<table>
<tr>
<td colspan=\"2\" style=\"text-align:center;\"><strong>Email Below</strong></td>
</tr
<tr>
<td>Subject</td>
<td>".GetSQLValueString($_POST['GuEmailerSubject'], "text")."</td>
</tr>
<tr>
<td>Content</td>
<td>".GetSQLValueString($_POST['GuEmailerContent'], "text")."</td>
</tr>
</table>
</body>
</html>
";
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: Student.com <contact@student.com>' . "\r\n";
$send = mail($to,$subject,$message,$headers);
$updateGoTo = "GuarantorEmailSent.php";
if (isset($_SERVER['QUERY_STRING'])) {
$updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
$updateGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $updateGoTo));
}
mysql_select_db($database_hostprop, $hostprop);
$query_Recordset1 = "SELECT userid, GuName, GuPhoneEmail FROM plus_signup";
$Recordset1 = mysql_query($query_Recordset1, $hostprop) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>
<script type="text/javascript">
function loadFields(Value) {
var Guarantor = Value.split("|");
var GuName = Guarantor[0];
var GuPhoneEmail = Guarantor[1] ;
document.getElementById('GuName').value=GuName;
document.getElementById('GuPhoneEmail').value=GuPhoneEmail;
}
</script>
<form action="<?php echo $editFormAction; ?>" method="post" name="form2" id="form2">
<table align="center">
<tr valign="baseline">
<td nowrap="nowrap" align="right"> </td>
<td><select name="Name" id="Name" onchange="loadFields(this.value)">
<option value="Select Guarantor">Select Guarantor</option>
<?php
do {
?>
<option value="<?php echo $row_Recordset1['GuName'] . '|' . $row_Recordset1['GuPhoneEmail'];?>"><?php echo $row_Recordset1['GuName'] . " , " . $row_Recordset1['GuPhoneEmail']; ?></option>
<?php
} while ($row_Recordset1 = mysql_fetch_assoc($Recordset1));
$rows = mysql_num_rows($Recordset1);
if($rows > 0) {
mysql_data_seek($Recordset1, 0);
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
}
?>
</select></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Userid:</td>
<td><?php echo $row_Recordset1['userid']; ?></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">GuName:</td>
<td><input type="text" name="GuName" id="GuName" readonly="readonly" value="<?php echo htmlentities($row_Recordset1['GuName'], ENT_COMPAT, 'utf-8'); ?>" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">GuPhoneEmail:</td>
<td><input type="text" name="GuPhoneEmail" id="GuPhoneEmail" readonly="readonly" value="<?php echo htmlentities($row_Recordset1['GuPhoneEmail'], ENT_COMPAT, 'utf-8'); ?>" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">GuEmailerSubject:</td>
<td><input type="text" name="GuEmailerSubject" value="" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">GuEmailerContent:</td>
<td><input type="text" name="GuEmailerContent" value="" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right"> </td>
<td><input type="submit" value="Update record" /></td>
</tr>
</table>
<input type="hidden" name="MM_update" value="form2" />
<input type="hidden" name="userid" value="<?php echo $row_Recordset1['userid']; ?>" />
</form>
thanks is advance
Copy link to clipboard
Copied
Jonathan, I'm assuming this is a continuation of your previous post. If one address is selected your script should work. If more than one is selected then it will fail because $_POST['GuPhoneEmail'] would be an array that would need to be combined into a single string in order to send the email successfully. Can you confirm this is what is happening or is no email being sent even to a single address?
Copy link to clipboard
Copied
yes the single email address is working, i know need to make it mail all email addresses in the DB. but this i have no clue how to do so if you could help it would be very much appreciated
regards
Copy link to clipboard
Copied
First issue you have is the form. 32 characters for an email address is cutting it close for one. If you plan on allowing multiple addresses this should be a textarea where you allow one address per line. Then you would trim the value ( http://php.net/manual/en/function.trim.php ) to remove excess whitespace, following by using explode on the unix return character "\n" ( http://php.net/manual/en/function.explode.php ). Finally you process the exploded array with a foreach loop ( http://php.net/manual/en/control-structures.foreach.php ) to put all the values of the array into a single string, or you could use the implode function to put them into a comma separted list ( http://php.net/manual/en/function.implode.php ).
Unfortunately as I mentioned in the previous post, this is not done through DW and something that you need to program soley in code view. I would highly recommend removing the DW code to put in your own code as there are a lot of custom actions taking place and you need to have a full understanding of every aspect of the code that is in place in order to build a system of this magnititude. And as previously discussed, if you choose to mail the users in bulk, the 150 contact database if I remember correctly, you will need to use the sleep function ( http://php.net/manual/en/function.sleep.php ) in between the mailings because otherwise your host may limit the number of simultaneous emails sent out from a single PHP mail execution.
Copy link to clipboard
Copied
Hey there, this looks a bit heavy for me. maybe i will get a programmer in to help me. thanks for your advise