Jump to content

What's Wrong with My Mailing Script?


Fluoresce

Recommended Posts

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 = "[email protected]";
$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.

Link to comment
https://forums.phpfreaks.com/topic/127193-whats-wrong-with-my-mailing-script/
Share on other sites

Thanks, PFMaBiSmAd!

 

Apparently, my mail syntax was wrong. I couldn't use both $message and $body, so I had to concatenate them for it to work.

 

Now, the mail function works. However, the e-mails arrive in my Yahoo! and gmail accounts as a mass of HTML code. And, in my AOL account, they arrive looking OK but the links don't work.

 

What am I doing wrong? I've added a DOCTYPE but it still doesn't work.

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 = "[email protected]";
$subject2 = "Confirmation!";
$autoreply = "Confirmation that 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>";
}
}

?>

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?

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!  :)

 

Now, Where's that "Solved" button . . . ?

The solved button is gone for now. =P  Anyway, were the backslashes stored in the database or just passed in on the e-mail script?

 

The backslashes were added by me, after I'd select the coupon from the database, so as to be able to echo it in a table. Then, I would send it off in an e-mail, complete with the backslashes - which, of course, meant that none of the links would work!  :-[

 

I dwelt on the problem all day, completely confused, and no-one offered any help (I think the length of my first post was a bit scary!). So I called my superpowers into use, identified the problem, and solved it by myself.  8)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.