Page 1 of 1

[SOLVED] how to amalgamate my data and pass it to swift form

Posted: Mon Apr 02, 2007 7:04 am
by roscoe
I have installed swift and it works with the sendmail method.

I search the forum and found you had created a form for someone to lob the data at swift. My knowledge is not that great of sessions and I need to pass some other variables to this form.

Code: Select all

//Start session 
session_start(); 

//Function to display the values in the fields 
function paint_value($field) 
{ 
    if (!empty($_SESSION["post"][$field])) 
    { 
        echo $_SESSION["post"][$field]; 
    } 
    else { echo ""; } 
} 

//Check if errors were sent 
if (!empty($_GET["error"])) 
{ 
    switch ($_GET["error"]) 
    { 
        case "incomplete": 
            $field_labels = array("user_name" => "Name", "email" => "E-mail address", "subject" => "Subject", "comments" => "Comments"); 
            $incomplete = explode(",", $_GET["fields"]); 
            ?><div class="error">All required fields (*) were not completed.  Please check the following fields:
                <ul> 
                    <?php foreach ($incomplete as $field) { 
                        if (isset($field_labels[$field])) { echo "<li>" . $field_labels[$field] . "</li>"; } 
                    } ?> 
                </ul></div><?php 
            break; 
        case "email_invalid": 
            ?><div class="error">The email address entered does not appear to be a valid format</div><?php 
            break; 
        case "attachment_failed": 
            ?><div class="error">The attachment upload failed.  Perhaps the file is too large?</div><?php 
            break; 
        case "runtime_error": 
            ?><div class="error">There was a problem processing your request.  Please try again later.</div><?php 
            break; 
    } 
} 

?> 
<form action="mail_handler.php" method="post" enctype="multipart/form-data"> 
    <div class="row"> 
        <div class="label">Your name*:</div> 
        <div class="field"><input type="text" name="user_name" value="<?php paint_value("user_name"); ?>" /></div> 
    </div> 
    <div class="row"> 
        <div class="label">Your e-mail*:</div> 
        <div class="field"><input type="text" name="email" value="<?php paint_value("email"); ?>" /></div> 
    </div> 
    <div class="row"> 
        <div class="label">Subject*:</div> 
        <div class="field"><input type="text" name="subject" value="<?php paint_value("subject"); ?>" /></div> 
    </div> 
    <div class="row"> 
        <div class="label">Attachment:</div> 
        <div class="field"><input type="file" name="attachment" /></div> 
    </div> 
    <div class="row"> 
        <div class="label">Comments*:</div> 
        <div class="field"><textarea name="comments"><?php paint_value("comments"); ?></textarea></div> 
    </div> 
    <div class="row"> 
        <div class="label">&nbsp;</div> 
        <div class="field"><input type="submit" name="submit" value="Send" /> 
    </div> 
</form>
I need to pass 2 variables on to the form and subsequent email with attachment:
a hidden subject field
$subject=$title.$loco.$money;

$mailme (the address to SEND email to as this is variable)

how does this affect the

Code: Select all

"<?php paint_value("subject"); ?>
? and can I pass the

Code: Select all

var $CONNECTION_TYPE = "sendmail";   	
var $TO_ADDRESS = $mailme;
	/**
	 * The name of the recipient
	 */
	var $TO_NAME = $myuser.- " to";
Sorry to be so dense

Re: how to amalgamate my data and pass it to swift form

Posted: Mon Apr 02, 2007 8:29 am
by Chris Corbyn
roscoe wrote: I need to pass 2 variables on to the form and subsequent email with attachment:
a hidden subject field
$subject=$title.$loco.$money;

$mailme (the address to SEND email to as this is variable)

how does this affect the

Code: Select all

"<?php paint_value("subject"); ?>
?
If those fields are hard-coded in the PHP and do not need to be displayed on the form then just remove those fields from the form completely. You can just pass the standard PHP variables to Swift instead.
and can I pass the

Code: Select all

var $CONNECTION_TYPE = "sendmail";   	
var $TO_ADDRESS = $mailme;
	/**
	 * The name of the recipient
	 */
	var $TO_NAME = $myuser.- " to";
