Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

phpmailer css conflict 1

Status
Not open for further replies.

jlockley

Technical User
Nov 28, 2001
1,522
US
First issue:

I am having two issues trying to set up form processing with phpmailer. The same worked with php's mail function.

The separate html document included the styles in the header like this
<style>
.style1 {
font-family: "Trebuchet MS";
font-size: large;
font-weight: bold;
}
</style>
creating the error
Parse error: syntax error, unexpected '.' in /home/jll/public_html/phpmailerform.php on line 13

(the "." is actually on line 12).

the included styles do not cause a problem using php's mail function.
Question: Is it necessary to separate css when using php mailer?
(I will include the html form and the first part of the script below, but I'd rather not impose the errors of the rest before I can try to find and fix them myself)

Second issue:
Having removed the css and placed it in an attached sheet I continue to get the error. Assuming it was a cache error I closed out and reopened in a different browser with the same result. I cannot reboot the server. Suggestions?
-----------------------------------
html form:
<DOCTYPE HTML PUBLIC
"-//W3C//DDT HTML 4.01 Transitional//EN"
"
<html>
<head>

<meta http-equiv="Content-Language" content="en-us" >
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>resume submission form</title>
//captcha theme here.

<style type="text/css">
.style1 {
font-family: "Trebuchet MS";
font-size: large;
font-weight: bold;
}
.style2 {
font-family: "Trebuchet MS";
font-size: large;
font-weight: bold;
text-align: right;
}
[snip - more of same]
}
</style>


</head>
<body>

<div class="style7"><h3><p>To register please fill out the following fields and upload a resume.</P>
<p>The files marked with * are required. The rest will make it easier for us to pick your resume. To be placed on our newsletter and job alert list, click the button at the end</p></h3></div>
<form action="phpmailerform.php" method="POST">
<table style="width: 792px; height: 553px" cellspacing="5" class="style4">
<tr>
<td style="width: 1085px" class="style2" valign="top">Last Name</td>
<td style="width: 1184px">
<input name="last" type="text" size="50" ></td>
</tr>
<tr>
<td style="width: 1085px" class="style2" valign="top">First Name</td>
<td style="width: 1184px">
<input first="first" type="text" size="50" ></td>
</tr>
<tr>
[snip - more of same]
<p><input type="submit" value="send" /p></p>
//captcha here
?>

</form>
</body>
</html>
---------
PHP form handler in following comment
 
Form processor
<html>
<head>
<title>Registration Form Handler</title>
</head>
<body>
<?php
//require_once('recaptchalib.php');

require("/include/language/phpmailer.lang-en.php");
require("/include/class.phpmailer.php");
require("/include/phpmailer-fe.php);

$privatekey = "...";

//recaptcha here.
$subject = "Resume from $_Post[first] $_POST[last], $_POST[position]";
$recipient ="me@mysite.com";
$last = $_POST[last];
$first = $_POST[first];
$phone = $_POST[phone];
$cell = $_POST[cell];
//snip - more of same.
$comments = $_POST[comments];
$subject = "Resume submission for $first $last $position $location";
$message = "$last \n $first \n $position \n $phone \n $cell \n $email \n$city \n $state \n $zip \n $position \n $location \n $type \n $comments";
?>

//phpmailer code below
<?php
$Mail = new PHPMailer();
$Mail->IsSMTP(); // send via SMTP
$Mail->Host = 'localhost; //SMTP server

$Mail->From = $From;
$Mail->FromName = $first $last <$email>;
$Mail->AddAddress('me@mysite.com');
$Mail->AddReplyTo($first $last <$email>);

$Mail->WordWrap = 50; // set word wrap
$Mail->IsHTML($HTML);

$Mail->Subject = $Subject;
$Mail->Body = $Message;

if($Mail->Send())
{
echo "Message Sent";
}
else
{
echo "Message Not Sent<br/>";
echo "Mailer Error: " . $Mail->ErrorInfo;
}
}




?>
//end phpmailer specific script

<h2> <a href="Thankyou.html"><h2>Thank you for registering. </a> </h2>


</body>
</html>
 
First problem:
the error is a php error which indicates that you are trying to use that style block in a php context (i.e. as php code) rather than as part of a variable. somehow you have told your webserver to treat the file as php. perhaps you are trying to include() or require() that file (which will not work, of course). If this is the case then instead you want to use readfile instead of include/require()

