What's Wrong with My Mailing Script?

Discussion in 'PHP' started by Masterful, Oct 5, 2008.

  1. #1
    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);
    ?>
    PHP:
    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>";
    }
    }
    
    ?>
    PHP:
    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 rewarded with reputation points.
     
    Masterful, Oct 5, 2008 IP
  2. Dman91

    Dman91 Peon

    Messages:
    46
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    1)mail syntax is wrong...
    2)Join it this way
    $message = $message . $body;
    $send = mail($to, $subject, $message, $headers);
    Code (markup):
    3)No
     
    Dman91, Oct 5, 2008 IP
    Masterful likes this.
  3. Masterful

    Masterful Well-Known Member

    Messages:
    1,653
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    140
    #3
    Thanks, Dman91! I've added points to your reputation. :)

    I have now concatenated $body and $message like you said ($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?
     
    Masterful, Oct 6, 2008 IP
  4. Masterful

    Masterful Well-Known Member

    Messages:
    1,653
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    140
    #4
    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>";
    }
    }
    
    ?>
    PHP:
     
    Masterful, Oct 6, 2008 IP
  5. Masterful

    Masterful Well-Known Member

    Messages:
    1,653
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    140
    #5
    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?
     
    Masterful, Oct 6, 2008 IP
  6. javaongsan

    javaongsan Well-Known Member

    Messages:
    1,054
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #6
    $str .= "Offer: <a href=\"" . $row['bmurl'] . "\">" . $row['anch'] . "</a><br />";
    Code (markup):
    a easy way is to change the quotes

    $str .= 'Offer: <a href="' . $row['bmurl'] . '">' . $row['anch'] . '</a><br />';
    Code (markup):
     
    javaongsan, Oct 6, 2008 IP
    Masterful likes this.
  7. Masterful

    Masterful Well-Known Member

    Messages:
    1,653
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    140
    #7
    Thanks, Java. However, I've solved it! (I still added points to your account.)

    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!
     
    Masterful, Oct 6, 2008 IP
  8. javaongsan

    javaongsan Well-Known Member

    Messages:
    1,054
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #8
    well done!
     
    javaongsan, Oct 6, 2008 IP