View Full Version : PHP and eMails
Pauly
December 13th, 2004, 22:42
This line of code produces an error that says unexpected @
$to = name@email.com;
Now I tried putting 'name@email.com'; and name'@'email.com; but neither worked. Can you explain why the @ cannot be used like this and what it is used for generally in PHP please?
Alex
December 14th, 2004, 00:18
did you try double quotes?
$to = "name@email.com";
that works for me.. what do you do with the variable?
Pauly
December 14th, 2004, 00:32
The variable is used to send the form contents to what it's defined as. No I didn't use double quotes because I told single is better :)
sonicgroup
December 14th, 2004, 01:29
Well, it depends on the situation. In general, strings using single quotes are faster because PHP does not interpret anything about them. With strings in double quotes, PHP will go through the string looking for variables to replace or modify, and escape sequences (\n, \t, etc.). So, in this respect, single quoted strings are faster.
However, there's a time and a place for both. In this case, you may want to use double.
Edit: Oh, and about what the @ symbol is used for in PHP: It's used to suppress warnings and errors when calling functions.
Pauly
December 14th, 2004, 01:36
Thanks both of you, I'll try it out.
Pauly
December 22nd, 2004, 17:27
I tried the double quotes, but didn't work either, it either goes blank or I get this error;
Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING in /blankedout/blankedout/blankedout/qcontact.php on line 3
Error with $to = email"@"name.com; - Blank with $to = "email@name.com";
ethicaldesign
December 22nd, 2004, 18:28
I think you might be looking in the wrong place for why it's appearing blank.
If you write a test script:
<?php
$to='email@name.com';
echo $to;
?>
you should find that it defines $to OK and echos it as expected (haven't tried this yet but that's what I'd expect).
I don't think you need to use double quotes around that and the @ character within single quotes will be handled as part of the regular string.
using:
$to=email"@"name.com; will cause an error because you're not defining it as a whole string. When php looks at that statement it'll think that you've defined a constant called 'email' and that you're trying to build a string using it incorrectly.
If you can post the whole script that would be better because I've got a feeling your blank output isn't going to be anything to do with this variable definition (could be wrong but that's what it looks like).
Most importantly though is to re-read those string handling links that I posted earlier because you'll need to get your head around those properly and the various ways in which strings can be composed and used otherwise it's going to hold you back a lot.
Pauly
December 22nd, 2004, 19:56
[syntax:0da5667b3e="php"]
<?php
if ($action == "submit") {
$to = email@email.com;
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["content"];
if (($name == "") OR ($email == "") OR ($message == "")) {
echo '<meta http-equiv="refresh" content="0;url=http://www.takinghold.co.uk/redirects/blank_field.php" />';
email_form();
} elseif (ereg("([[:alnum:]\.\-]+)(\@[[:alnum:]\.\-]+\.+)", $email)) {
$recipient = $to;
$subject = 'TakingHold Query';
$additional_headers = ("From: $email\n");
$body = ("Name: $name\nEmail: $email\n\nMessage:\n\n" . $message);
if (mail($recipient, $subject, $body, $additional_headers)) {
echo '<meta http-equiv="refresh" content="0;url=http://www.takinghold.co.uk/redirects/sent_email.php" />';
} else {
echo '<meta http-equiv="refresh" content="0;url=http://www.takinghold.co.uk/redirects/script_error.php" />';
$send = "false";
email_form();
}
} else {
echo '<meta http-equiv="refresh" content="0;url=http://www.takinghold.co.uk/redirects/inval_email.php" />';
$send = "false";
email_form();
}
}
function emailform(){
echo '<form action=\"$PHP_SELF\" method="get">
<table width="220" border="0" cellspacing="0" cellpadding="0" class="un">
<tr>
<td>
<img src="http://www.takinghold.co.uk/images/qcontact.gif" alt="Contact TakingHold" />
<br />
<input name="name" type="text" value="Full Name" size="25" class="newsletterbox" />
<br />
<input name="email" type="text" value="e-Mail Address" size="25" class="newsletterbox" />
<br />
<textarea name="content" cols="25" rows="5" class="newsletterbox"></textarea>
<br />
<br />
<div align="right"><a href="http://www.takinghold.co.uk/images/qcontact.php?action=submit" class="readm">Send e-Mail</a></div>
</td>
</tr>
</table>
</form>';}
?>
[/syntax:0da5667b3e]
XHTML Transitional DTD
My brother gave me the script working, but once I edited it it messed up a little and he hasn't had time to look at it again - Hence posting here ;)
ethicaldesign
December 22nd, 2004, 20:56
I haven't tested it, but I think if you replace the line:
$to = email@email.com;
to :
$to='youraddress@yourdomain.com';
replacing with your real domain name and address that you want the message sent to that part should work.
I can see a problem with the output though:
echo '<form action=\"$PHP_SELF\" method="get">
won't work as you need to evaluate $PHP_SELF. This won't happen because it's inside of single quotes.
If you change that to:
echo '<form action="'.$PHP_SELF.'" method="get">
It should fix that.
As the double quotes are inside of single quotes they'll just be treat as simple characters and evaluation won't take place so you need to put $PHP_SELF outside of the single quotes and concatenate the string.
I haven't used $PHP_SELF though and I would tend do to it $_SERVER['PHP_SELF'] so not sure if that makes a difference (perhaps $PHP_SELF is defined somewhere else or it's just shorthand.
Putting output like that inside of a function isn't a good practice though, as functions are generally expected to return output to the calling code rather than output values directly.
A good candidate for a function would be your email validation and the emailer itself, with the email form being an include (if it's needed more than once) or just in-line.
I'll post the way that I would re-order that code in a moment below this post.
edit: something has just cropped up. I think the above changes should help for now (unless I'm missing something - it's been a long day). I'll try to get back a bit later and post some structure suggestions. If the fix above doesn't work if you could let me know what's happening I'll try to help with that first (re-ordering things as a suggestion is going to take more time).
ethicaldesign
December 22nd, 2004, 21:18
One other quick thing that I spotted:
http://www.takinghold.co.uk/redirects/blank_field.php - is currently a 404 page, so that redirect won't work.
http://www.takinghold.co.uk/redirects/script_error.php - is also missing - another 404
http://www.takinghold.co.uk/redirects/inval_email.php - is also missing.
You also shouldn't really be referencing URLs directly like that, but using relative paths as this will make your scripts more portable. If you try to install that script on another domain or in another folder structure then you're going to have to go through all of your code and replace all of the lines like this where they appear.
Far better to use something like:
redirects/inval_email.php - assuming that the script that's referencing the url is in the folder above the redirects folder in the tree.
I would also tend to use the header command rather than echoing headers directly like that.
header('location:YourRedirectedPageHere.php');
Also noticed:
} else {
echo '<meta http-equiv="refresh" content="0;url=http://www.takinghold.co.uk/redirects/script_error.php" />';
$send = "false";
email_form();
Because you're redirecting to a scripts_error.php form there, the email_form() function will never be called. As far as I'm aware all code after a redirect will be igonred and the browser will be redirected to a new page (could be wrong about that because I've never needed to do it if the code is structured properly to begin with).
Pauly
December 22nd, 2004, 23:07
ethical, I know the pages are missing, haven't had chance to do those yet ;)
I'll try out what you've said tomorrow and see how that goes, thanks!
vBulletin® v3.6.8, Copyright ©2000-2010, Jelsoft Enterprises Ltd.