Second Problem:
seems to be a reiteration of the first, so i won't repeat myself.

General Observations:
you are trying to send the message in HTML but are using line breaks instead of <br/> tags (or <p> or any other block level element). is this because the variables are themselves html encoded and have appropriate paragraph spacing in them?

you are setting the message body to $Message whereas your message variable is LOWER CASE. variables in php are case sensitive.

you are setting ISHTML to $HTML but are not defining $HTML anywhere.

css and email clients do not play nicely with each other. google for best practices. from memory the norm is to use ONLY inline styles at a very simple level. most email clients strip separate css (i believe)

your declaration for an SMTP host will fail. you must include both quotation marks to enquote a variable.


 
[quote}First problem:
the error is a php error which indicates that you are trying to use that style block in a php context (i.e. as php code) rather than as part of a variable. somehow you have told your webserver to treat the file as php. perhaps you are trying to include() or require() that file (which will not work, of course). If this is the case then instead you want to use readfile instead of include/require[/quote]
I will look that up, but all of the require() instances are quoted above.

Second Problem:
seems to be a reiteration of the first,
..did not express myself well. The second issue is that the error persists after the css was removed from the html document. some caching issue, I suspect.

General Observations:
you are trying to send the message in HTML but are using line breaks instead of <br/> tags (or <p> or any other block level element). is this because the variables are themselves html encoded and have appropriate paragraph spacing in them?
I was experimenting with that. It can and probably should be sent ascii, and I probably will. I just hadn't got that far in checking the script due to the "." error. I will look more closely at it. In fact, once I get this to work, I want to not only send myself a copy but to put the data in a MySql database for import into Paradox, so HTML and certainly SHTML are not necessary. I just wanted to play a little.

The capitalization: Many thanks. Will check on that.

css and email clients: The problem here was that the styles were included in the html file. The css is only for the form page. You have given me an idea, though, and that is that I was working on using captcha for security. The only php on the form page was the Captcha code, which didn't work, anyway. I will check in if that is the issue....

Another question if allowed: What I am not finding documented or not recognizing in what I have been reading is the issue of importing values into the object instance from the variables derived from the $_POST[] data. This is possible then?
$last = $_POST[last];
$first = $_POST[first];
$phone = $_POST[phone];
$cell = $_POST[cell];
<etc>
$message = "$last \n $first \n $position \n $phone \n $cell \n $email \n$city \n $state \n $zip \n $position \n $location \n $type \n $comments";

 
you can import array elements into singular variable scope. one method of doing so is this

Code:
$post= $_POST;
//cleanse the variables with trim
array_map('trim', $post);
extract($post);

the danger in doing the above is you have no idea what people may have posted to your server and thus may overwrite your own variables.

consider this
Code:
$userID = 10 ; //low permission user

//someone posts in a variable of userID =1 and 1 is the admin user
extract ($_POST);
//and suddenly the user perm level is escalated to admin

now that's a silly example as noone would code like that, but the point, hopefully is made. it's not just a security issue it's an issue of maintaining code control.

the way i break in variables is like this

Code:
$fields = array ('last', 'first', 'phone');

