Forum Moderators: coopster

Message Too Old, No Replies

Why Doesn't My Mailing Script Work?

         

Tehuti

5:04 am on Oct 6, 2008 (gmt 0)

10+ Year Member Top Contributors Of The Month



I present coupons on my web site. Each one has an "E-mail to a Friend" button. When clicked, the visitor is sent to a page which:

1) selects the coupon
2) stores it in a variable, boxed in an HTML table
3) echos the variable
4) asks for To and From e-mail addresses and an optional message

Here's that page's code:

##########

<?php
$id = $_GET['id'];

$conn = mysql_connect('localhost', 'root') or trigger_error("SQL", E_USER_ERROR);
mysql_select_db('ctyi', $conn) or trigger_error("SQL", E_USER_ERROR);

$select = "SELECT blah, blah, blah . . .";
$query = mysql_query($select);

$str = "";
while($row = mysql_fetch_assoc($query)) {
$str .= "<html>";
$str .= "<body>";
$str .= "<table>";
$str .= "<tr>";
$str .= "<td>";
$str .= "Offer: <a href=\"" . $row['bmurl'] . "\">" . $row['anch'] . "</a><br />";
$str .= "Store: " . $row['adv'];
$str .= "<br /> Expires: ";
$str .= (empty($row['formated_date']) ? $row['expun'] : $row['formated_date']);
$str .= "<br /> Instructions: ";
$str .= (empty($row['code']) ? $row['instr'] : $row['code']);
$str .= "</td>";
$str .= "</tr>";
$str .= "</table>";
$str .= "</body>";
$str .= "</html>";
}

echo "$str";

echo "<form action=\"email-processor.php\" method=\"post\">";
echo "<table>";
echo "<tr>";
echo "<td>";
echo "<input type=\"hidden\" name=\"id\" value=\"" . htmlentities($str,ENT_QUOTES) . "\" />";
echo "Additional words (optional):<br />
<textarea name=\"message\" rows=\"5\" cols=\"35\"></textarea>";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>* Send to:<br />
<textarea name=\"to\" rows=\"1\" cols=\"35\"></textarea></td>";
echo "</tr>";
echo "<tr>";
echo "<td>* From:<br />
<input name=\"from\" size=\"25\" /></td>";
echo "</tr>";
echo "<tr>";
echo "<td><input type=\"submit\" name=\"send\" value=\"Send\" /></td>";
echo "</tr>";
echo "</table>";
echo "</form>";

mysql_close($conn);
?>

##########

When the visitor clicks "Submit", they are sent to the following page, which is supposed to process the e-mail and send me a confirmation message:

##########

<?php

$to = $_POST['to'];
$from = $_POST['from'];
$message = $_POST['message'];
$body = html_entity_decode($_POST['id'],ENT_QUOTES);
$headers = "From: $from";
$subject = "This Might Be of Use to You . . . !";

$me = "contact@mydomain.com";
$subject2 = "Confirmation";
$autoreply = "Confirmation that someone's sent a coupon to a friend.";
$headers2 = "From: $from";

if ($to == '') {

echo "<p>You have not entered the recipient's e-mail.</p>";

} elseif ($from == '') {

echo "<p>You have not entered your e-mail address.</p>";

} else {

$send = mail($to, $subject, $body, $message, $headers);
$send2 = mail($me, $subject2, $autoreply, $headers2);

if($send) {

echo "<p>Thank you!</p>";

} else {

echo "<p>Sorry! An error occurred.</p>";
}
}

?>

##########

Basically, it doesn't work. All I keep seeing is, "Sorry! An error occured."

1) What have I done wrong?
2) How can I join $message and $body?
3) Will having <html> and </html> tags echoed on my page (in the variable) cause a problem?

Any help will be so appreciated.

dreamcatcher

7:23 am on Oct 6, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



PHP`s mail function has 4 parameters, but in your example, you have 5.

mail($to, $subject, $body, $message, $headers);

Should be:
mail($to, $subject, $body, $headers);

or:
mail($to, $subject, $message, $headers);

I`m guessing thats why its failing.

dc

Tehuti

8:53 pm on Oct 6, 2008 (gmt 0)

10+ Year Member Top Contributors Of The Month



Thanks, Dreamcatcher!

I have now concatenated $body and $message ($message = $body . $message;) and the mail function works!

However, the e-mail arrives as a mass of HTML code in my Yahoo! and gmail accounts, and although it arrives looking OK in my AOL account, the links don't work when you click them.

I have added a DOCTYPE, but still no joy.

Anyone know what I'm doing wrong?

Tehuti

10:04 pm on Oct 6, 2008 (gmt 0)

10+ Year Member Top Contributors Of The Month



OK. I've nearly fixed it. The only problem remaining is that the links in my e-mails don't work. They can't be clicked. Anyone know why?

Here's the code:

##########

<?php

$to = $_POST['to'];
$from = $_POST['from'];
$message = $_POST['message'];
$body = html_entity_decode($_POST['id'],ENT_QUOTES);
$headers = "From: $from\r\n";
$headers .= "Content-type: text/html\r\n";
$subject = "This Might Be of Use to You . . . !";

$body = $message . "<br /><br />" . $body;

$message = "";
$message .= "<html>";
$message .= "<body>";
$message .= $body;
$message .= "</body>";
$message .= "</html>";

$me = "contact@mysite.com";
$subject2 = "Confirmation!";
$autoreply = "Someone's sent a coupon to a friend.";
$headers2 = "From: $from\r\n";

if ($to == '') {

echo "<p>You have not entered the recipient's e-mail.</p>";

} elseif ($from == '') {

echo "<p>You have not entered your e-mail.</p>";

} else {

$send = mail($to, $subject, $message, $headers);
$send2 = mail($me, $subject2, $autoreply, $headers2);

if($send) {

echo "<p>Thank you!</p>";

} else {

echo "<p>Sorry! An error occurred.</p>";
}
}

?>

Tehuti

12:30 am on Oct 7, 2008 (gmt 0)

10+ Year Member Top Contributors Of The Month



I think I know why the links in my e-mails don't work - although I'm not certain . . .

When I select the coupon and store it in the variable (see first post), the link is stored with the backslashes:

<a href=\"\">anchor</a>

I then send the coupon off in the e-mail, still with the backslashes.

How can I do it without the backslashes? Anyone know?

Tehuti

12:46 am on Oct 7, 2008 (gmt 0)

10+ Year Member Top Contributors Of The Month



OK, guys, I've figured it out . . .

Apparently, there's a specific function for this: stripslashes(). All I had to do was strip the backslashes from my variable before sending it off in an e-mail.

Cool! I love this PHP stuff! It's exciting!

dreamcatcher

2:32 pm on Oct 7, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Glad you sorted it out.

dc