James30
James30

Reputation: 285

PHP- how to pass variable with url in it from form to email program

I've been trying to figure this out all day. I'm trying to create a simple back-end email interface that will allow user to enter a few fields and then mail off to several email recipients. I have figured out most of it with the exception of being able to create a link in the message. Please help. p.s. If there are suggestions for an entirely better way to do this, I'm open to them.Thank you, james.

my code:

$subj = "Try Our New Strawberry Bagels";
$chicken = $_POST['comments'];
$message = $chicken;



 // 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";

// Additional headers
$headers .= 'From: Birthday Reminder <[email protected]>' . "\r\n";
$headers .= 'Cc: [email protected]' . "\r\n";
$headers .= 'Bcc: [email protected]' . "\r\n";


 while ($row = mysqli_fetch_assoc($result))
 {
  $emails = $row['email'] . ",";
  mail($emails, $subj, $message, $headers);
 }



?>

<form action="testMail.php" method="post">
                            Name: <input type="text" name="fname" />
                             Age: <input type="text" name="age" />
                           Comment:<textarea name="comments" 
                           id="comments">      </textarea>
                            <input type="submit" />
                            </form>

Upvotes: 0

Views: 300

Answers (1)

Marc B
Marc B

Reputation: 360682

You're going to be spamming the heck out of the first whose name shows up in the database results. Most likely you'd want to move the mail() call OUTSIDE of your loop, you'll be sending to:

1st loop: [email protected],
2nd loop: [email protected], [email protected],
3rd loop: [email protected], [email protected], [email protected], 
etc...

You're also specifying in the mail headers that you're sending an HTML email, so just use html to specify the link.

$message = <<<EOL
<a href="http://example.com">Click here</a> for a strawberry bagel deal.
EOL;

Upvotes: 4

Related Questions