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.
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.
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:
/** 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
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/
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.
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.
<?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();
}
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