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!

unexpected T_ECHO from phpmailer_v5.1 1

Status
Not open for further replies.

usit

Technical User
May 2, 2011
8
US
Running PHP5.2.6 +Lemy5 on APACHE 2.0.49 my phpmailer_v5.1 program throws the following error:

‘Parse error: syntax error, unexpected T_ECHO in /home/vg010web02/68/65/2926568/web/lib/phpmailer/phpmailer5/test/class.phpmailer.php on line 533’

The script calling class.phpmailer.php came with phpmailer_v5.1 as an example script. I added parameter values needed for my server.

I understand that T_ECHO often refers to incomplete pairs of (), {}, ‘’, “”, or missing ;’s.
I’ve searched the 1st 600 lines of class.phpmailer.php and my modified mailer script and haven’t found the problem.
_________________________________________________________________
My mailer script is ...
my_test_smtp_advanced_no_auth.php (test_smtp_advanced_no_auth.php as shipped with phpmailer_v5.1 with my parameters added)
-->
<?php
require_once('class.phpmailer.php');

$mail = new PHPMailer(true);
$mail->IsSMTP();$contents = 'Test_Msg.html';
$attachment = $_SERVER{'DOCUMENT_ROOT'} . '/PHPfiles/NwsLtr/Nwsltr_pdfs/$EnNL51_60.pdf';

try {
$mail->Host = "smtp.usit.net";
$mail->SMTPDebug = 2;
$mail->AddReplyTo('my@website', 'me');

$mail->AddAddress('friend@friends_email', 'k.friend');
$mail->SetFrom('my@website', 'me');
$mail->AddReplyTo('my@website', 'me');

$mail->Subject = 'PHPMailer Test -- let me know if you get this.';
//$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';
$mail->MsgHTML(file_get_contents('contents'));

$mail->AddAttachment($_SERVER{'DOCUMENT_ROOT'} . '/auxlib/web07/pix/image001.gif');
//$mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
$mail->Send();
echo "Message Sent OK</p>\n";

} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
?>
_________________________________________________________________

The error points to line 533 in a ValidateAddress() method of class.phpmailer.php. The method follows ...

public static function ValidateAddress($address) {
if (function_exists('filter_var')) { //Introduced in PHP 5.2
if(filter_var($address, FILTER_VALIDATE_EMAIL) === FALSE) {
return false;
} else { <---Line 533
return true;
}
} else {
return preg_match('/^(?:[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+\.)*[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+@(?:(?:(?:[a-zA-Z0-9_](?:[a-zA-Z0-9_\-](?!\.)){0,61}[a-zA-Z0-9_-]?\.)+[a-zA-Z0-9_](?:[a-zA-Z0-9_\-](?!$)){0,61}[a-zA-Z0-9_]?)|(?:\[(?:(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\.){3}(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\]))$/', $address);
}
}
_________________________________________________________________
I was at first suspicious of the preg_match, but it occurs after the error is thrown.

My purpose in this exercise is to repair a crashed registration page on my website by moving to PHP5 and an updated version of phpmailer. I have not succeeded.

Any help will be appreciated.
EdS

 
nope. that's not the line. nothing wrong with that method and no echo in it either.

but your code has plenty of errors

Code:
$mail = new PHPMailer(true); 
$mail->IsSMTP();$contents = 'Test_Msg.html';
[red]//array elements use square braces [/red]
$attachment = $_SERVER{'DOCUMENT_ROOT'} . '/PHPfiles/NwsLtr/Nwsltr_pdfs/$EnNL51_60.pdf';

