Parse error: syntax error, unexpected $end +using gmail

Swift Mailer is a fantastic library for sending email with php. Discuss this library or ask any questions about it here.

Moderators: Chris Corbyn, General Moderators

Post Reply
teckmax
Forum Newbie
Posts: 2
Joined: Sat Mar 08, 2008 11:58 am

Parse error: syntax error, unexpected $end +using gmail

Post by teckmax »

Hey guys - I'm a complete newb when it comes to php and have been working on this for about 3 hours. I finally give up and admit to needing help.

I'm using the example and trying to send mail via my hosted gmail account. Here's what I have so far in the handle_form.php file. The form.php file is exactly like the tutorial on your site.

I tried turning on the logging for more information, but got nowhere. I am using php4 and have the correct swift version.

Code: Select all

<?php
 
$log =& Swift_LogContainer::getLog();
$log->setLogLevel(4);
 
//Check if the required fields were sent
// Redirect back to the form if not
if (empty($_POST["sender_name"]) || empty($_POST["sender_email"])
    || empty($_POST["comment_title"]) || empty($_POST["comment_body"]))
{
    //redirect back to form
    header("Location: ./form.php?error=not_enough_info"); //This should really be an absolute URL if you know it
    exit();
}
 
//Copy into global variables
$name = $_POST["sender_name"];
$email = $_POST["sender_email"];
$title = $_POST["comment_title"];
$body = $_POST["comment_body"];
 
//Validate the email address using a regex (I suggest you use a better one than this!!)
if (!preg_match("/[a-zA-Z0-9_\\.-]+@[a-zA-Z0-9_\\.-]+/", $email))
{
    header("Location: ./form.php?error=invalid_email");
    exit();
}
 
//Check if an attachment was uploaded
$file_path = false;
$file_name = false;
$file_type = false;
if (!empty($_FILES["attachment"]["tmp_name"]))
{
    if ($_FILES["attachment"]["error"])
    {
        //Redirect if the upload has failed
        header("Location: ./form.php?error=upload_failed");
        exit();
    }
    $file_path = $_FILES["attachment"]["tmp_name"];
    $file_name = $_FILES["attachment"]["name"];
    $file_type = $_FILES["attachment"]["type"];
}
 
//Everything looks ok, we can start Swift
 
require_once "lib/Swift.php";
require_once "lib/Swift/Connection/SMTP.php";
 
//Enable disk caching if we can
if (is_writable("/tmp"))
{
    Swift_CacheFactory::setClassName("Swift_Cache_Disk");
    Swift_Cache_Disk::setSavePath("/tmp");
}
 
require_once "lib/Swift.php";
require_once "lib/Swift/Connection/SMTP.php";
  
//Connect to Gmail (PHP4)
$swift =& new Swift(new Swift_Connection_SMTP("smtp.gmail.com", SWIFT_SMTP_PORT_SECURE, SWIFT_SMTP_ENC_TLS));
 
$smtp->setUsername('support@mydomain.com');
$smtp->setPassword('password');
$swift =& new Swift($smtp);
 
//Create the sender from the details we've been given
$sender =& new Swift_Address($email, $name);
 
//Create the message to send
$message =& new Swift_Message("New comment: " . $title);
$message->attach(new Swift_Message_Part($body));
 
//If an attachment was sent, attach it
if ($file_path && $file_name && $file_type)
{
    $message->attach(
        new Swift_Message_Attachment(new Swift_File($file_path), $file_name, $file_type));
}
 
//Try sending the email
$sent = $swift->send($message, "support@mydomain.com", $sender);
//Disconnect from SMTP, we're done
$swift->disconnect();
 
if ($sent)
{
    header("Location: ./success.php");
    exit();
}
else
{
    header("Location: ./form.php?error=sending_failed");
    exit();
 
$log =& Swift_LogContainer::getLog();
echo $log->dump(true);
 
?>
The error I get is:
Parse error: syntax error, unexpected $end in /home/username/public_html/contactus/handle_form.php on line 100
Line 100 is the last line of my handle_form.php file.

Everything else is done exactly as the documentation on your site shows. Please help if you do not mind. I would GREATLY appreciate it.

Thank you,
Nick
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Parse error: syntax error, unexpected $end +using gmail

Post by John Cartwright »

Your missing a closing curly bracket. My guess is around Line 96 :)
teckmax
Forum Newbie
Posts: 2
Joined: Sat Mar 08, 2008 11:58 am