Sorry to be so dense
[/quote]

Syntactically that's wrong (you can't assign a variable value in the class declaration) but I'm maybe getting mixed up with why you're doing that. That code looks like it's come from the TestConfiguration.php file in Swift. That file has no role in the running of Swift, it's just there for testing purposes (so when you run the smoke tests it sends to your own mailbox). Swift reads values that are directly passed to the Swift components themselves :)

There's another form tutorial in the documentation online, perhaps you might want to read that too:

http://www.swiftmailer.org/wikidocs/v3/ ... /form2mail

Posted: Mon Apr 02, 2007 8:51 am
by roscoe
OK fixed sending through data:

Code: Select all

//Start session
session_start();

//Function to display the values in the fields
function paint_value($field)
{
    if (!empty($_SESSION["post"][$field]))
    {
        echo $_SESSION["post"][$field];
    }
    else { echo ""; }
}
//echo "$subject, $mailme";
//Check if errors were sent
if (!empty($_GET["error"]))
{
    switch ($_GET["error"])
    {
        case "incomplete":
            $field_labels = array("user_name" => "Name", "email" => "E-mail address", "subject" => "Subject", "comments" => "Comments");
            $incomplete = explode(",", $_GET["fields"]);
            ?><div class="error">All required fields (*) were not completed.  Please check the following fields:
                <ul>
                    <?php foreach ($incomplete as $field) {
                        if (isset($field_labels[$field])) { echo "<li>" . $field_labels[$field] . "</li>"; }
                    } ?>
                </ul></div><?php
            break;
        case "email_invalid":
            ?><div class="error">The email address entered does not appear to be a valid format</div><?php
            break;
        case "attachment_failed":
            ?><div class="error">The attachment upload failed.  Perhaps the file is too large?</div><?php
            break;
        case "runtime_error":
            ?><div class="error">There was a problem processing your request.  Please try again later.</div><?php
            break;
    }
}

?>
<form action="mail_handler.php" method="post" enctype="multipart/form-data">
    <div class="row">
        <div class="label">Your name*:</div>
        <div class="field"><input type="text" name="user_name" value="<?php paint_value("user_name"); ?>" /></div>
    </div>
    <div class="row">
        <div class="label">Your e-mail*:</div>
        <div class="field"><input type="text" name="email" value="<?php paint_value("email"); ?>" /></div>
    </div>


<?php echo "    <div class=\"row\">
        <div class=\"label\">Subject*:</div>
        <div class=\"field\">$subject<input type=\"hidden\" name=\"subject\" value=\"$subject\" /><input type=\"hidden\" name=\"to\" value=\"$mailme\" /></div>
    </div>";   ?>
    <div class="row">
        <div class="label">Attachment:</div>
        <div class="field"><input type="file" name="attachment" /></div>
    </div>
    <div class="row">
        <div class="label">Comments*:</div>
        <div class="field"><textarea name="comments"><?php paint_value("comments"); ?></textarea></div>
    </div>
    <div class="row">
        <div class="label">&nbsp;</div>
        <div class="field"><input type="submit" name="submit" value="Send" />
    </div>
</form>
then

Code: Select all

/** 1 **/
session_start();

/** 2 **/
//See if evil magic_quotes is enabled, and fix problems if it is
$quotes_on = (get_magic_quotes_gpc() || get_magic_quotes_runtime());
if ($quotes_on)
{
    foreach ($_POST as $key => $value)
    {
        $_POST[$key] = stripslashes($value);
    }
}

/** 3 **/
$_SESSION["post"] = $_POST;

/** 4 **/
//Load in the required Swift files
require_once "classes/Swift.php";
require_once "classes/Swift/Connection/Sendmail.php";

/** 5 **/
//Create an empty array where we can catch any fields which were not filled in
$fields_not_set = array();

//Check if all POST data was sent, redirect with an error if not
if (empty($_POST["user_name"])) $fields_not_set[] = "user_name";
if (empty($_POST["email"])) $fields_not_set[] = "email";
if (empty($_POST["subject"])) $fields_not_set[] = "subject";
if (empty($_POST["comments"])) $fields_not_set[] = "comments";