foreach ($fields as $field){
 if (empty($_POST[$field])){
   $$field = '';
 } else {
  $$field = trim($_POST[$field];
 }
}

or if all you are trying to do is to flatten the array into a message you could try this

Code:
$message = '';
foreach ($_POST as $key -> $val){
 $message . = "$key = $val \r\n";
}

or even simpler

Code:
$message = implode ("\r\n", $_POST);
 
Interesting.

The purpose of this, aside from learning to use PHP is to gather information from candidates and to register them for an email letter, which will be a future challenge.

I am taking this step by step.

I thought I would start out with a simple mail program, sending the variables with the mail function. it works nicely. But I want to be able to receive uploaded resumes..the challenge after next, the next being security.

From what I have looked at the best option for this is using phpmailer, so I am switching my simpler response (for me at any rate) over to PHPmailer (ergo the interest in OOP). so far without the upload.

Once I can get the data via email I want to move on and set up a database on line where much of this information can be stored pending download and import into an OBDC driven desktop database. The security concerns are interesting. I've been reading up on it, but hadn't seen it from that perspective.

 
phpmailer is the way to go.

oddly enough i built a resume receiver for Fish4 (big job board in the UK) at the beginning of this year. complete with e-commerce journey and everything. it was proof of concept only and they never used it in the end. in order to give you an idea of the complexity that you are dealing with, the whole application took an afternoon to write.

 
It will probably take a bit longer for me. Steep learning curve for some.

so if I your system to create message (thanks again..that resloves that)

$message = '';
foreach ($_POST as $key -> $val){
$message . = "$key = $val \r\n";
}

which yields the variable $message

Or do what seems simple to me and make $email = $_POST

Then am I correct in believing that I can use $email or $message directly in the script as in
[quote]
$mail->FromName = $sender
// derived either using array of simply $sender = "Resume //from $_Post[first] $_POST[last] $_POST[$title];"
$mail->Message = $message;[/quote]

 
Great. I have been breaking my head over that. It appeared from everything I have been looking up that the variable needed to be declared in the class, which makes absolutely no sense at all.
There is one variation in Ullman's PHP books (I like these) which doesn't use the POST method but just posts the variable as in $email $name. This is a library book and I realized a bit late that it is for PHP4, not five.
 
it's not a 4 vs 5 thing; well not entirely. it will be making the assumption that register_globals is turned on. this is a heinous sin.
 
well, it was PHP3 and PHP4. I thought globals were on by default. We will see what he has in the new one.

In the meantime, I am stuck at the question above.

working with phpmailer I have the input as $_Post['email'] from the form.
This simplified (probably unnecessary)
$email = $_POST['email'];

To send with PHHmailer have:
$mail->From = "name@yourdomain.com";
but I want the mail from (or respond to) to be the sender,
so I use
$mail->From = $email;

$mail->FromName = $candidate;

etc. It's obviously wrong.
This too:
$mail->From($email, $candidate);
(I figured it was, but thought I would try it
The question is how to pass the material from the form to the required mail fields.


 
Code:
$mail->From       = $email;

$mail->FromName   = $candidate;

this is fine. provided that the $email and $candidate variables contain the right information.

if you are not getting the right mail data then try using the superglobals instead of your assigned variable

$mail->From = $_POST['email'];

=> there may be an issue with scope.
 
It didn't work out, but maybe I was doing something else wrong. I have also tried adding a new function, probably incorrectly. So far little success.

I haven't done it with your implosion method yet, as I would prefer to have the various elements on separate lines, so I can set a variable to include line breaks, strings and _POST[values].

I will rewrite it to the first version and see what I get, post it here.

 
separate lines? just implode using "<br/>\n\n" as the separator.

 
Neat. I am trying to figure out what the error handler is griping about now, then I will post what I have. I had it working to a point, or at least not nit picking. Having recreated it, I am dealing with the small errata now.
 
Four beautiful words: Message has been sent.

japdie, I can't thank you enough. It's far from finished. Needs security issues addressed, captcha or other bot deterrent, fleshing out, etc, but I have the system. It now seems so obvious, and I am guessing that the issues I had were caused by idiot errata. I am prone to that.

I don't know if it's practice, but the two scripts follow. Maybe they will be useful to someone.

Form:
<DOCTYPE HTML PUBLIC
"-//W3C//DDT HTML 4.01 Transitional//EN"
"
<html>
<head>

<meta http-equiv="Content-Language" content="en-us" >
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Register and Submit</title>


<link rel="stylesheet" type="text/css" href="phpmailerform.css">

</head>
<body>

<div class="style7"><h3><p>To register with us please fill out the following fields and upload a resume.</P>
<p>The files marked with * are required. The rest will make it easier for us to pick your resume. To be placed on our newsletter and job alert list, click the button at the end</p></h3></div>
<form action="phpmailerform.php" method="POST">
<table style="width: 792px; height: 553px" cellspacing="5" class="style4">
<tr>
<td style="width: 1085px" class="style2" valign="top">Last Name</td>
<td style="width: 1184px">
<input name="last_name" type="text" size="50" ></td>
</tr>
<tr>
<td style="width: 1085px" class="style2" valign="top">First</td>
<td style="width: 1184px">
<input name="first" type="text" size="50" ></td>
</tr>
</tr>
<tr>
<td style="width: 1085px" class="style2" valign="top">EMail</td>
<td style="width: 1184px">
<input name="email" type="text" size="50" ></td>
</tr>

<tr>
<td style="width: 1085px" class="style2" valign="top">Cell Phone </td>
<td style="width: 1184px">
<input name="cell" type="text" size="50" /></td>
</tr>
<tr>
<td style="width: 1085px" class="style2" valign="top">Phone</td>
<td style="width: 1184px">
<input name="phone" type="text" size="50" /></td>
</tr>
<tr>
<td style="width: 1085px" class="style2" valign="top">City</td>
<td style="width: 1184px">
<input name="city" type="text" size="50" /></td>
</tr>
<tr>
<td style="width: 1085px" class="style2" valign="top">State</td>
<td style="width: 1184px">
<input name="state" type="text" size="50" /></td>
</tr>
<tr>
<td style="width: 1085px" class="style2" valign="top">Zip</td>
<td style="width: 1184px">
<input name="zip" type="text" size="50" /></td>
</tr>
<tr>
<td style="width: 1085px" class="style2" valign="top">Current
position/title</td>
<td style="width: 1184px">
<input name="position" type="text" size="50" /></td>
</tr>
<tr>
<td style="width: 1085px" class="style2" valign="top">Current
Current or last position at</td>
<td style="width: 1184px">
<input name="location" type="text" size="50" /></td>
</tr>
<tr>
<td style="width: 1085px; " class="style2" valign="top">
Employer type (restaurant, hotel, Club, etc)</td>
<td style="width: 1184px">
<input name="type" type="text" size="50" /></td>
</tr>
<tr>
<td style="width: 1085px" class="style2" valign="top">Desired
location</td>
<td style="width: 1184px">
<input name="Text16" type="text" size="50" /></td>
</tr>
<tr>
<td style="width: 1085px" class="style2" valign="top">Training or
education</td>
<td style="width: 1184px">
<input name="training" type="text" size="50" /></td>
</tr>
<tr>
<td style="width: 1085px; height: 40px;" class="style2" valign="top">Special skills(Asian, comfort, etc)</td>
<td style="width: 1184px; height: 40px;">
<input name="skills" type="text" size="50" /></td>
</tr>
<tr>
<td style="width: 1085px" class="style2" valign="top">
Culinary Style</td>
<td style="width: 1184px">
<input name="styles" type="text" size="50" /></td>
</tr>
<tr>
<td style="width: 1085px" class="style2" valign="top">
Comments</td>

<td style="width: 1184px">
<textarea name="comments" cols="45" rows="5"></textarea></td>


</tr>
<tr>
<td style="width: 1085px" class="style2" valign="top">Upload resume</td>
<td style="width: 1184px">
<input name="File1" type="file" size="45" /></td>
</tr>

</table>




<p><input type="submit" value="send" /p></p>


?>

</form>
</body>
</html>

The Form Handler

<?php
include_once('class.phpmailer.php');
#in same directory
$mail = new PHPMailer(); // defaults to using php "mail()"
error_reporting(E_ALL);
//error_reporting(E_STRICT);
$about = "Resume from";
$about .= $_POST['first'];
$about .= " ";
$about .= $_POST['last_name'];
$about .= ' ';

$candidate = $_POST['first'];
$candidate .= " ";
$candidate .= $_POST['last_name'];
$content = "<html>";
$content .= $first;
$content .= ' ';
$content .= $last_name;
$content .= "<br>";
$content .= $position;
$content .= "<br>";
$content .= $location;
$content .= "</html>";

$phone = $_POST['phone'];
$cell = $_POST['cell'];
$city= $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$email = $_POST['email'];
$position = $_POST['position'];
$location = $_POST['location'];
$type = $_POST['type'];
$myadd = "me@mydomain.com";
$myAssc = "assc@mydom.com";


date_default_timezone_set('America/Toronto');
//date_default_timezone_set(date_default_timezone_get());



$mail->To = $myadd;
$mail->From = $email;
$mail->FromName = $candidate;

$Mail->WordWrap = 50; // set word wrap
$mail->Subject = $about;
$mail->Body = $content;
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

$mail->MsgHTML = $content;

$mail->AddAddress($myadd);

if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top