Re: Parse error: syntax error, unexpected $end +using gmail

Post by teckmax »

Thank you very much for taking the time to look at that and I'm sorry for the dumb mistake. :oops:

I fixed that and removed the logging code. But I'm still getting an error.

Here is my code now.

Code: Select all

<?php
 
//Check if the required fields were sent
// Redirect back to the form if not
if (empty($_POST["sender_name"]) || empty($_POST["sender_email"])
    || empty($_POST["comment_title"]) || empty($_POST["comment_body"]))
{
    //redirect back to form
    header("Location: ./form.php?error=not_enough_info"); //This should really be an absolute URL if you know it
    exit();
}
 
//Copy into global variables
$name = $_POST["sender_name"];
$email = $_POST["sender_email"];
$title = $_POST["comment_title"];
$body = $_POST["comment_body"];
 
//Validate the email address using a regex (I suggest you use a better one than this!!)
if (!preg_match("/[a-zA-Z0-9_\\.-]+@[a-zA-Z0-9_\\.-]+/", $email))
{
    header("Location: ./form.php?error=invalid_email");
    exit();
}
 
//Check if an attachment was uploaded
$file_path = false;
$file_name = false;
$file_type = false;
if (!empty($_FILES["attachment"]["tmp_name"]))
{
    if ($_FILES["attachment"]["error"])
    {
        //Redirect if the upload has failed
        header("Location: ./form.php?error=upload_failed");
        exit();
    }
    $file_path = $_FILES["attachment"]["tmp_name"];
    $file_name = $_FILES["attachment"]["name"];
    $file_type = $_FILES["attachment"]["type"];
}
 
//Everything looks ok, we can start Swift
 
require_once "lib/Swift.php";
require_once "lib/Swift/Connection/SMTP.php";
 
//Enable disk caching if we can
if (is_writable("/tmp"))
{
    Swift_CacheFactory::setClassName("Swift_Cache_Disk");
    Swift_Cache_Disk::setSavePath("/tmp");
}
 
require_once "lib/Swift.php";
require_once "lib/Swift/Connection/SMTP.php";
  
//Connect to Gmail (PHP4)
$swift =& new Swift(new Swift_Connection_SMTP("smtp.gmail.com", SWIFT_SMTP_PORT_SECURE, SWIFT_SMTP_ENC_TLS));
 
$smtp->setUsername('support@mydomain.com');
$smtp->setPassword('password');
$swift =& new Swift($smtp);
 
//Create the sender from the details we've been given
$sender =& new Swift_Address($email, $name);
 
//Create the message to send
$message =& new Swift_Message("New comment: " . $title);
$message->attach(new Swift_Message_Part($body));
 
//If an attachment was sent, attach it
if ($file_path && $file_name && $file_type)
{
    $message->attach(
        new Swift_Message_Attachment(new Swift_File($file_path), $file_name, $file_type));
}
 
//Try sending the email
$sent = $swift->send($message, "support@mydomain.com", $sender);
//Disconnect from SMTP, we're done
$swift->disconnect();
 
if ($sent)
{
    header("Location: ./success.php");
    exit();
}
else
{
    header("Location: ./form.php?error=sending_failed");
    exit();
}
 
?>
The error I get is this:
Fatal error: Call to a member function on a non-object in /home/username/public_html/contactus/handle_form.php on line 61
Please help if you don't mind... :)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Parse error: syntax error, unexpected $end +using gmail

Post by Chris Corbyn »

I need an FAQ... this has to be in the top 5 most common mistakes.

Code: Select all

$swift =& new Swift(new Swift_Connection_SMTP("smtp.gmail.com", SWIFT_SMTP_PORT_SECURE, SWIFT_SMTP_ENC_TLS));
That hasn't created an object called $smtp. You need to break it up a bit:

Code: Select all

$smtp =& new Swift_Connection_SMTP("smtp.gmail.com", SWIFT_SMTP_PORT_SECURE, SWIFT_SMTP_ENC_TLS);
$stmp->setUsername( ... );
$smtp->setPassword( ... );
 
$swift =& new Swift($smtp);
Post Reply