/    Sign up×
Community /Pin to ProfileBookmark

E-mail script 101

So recent playing around with my site I found out my PHP script doesn’t work when I could have sworn that it did at one point. Anyway, I went and made the utmost basic version one could make, and yet I get an error.

<?php
$to = ‘[email protected]‘;
$subject = ‘You have mail’;
$body = ‘Blah’;
mail($to, $subject, $body);
?>

I get a parse error in line 26. Line 26 being where $to is defined. I’m thinking something else is at work here, but I’m clueless as to what. Yes, my site allows for PHP. ?

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@NevermoreMay 22.2004 — Post the whole file please.
Copy linkTweet thisAlerts:
@beckjo1May 22.2004 — try this one:

<?

$recipient = '[email protected]';

// replace with your email

$subject = 'Your Subject';

// replace with subject of email to be sent

$success_url = 'http://www.yoursite.com/thankyou.html';

// replace with path to thank you on successful email send

/* name your 3 html form fields name, email, comments leave these in-tact, do not edit below this line*/

$body = "From: $namen";

$body .= "Email: $emailnn";

$body .= "Comments:n";

$body .= "$comments";

mail($recipient, $subject, $body, "From: $email");

header("Location: $success_url");

?>
Copy linkTweet thisAlerts:
@spufiauthorMay 23.2004 — <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

<head>

<title>Matt's Website</title>

<meta name="Author" content="Me" />

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<meta http-equiv="Content-Script-Type" content="text/javascript" />

<meta http-equiv="Content-Style-Type" content="text/css" />

<meta http-equiv="refresh" content="5; url=index.html">

<link rel="stylesheet" title="Default" media="screen" href="styles.css" type="text/css" />

</head>


<body>


<div class="menu">

<img src="images/msbBanner.gif" width="325px" height="50px" alt="Matthew Brown" /><br />

<a href="index.html">Home</a> &#183;

<a href="words/index.html">Words</a> &#183;

<a href="pics/index.html">Pics</a> &#183;

<a href="geek/index.html">Geek Stuff</a> &#183;

<a href="links.html">Links</a> &#183;

<a href="about.html">About</a>

</div>

<div class="mainBG">

<div class="main">

<!-- Begin edit here -->

<?php

$to = '[email protected]';

$subject = 'You have mail';

$body = 'Blah';

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

?>

<!-- End edit here -->

</div>

</div>

<div class="copyright">

&copy; 2002 <a href="contact.html">My name</a>

</div>

</body>

</html>
Copy linkTweet thisAlerts:
@fredmvMay 23.2004 — Quite frankly, your code works just fine for me. Maybe try simplifying even more so:&lt;?php
mail('[email protected]', 'subject', 'body');
?&gt;
Another idea might be to include this at the very top of your source code (even before the DOCTYPE). I don't think [font=courier]mail[/font] sends any HTTP headers upon executing, but it's worth a try.
Copy linkTweet thisAlerts:
@spufiauthorMay 23.2004 — Ok, my original code did send headers and the PHP code wasn't above the Doctype. Why it worked at one point is beyond me. With my recent playing around with it, I did see a "headers already sent" message, but it's been some time since I played around with PHP and my knowledge of it is minimal at best. It was still a major d'oh moment. Once I get some projects done, I'm diving back into it to learn it for good. *crosses fingers* :p My code I'm using now seemed to have worked, but I'm still waiting for the e-mail to go through. If it doesn't, I'll post that code too.
Copy linkTweet thisAlerts:
@beckjo1May 23.2004 — More than once I played around with something and ruined something that was working fine ?. I'll probably be doing just that here in a minute ?

I now keep a notebook by me, write changes and check my original working program after evey couple changes so I'm not hunting down my problems
Copy linkTweet thisAlerts:
@Daniel_TMay 23.2004 — I've noticed that when I use the mail() function, it doesn't work if I store the target email address in a variable. For example:[code=php]$email = "[email protected]";
mail("$email", ...[/code]

Wouldn't work, but[code=php]mail("[email protected]", ...[/code]
would work. It's very weird.

-Dan
Copy linkTweet thisAlerts:
@spufiauthorMay 24.2004 — Ok, I never got the e-mail I tried to send myself, but Tom pointed to a [URL=http://www.thesitewizard.com/archive/feedbackphp.shtml]toturial on a PHP e-mail script[/URL] on his thread in the site review forum, and so far it's worked. That's the good part, the bad is the formatting needs to be cleaned up.

Here's the form. (Yes, it needs to be clean up, it's old code)

<form method="post" action="confirm.php">


<div>Name:</div>

<div><input type="text" name="sender" /></div>

<div><sup class="req">*</sup>E-mail:</div>

<div><input type="text" name="email" /></div>

<div>Comments:</div>

<textarea cols="50" rows="5" name="msg"> </textarea>

<div>

<input type="submit" value="Submit" />

<input type="reset" value="Reset" />

</div>

</form>

Here's the confirm.php code I'm using.

<?php


$sender = $_REQUEST['sender'];

$email = $_
REQUEST['email'];

$msg = $_REQUEST['msg'];

$msg = $sender." wrote...n".$msg;

mail("[email protected]", "E-mail from you site", $msg, "From $email" );

header("Refresh: 3; URL=/index.php");

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

<head>

<title>Matt's Website</title>

<meta name="Author" content="Me" />

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<meta http-equiv="Content-Script-Type" content="text/javascript" />

<meta http-equiv="Content-Style-Type" content="text/css" />

<meta http-equiv="refresh" content="5; url=index.html">

<link rel="stylesheet" title="Default" media="screen" href="styles.css" type="text/css" />

</head>


<body>


<div class="menu">

<img src="images/msbBanner.gif" width="325px" height="50px" alt="Me" /><br />

<a href="index.html">Home</a> &#183;

<a href="words/index.html">Words</a> &#183;

<a href="pics/index.html">Pics</a> &#183;

<a href="geek/index.html">Geek Stuff</a> &#183;

<a href="links.html">Links</a> &#183;

<a href="about.html">About</a>

</div>

<div class="mainBG">

<div class="main">

<?php

echo "Your email has been sent to me and I will read it as soon as possible.";

?>

</div>

</div>

<div class="copyright">

&copy; 2002 <a href="contact.html">Me</a>

</div>

</body>

</html>

Now, here's the text I get.

">From (e-mail that's passed as the var email)

Matt wrote...

Blah.


."

I don't see how to get rid of the initial >, or the period that's way down below. Another problem is the "From" field that goes along with my subect and "To" fields isn't showing the same e-mail address that's shown in the body of the message. The "From" field says Nobody and has an address of [email][email protected][/email].
Copy linkTweet thisAlerts:
@MstrBobMay 24.2004 — I don't know if it will make a difference, but what about including a From field?

[code=php]
$to = '[email protected]';
$subject = 'You have mail';
$body = 'Blah';
$from = 'From: [email][email protected][/email]';
mail($to, $subject, $body, $from);
[/code]


When I was having troubles with my mail() function, I asked my web host, and they said I had to include a from flag, so that the header looked like

[code=php]
mail($to, $subject, $body, "-f: [email][email protected][/email]");
[/code]


Depends on your server's settings, I guess.
×

Success!

Help @spufi spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...