try {
  $mail->Host       = "smtp.usit.net";     
  $mail->SMTPDebug  = 2;        
  $mail->AddReplyTo('my@website', 'me');

  $mail->AddAddress('friend@friends_email', 'k.friend');
  $mail->SetFrom('my@website', 'me');
  $mail->AddReplyTo('my@website', 'me');

  $mail->Subject = 'PHPMailer Test -- let me know if you get this.';
  //$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';  
[red]//this should reference $contents one assumes.  not 'contents'[/red]
$mail->MsgHTML(file_get_contents('contents'));

[red]//array elements use square brackets[/red]
  $mail->AddAttachment($_SERVER{'DOCUMENT_ROOT'} . '/auxlib/web07/pix/image001.gif');  
  //$mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
  $mail->Send();
  echo "Message Sent OK</p>\n";
 
Thanks jpadie. Your quick response and trained eyes are appreciated. I changed array notations from {} to []. You're correct file_get_contents needs a $. $contents is a file in the same directory as this program. After making these corrections I get the same error.
Any other insights to share?

try {
$mail->Host = "smtp.usit.net";
$mail->SMTPDebug = 2;
$mail->AddReplyTo('my@website', 'me');

$mail->AddAddress('friend@friends_email', 'k.friend');
$mail->SetFrom('my@website', 'me');
$mail->AddReplyTo('my@website', 'me');

$mail->Subject = 'PHPMailer Test -- let me know if you get this.';
//$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';
$mail->MsgHTML(file_get_contents('$contents'));

$mail->AddAttachment($_SERVER['DOCUMENT_ROOT'] . '/auxlib/web07/pix/image001.gif');
//$mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
$mail->Send();
echo "Message Sent OK</p>\n";

} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
?>
usit
 
The $contents should not be enquoted. variables are not expanded within single quotes.

apart from that i see nothing intrinsically wrong in your script. likewise I have more than 50 installations using phpmailer 5.1 and there is no syntax error in the library.

i suggest that you redownload the library from a trusted source. the ONLY files that you need in your path are class.phpmailer.php and class.smtp.php.
 
Thanks again jpadie,progress is evident.
.removed encoding of $contents but the error was still thrown.
..I was impressed with your 51 successful installations so I redownloaded class.phpmailer.php from Andy Prevost's Worx International site. In stalled fresh copies of class.phpmailer.php and class.smtp.php and now the program runs! :)
However, no mail is sent. Suspicious of incomplete parameter initialization in class.phpmailer.php I checked them again. Here are the parameters I initialized:
__________________________________________
class PHPMailer {

// PROPERTIES, PUBLIC

public $From = 'ntelleck@u-sit.net';
public $FromName = 'Ntelleck';
public $Sender = 'ntelleck@u-sit.net';

public $Mailer = 'smtp';
public $Sendmail = '/usr/sbin/sendmail';
public $Hostname = 'ntelleck.u-sit.net';

// PROPERTIES FOR SMTP

public $Host = 'smtp.u-sit.net';
________________________________________________
Do you see any oversights/errors?

I really appreciate your assistance.

usit
 
ok.

first off don't edit class.phpmailer.php. leave it alone. completely.

to send an email via smtp (which appears to be what you are doing) just do this

Code:
require 'path/to/class.phpmailer.php';
$mail = new phpmailer;

//use smtp
$mail->isSMTP();
$mail->Host = 'smtp.u-sit.net';
//note that this assumes you have relay rights on this server and do not need any username or password

//set up addresses
$mail->SetFrom('my@website', 'me');
$mail->AddReplyTo('my@website', 'me');
$mail->AddAddress('friend@friends_email', 'k.friend');

//set up subject
$mail->Subject = 'PHPMailer Test -- let me know if you get this.';
$mail->IsHTML(true);
$mail->Body(file_get_contents($contents));

//send message
if(!$mail->Send()):
   echo 'Message was not sent.';
   echo 'Mailer error: ' . $mail->ErrorInfo;
else:
   echo 'Message has been sent.';
endif;
 
More thanks jpadie.
I tried w/wo smtp authentication -- no luck

The following script, run with or without authentication, can’t make an smtp connection. It throws this error both times:
Fatal error: Uncaught exception 'phpmailerException' with message 'SMTP Error: Could not connect to SMTP host.' in /home/vg010web02/68/65/2926568/web/lib/phpmailer/phpmailer5/test/class.phpmailer.php:820 Stack trace: #0 /home/vg010web02/68/65/2926568/web/lib/phpmailer/phpmailer5/test/class.phpmailer.php(705): PHPMailer->SmtpConnect() #1 /home/vg010web02/68/65/2926568/web/lib/phpmailer/phpmailer5/test/class.phpmailer.php(576): PHPMailer->SmtpSend('Date: Tue, 3 Ma...', '--b1_92209666ed...') #2 /home/vg010web02/68/65/2926568/web/lib/phpmailer/phpmailer5/test/my_test_smtp_advanced_no_auth.php(42): PHPMailer->Send() #3 {main} thrown in /home/vg010web02/68/65/2926568/web/lib/phpmailer/phpmailer5/test/class.phpmailer.php on line 820

<?php
//required programs/data
require_once('class.phpmailer.php'); //$_SERVER['DOCUMENT_ROOT'] . 'lib/phpmailer/phpmailer5/test/
require_once('class.smtp.php');
$mail = new PHPMailer(true); // true throws exceptions on errors

$contents = 'Test_Msg.html';
$attachment = $_SERVER['DOCUMENT_ROOT'] . '/PHPfiles/NwsLtr/Nwsltr_pdfs/$EnNL51_60.pdf';

//use smtp
$mail->IsSMTP();
$mail->Host = "smtp.usit.net:25;smtp.usit.net:587"; //offering 2 ports

//use authentication: the next 3 lines are included when run with authentication
//$mail->SMTPAuth = true;
//$mail->Username = "account name"; // SMTP account
//$mail->Password = "pw"; // SMTP account

//set up addresses
$mail->SetFrom('my@domain', 'me');
$mail->AddReplyTo('my@domain', 'me');
$mail->AddAddress('my@domain', 'me');

//set up subject
$mail->Subject = 'PHPMailer test -- let me know if you get this.';
$mail->IsHTML(true);
$mail->MsgHTML(file_get_contents($contents)); //Body() is not defined

//send message
if(!$mail->Send()){
echo 'Message was not sent!';
echo 'Mailer error:' .$smail->ErrorInfo;
}else{
echo 'Message has been sent.';
}
?>

I'm missing something??
usit
 
More thanks jpadie,
moving 2 steps forward then 1 step back.

The following script, run with or without authentication, can’t make an smtp connection. It throws this error both times:
Fatal error: Uncaught exception 'phpmailerException' with message 'SMTP Error: Could not connect to SMTP host.' in /home/vg010web02/68/65/2926568/web/lib/phpmailer/phpmailer5/test/class.phpmailer.php:820 Stack trace: #0 /home/vg010web02/68/65/2926568/web/lib/phpmailer/phpmailer5/test/class.phpmailer.php(705): PHPMailer->SmtpConnect() #1 /home/vg010web02/68/65/2926568/web/lib/phpmailer/phpmailer5/test/class.phpmailer.php(576): PHPMailer->SmtpSend('Date: Tue, 3 Ma...', '--b1_92209666ed...') #2 /home/vg010web02/68/65/2926568/web/lib/phpmailer/phpmailer5/test/my_test_smtp_advanced_no_auth.php(42): PHPMailer->Send() #3 {main} thrown in /home/vg010web02/68/65/2926568/web/lib/phpmailer/phpmailer5/test/class.phpmailer.php on line 820

<?php
//required programs/data
require_once('class.phpmailer.php'); //$_SERVER['DOCUMENT_ROOT'] . 'lib/phpmailer/phpmailer5/test/
require_once('class.smtp.php');
$mail = new PHPMailer(true); // true throws exceptions on errors

$contents = 'Test_Msg.html';
$attachment = $_SERVER['DOCUMENT_ROOT'] . '/PHPfiles/NwsLtr/Nwsltr_pdfs/$EnNL51_60.pdf';

//use smtp
$mail->IsSMTP();
$mail->Host = "smtp.usit.net:25;smtp.usit.net:587"; //offering 2 ports

//use authentication: the next 3 lines are included when run with authentication
//$mail->SMTPAuth = true;
//$mail->Username = "account name"; // SMTP account
//$mail->Password = "pw"; // SMTP account

//set up addresses
$mail->SetFrom('my@domain', 'me');
$mail->AddReplyTo('my@domain', 'me');
$mail->AddAddress('my@domain', 'me');

//set up subject
$mail->Subject = 'PHPMailer test -- let me know if you get this.';
$mail->IsHTML(true);
$mail->MsgHTML(file_get_contents($contents)); //Body() is not defined

//send message
if(!$mail->Send()){
echo 'Message was not sent!';
echo 'Mailer error:' .$smail->ErrorInfo;
}else{
echo 'Message has been sent.';
}
?>

I'm missing something, but what?

usit
 
the first bit just means that you are using phpmailer with exceptions rather than traditional errors. enclosing in a try { } catch { } block will resolve this.

the error says it all. You cannot connect to the HOST that you have specified in your connection script. Either it is not available on the standard port 25 or it is refusing connections from your IP address. Remember, the way you have set up your script suggests that you MUST have relay permissions on that SMTP server and that SMTP server MUST allow unauthenticated connections from your IP address. If any of these are not true then it simply will not work and you should consider using an alternative MTA method. Which might include SMTP Auth, normal php mail() etc etc.

I can telnet into port 25 of your smtp server, so it does accept incoming connections. if you try again with your Smtp auth settings turned on, you may get a more verbose error message that will help debug.

If you cannot connect on port 25 then something on your server or network may be blocking outbound connections.

 
jpadie thanks for your excellent service.
Email plus attachments is working.
I have been chasing some red herrings.
In the definition of $host='localhost' it shows how to use multiple hosts to be tried if a previous one fails:
(e.g. "smtp1.example.com:25;smtp2.example.com").
I naively used this format to offer two ports, 25, and 587.
(e.g. "smtp.mydomain.net:25;smtp.mydomain.net:587").
Once I eliminated 25 in favor of 587 I finally got a connection.
At the same time I found a missing '-' in my host definition. So I can't claim to know which caused the error.
For several days I had been running the test script with/without authentication. The latter proved to be necessary.
Thanks for your continued interest.
usit
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top