//If $fields_not_set contains any values, then something wasn't filled in. Time to redirect.
if (!empty($fields_not_set))
{
    //Read further down to see how we'll modify form.php to handle the error
    header("Location: form.php?error=incomplete&fields=" . implode(",", $fields_not_set));
    exit();
}

//Copy the POST data to standard globals
$user_name = $_POST["user_name"];
$email = $_POST["email"];
$subject = $_POST["subject"];
$comments = $_POST["comments"];

/** 6 **/
//This is a RegExp I've adopted for validating email addresses.  NOTE that it's NOT RFC compliant.
// Use another regexp at your own choice
$email_re = '(?#Start of dot-atom
                )[-!#\$%&\'\*\+\/=\?\^_`{}\|~0-9A-Za-z]+(?:\.[-!#\$%&\'\*\+\/=\?\^_`{}\|~0-9A-Za-z]+)*(?#
                End of dot-atom)(?:@(?#Start of domain)[-0-9A-Za-z]+(?:\.[-0-9A-Za-z]+)*(?#End of domain))?';

//Now check if the email address they gave is valid, redirect back to the form if not
if (!preg_match('/^' . $email_re . '$/', $email))
{
    header("Location: form.php?error=email_invalid");
    exit();
}

/** 7 **/
$attachment_data = array();
//Now check if there's an attachment they've sent
if (!empty($_FILES["attachment"]["tmp_name"]))
{
    //If an attachment was sent, but there was an error, redirect
    if ($_FILES["attachment"]["error"] != 0)
    {
        header("Location: form.php?error=attachment_failed");
        exit();
    }
    else $attachment_data = $_FILES["attachment"];
}

/** 8 **/
//Everything looks ok to send an email, create an instance of Swift
$swift = new Swift(new Swift_Connection_SMTP("mail.w3style.co.uk"));

/** 9 **/
//Now build your message body
$body = "A message was sent from " . $user_name . " with the title '" . $subject . "'";
if (!empty($attachment_data))
{
    $body .= "\r\nAn attachment with the name '" . $attachment_data["name"] . "' was added";
}

/** 10 **/
//Attach any files if they were sent
// PHP stores files in a temporary location and cleans up itself, so we'll just read the temporary file
if (!empty($attachment_data))
{
    $attachment_str = file_get_contents($attachment_data["tmp_name"]);
    //Check if we need to remove slashes (again!!)
    if ($quotes_on)
    {
        $attachment_str = stripslashes($attachment_str);
    }
    $swift->addAttachment(
        $attachment_str, $attachment_data["name"], $attachment_data["type"]);
}

/** 11 **/
//Add the email body
$swift->addPart($body);

/** 12 **/
//Try sending the email.
// Redirect to success page on success, or form on failure
if ($swift->send("ros@xxxxx.com", $email, $subject))
{
    unset($_SESSION["post"]); //It worked, we have no reason to keep this data
    $swift->close();
    header("Location: success.php"); /** 13 **/
    exit();
}
else
{
    $swift->close();
    header("Location: form.php?error=runtime_error"); /** 13 **/
    exit();
}


//End of script
error from your script :

Parse error: parse error, unexpected T_STRING in /home/jobsyoulike/public_html/2007/mailoutattach/classes/Swift.php on line 70 which is


Code: Select all

interface Swift_IPlugin
and here I am lost....I would be grateful for the heads up

Posted: Mon Apr 02, 2007 8:55 am
by feyd
I would have to guess you're attempting to use the PHP 5 code in a PHP 4 environment.

Posted: Mon Apr 02, 2007 9:08 am
by roscoe
blast and damn.

If I connect the mail_handler.php to the swift.php for V 3 will it work?

Posted: Mon Apr 02, 2007 9:11 am
by Chris Corbyn
roscoe wrote:blast and damn.

If I connect the mail_handler.php to the swift.php for V 3 will it work?
No. If you want a V3 form see the tutorial I linked to. If you want a v2 copy of Swift I keep them all backed up including publicly available copies here: ftp://ftp.swiftmailer.org/

Posted: Mon Apr 02, 2007 9:47 am
by roscoe
I downloaded the original Swift-3.1.1-php4.zip

and using your handle_form.php in the tut the form blinks and sends no email or attachment (eventhough the smoke test basic worked with sendmail)

?

Posted: Mon Apr 02, 2007 9:59 am
by Chris Corbyn
Did you change the form to correspond with the other handle_form.php? Sounds like it's redirecting back to the form with an error about missing fields, except your form won't display the error if it's not the correct form.

Posted: Mon Apr 02, 2007 10:21 am
by roscoe
ok checked out your suggestion and used the other form and the same problem, it reverts back to the form and clears he data.... no response on th email front.


I tried the latest files for Version 4 php

here again is the scripts:

Code: Select all

<?php

//Display an error if something went wrong
if (!empty($_GET["error"]))
{
    switch ($_GET["error"])
    {
        case "not_enough_info": ?>
            <strong style="color: red;">You need to complete all fields marked *<strong><?php
            break;
        case "invalid_email": ?>
            <strong style="color: red;">Please provide a valid email address</strong><?php
            break;
        case "upload_failed": ?>
            <strong style="color: red;">The file you uploaded failed to attach, this could be a temporary problem.
            Please try later.</strong><?php
            break;
        case "sending_failed": ?>
            <strong style="color: red;">Temporary problem, please try later.</strong><?php
            break;
    }
}

?>

<form action="form.php" method="post" enctype="multipart/form-data">
    <table>
        <tr>
            <td class="label">Name *</td>
            <td><input type="text" name="sender_name" value="" /></td>
        </tr>
        <tr>
            <td class="label">E-mail address *</td>
            <td><input type="text" name="sender_email" value="" /></td>
        </tr>





         <?php echo "  <tr>
            <td class=\"label\">Subject *</td>
            <td><input type=\"hidden\" name=\"comment_title\" value=\"$subject\" />$subject</td>
        </tr> "; ?>
        <tr>
            <td class="label">Attachment (optional)</td>
            <td><input type="file" name="attachment" /></td>
        </tr>
        <tr>
            <td colspan="2">Comment *<br />
                <textarea name="comment_body" rows="10" cols="30"></textarea></td>
        </tr>
        <tr>
            <td colspan="2"><input type="submit" name="submit" value="Submit" /></td>
        </tr>
    </table>
</form>
and the handler.....

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 "mylib/Swift.php";
require_once "mylib/Swift/Connection/Sendmail.php";

//Enable disk caching if we can
if (is_writable("/tmp"))
{
    Swift_CacheFactory::setClassName("Swift_Cache_Disk");
    Swift_Cache_Disk::setSavePath("/tmp");
}

//Create a Swift instance
$swift =& new Swift(new Swift_Connection_SMTP("your_smtp_server.tld"));

//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, "ros@xxx.com", $sender);  
 // my proper address IS in here....
//Disconnect from SMTP, we're done
$swift->disconnect();

if ($sent)
{
    header("Location: success.php");
    exit();
}
else
{
    header("Location: form.php?error=sending_failed");
    exit();
}
What am I missing here????? :(

Posted: Mon Apr 02, 2007 11:15 am
by roscoe
OK ignore above, I sorted that, can you help with error message

Fatal error: Cannot instantiate non-existent class: swift_connection_smtp in /home/jobsyoulike/public_html/2007/mailoutattach/mail_handler.php on line 59


this is

Code: Select all

$swift =& new Swift(new Swift_Connection_SMTP("your_smtp_server.tld"));
I do not wish to use the smtp, I would like to use the sendmail.

Can I // this out??

Posted: Mon Apr 02, 2007 2:41 pm
by Chris Corbyn
I'll leave you to sweat that one out for a bit then ;) I'm sure you'll figure it out :)

(Sorry, not being cheeky with you but you pretty much answered your own question)

Maybe this will be useful: http://www.swiftmailer.org/wikidocs/v3/ ... s/sendmail