Page 1 of 1

[SOLVED] Failing to send all form data.

Posted: Mon Jun 02, 2008 11:12 am
by bullzi
I am failing to successfully code to retrieve all data from my form.
First my successes:
I successfully bounce to error messages for missing required fields.
I successfully error on invalid email address.
I successfully send an attachment.
I successfully bounce to my "success" page.

My failures:
I only get "name", no "email" for the $sender.
The only form field I get upon submission is "name".

My current code below only gives me a mime_attachment that includes "name".

Code: Select all

<?php
//Check if the required fields were sent
// Redirect back to the form if not
if (empty($_POST["name"]) || empty($_POST["email"]) || empty($_POST["organization"]) || empty($_POST["phone"]))
{
//redirect back to form
header("Location: support.php?error=not_enough_info"); //This should really be an absolute URL if you know it
exit();
}
//Copy into global variables
$name = $_POST["name"];
$organization = $_POST["organization"];
$phone = $_POST["phone"];
$email = $_POST["email"];
$fax = $_POST["fax"];
$address = $_POST["address"];
$city = $_POST["city"];
$state = $_POST["state"];
$zip = $_POST["zip"];
$body = $_POST["comments"];
$title = $_POST["subject"];
//Validate the email address using a regex
if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $email))
{
    header("Location: /support.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: /support.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 "bullzi_swift/Swift.php";
require_once "bullzi_swift/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");
}
//Try to connect using /usr/sbin/sendmail -bs
$swift =& new Swift(new Swift_Connection_Sendmail());
//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($title, $name, $organization, $phone, $fax, $address, $city, $state, $zip, $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, "me@mysite.tld", $sender);
//Disconnect from SMTP, we're done
$swift->disconnect();
if ($sent)
{
    header("Location: /thanks.html");
    exit();
}
else
{
    header("Location: /support.php?error=sending_failed");
    exit();
}
?>
I tried the code below and it gives me the "name" text without the mime_attachment, but only "name".

Code: Select all

 
$message =& new Swift_Message($title);
$message->attach(new Swift_Message_Part($name));
$message->attach(new Swift_Message_Part($organization));
$message->attach(new Swift_Message_Part($phone));
$message->attach(new Swift_Message_Part($fax));
$message->attach(new Swift_Message_Part($address));
$message->attach(new Swift_Message_Part($city));
$message->attach(new Swift_Message_Part($state));
$message->attach(new Swift_Message_Part($zip));
$message->attach(new Swift_Message_Part($body));
I'm obviously overlooking something in the Swift Message tutorial, but can't figure it out.

Re: Failing to send all form data.

Posted: Tue Jun 03, 2008 1:06 pm
by vargadanis

Code: Select all

if (empty($_POST["name"]) || empty($_POST["email"]) || empty($_POST["organization"]) || empty($_POST["phone"]))
The only thing that I recognized in ur quote is that u are not using brackets before and after the empty() function. It might mean nothing but it can be the problem. I always use () cause it is easier for me to read the code.
And how can the code pass this line if you can only get the name part of it? Can u post the form which sends the data? Is it a type multipart/form-data?

Re: Failing to send all form data.

Posted: Tue Jun 03, 2008 1:28 pm
by bullzi
Yes, it is multipart-form data.
The "if empty" line you are referring to is for required fields and it works fine. I copied from the form tutorial and adjusted to my needs.

My main problem is I can't figure out the proper way of writing the Swift_Message and Swift_Message_Part. All I'm trying to do is send the data from each form field, but all I get is the "name" field. I'm far from a PHP wizard and I've tried dozens of approaches.

All I know is $message somehow has to include all of the data and the form tutorial and the Swift Message Basics don't explain it very well. I've been through the PHP Manual, the tutorials, the API info, and continue to struggle.

It all comes down to how do I get the form fields into a $message.

Re: Failing to send all form data.

Posted: Tue Jun 03, 2008 1:35 pm
by vargadanis
I personally do not know Swift but if it is the only mesasge part you can send than maybe you should construct the message previously and add that message as message part:

Code: Select all

$msg_part = $_POST['email']. " - " . $_POST['name']; // and so on...
maybe..

Re: Failing to send all form data.

Posted: Tue Jun 03, 2008 1:41 pm
by vargadanis
As far as I understand Swift mailer you can add MIME parts with the attach function and not to add data to the part. Mime parts are quite different.

Take a look at the last example on this website: http://www.swiftmailer.org/wikidocs/v3/ ... ion/basics

I am confident now that what you need to do is composing the body of the mail manually and add it as a text part:

Code: Select all

$message =& new Swift_Message("My subject");
 
$part1 =& new Swift_Message_Part("Plain text part");
$part1->setCharset("iso-8859-2");
$message->attach($part1);
iso-8859-2 is centrail EU character set. If you send only english text u can use UTF-8 or maybe US-ASCII

Regards

Re: Failing to send all form data.

Posted: Tue Jun 03, 2008 1:53 pm
by bullzi
This is my form:

Code: Select all

 
<form method="post" name="supportform" enctype="multipart/form-data" action="support_handler.php"> 
                <input name="name" type="text" value="" size="56">
                <span class="fixedfont10"><strong><font face="Arial, Helvetica, sans-serif">Name</font></strong> 
                <strong><font color="#0000FF">(Required)</font></strong> <br>
                <input name="organization" type="text" value="" size="56">
                <strong><font face="Arial, Helvetica, sans-serif">Organization</font></strong> 
                <strong><font color="#0000FF">(Required)</font></strong><br>
                <input name="email" type="text" value="" size="56">
                <strong><font face="Arial, Helvetica, sans-serif">eMail </font><font color="#0000FF">(Required)</font></strong><br>
                <input name="phone" type="text" value="" size="56">
                <strong><font face="Arial, Helvetica, sans-serif">Phone </font><font color="#0000FF">(Required)</font></strong><br>
                <input name="fax" type="text" value="" size="56">
                <strong><font face="Arial, Helvetica, sans-serif">Fax</font></strong><br>
                <input name="address" type="text" value="" size="56">
                <strong><font face="Arial, Helvetica, sans-serif">Address </font></strong><br>
                <input name="city" type="text" value="" size="56">
                <strong><font face="Arial, Helvetica, sans-serif">City</font></strong><br>
                <input name="state" type="text" value="" size="56">
                <strong><font face="Arial, Helvetica, sans-serif">State</font></strong><br>
                <input name="zip" type="text" value="" size="56">
                <strong><font face="Arial, Helvetica, sans-serif">Zip</font></strong><br>
                <textarea name="comments" cols="53" rows="5" value=""></textarea>
                <input name="subject" type="hidden" value="Bullzi Support Request">
                <br>
                <font face="Arial, Helvetica, sans-serif">Please submit our support form. The more information you provide<br>
                us the more accurately and rapidly we can diagnose and remedy.<br>
                </font></span><br>
              <a href="javascript&#058;document.supportform.submit()" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('supportsubmit','','images/interface/submitroll.gif',1)"> 
              <img src="images/interface/submit.gif" alt="Submit Form" name="supportsubmit" width="120" height="29" border="0"></a><br>
              <em><strong><font face="Times New Roman, Times, serif"><br>
              Our goal is to reply within 24 hours...<br>
                answer your question...<br>
          solve your problem...<br>
                or simply help as best we can.</font></strong></em><br>
                <br>
                <span class="style1">Attach and send us a file here.</span><br>
                <input name="attachment" type="file"><br>
                </form>

Re: Failing to send all form data.

Posted: Wed Jun 04, 2008 10:24 am
by vargadanis
Have you tried yet, what I wrote about putting the mail together and the attaching the composed mail with a single command instead of using multiple attaches?

[SOLVED] Failing to send all form data.

Posted: Thu Jun 12, 2008 9:56 am
by bullzi
I apologize for not replying sooner, but I didn't realize you had posted again.
I'm new to PHP and Swift Mailer and had difficulty understanding your suggestion, but it did lead me to a solution that may be what you were suggesting. Although I didn't use "part1" etc, I put all of my form data in the $body.

Code: Select all

 
$name = $_POST["name"];
$organization = $_POST["organization"];
$email = $_POST["email"];
$body = "Name: " . $_POST["name"] . "\n";
$body .= "Org: " . $_POST["organization"] . "\n";
$body .= "Email: " . $_POST["email"] . "\n";
$body .= "Phone: " . $_POST["phone"] . "\n";
$body .= "Fax: " . $_POST["fax"] . "\n";
$body .= "Address: " . $_POST["address"] . "\n";
$body .= "City: " . $_POST["city"] . "\n";
$body .= "State: " . $_POST["state"] . "\n";
$body .= "Zip: " . $_POST["zip"] . "\n";
$body .= "User: " . $_SERVER["HTTP_USER_AGENT"] . "\n";
$body .= "Host: " . $_SERVER["REMOTE_HOST"] . "\n";
$body .= "IP: " . $_SERVER["REMOTE_ADDR"] . "\n\n";
$body .= "Message: " . $_POST["comments"] . "\n\nAttachment:\n";
 
And then sent the following:

Code: Select all

 
$message =& new Swift_Message("Support Request from: " . $organization);
$message->attach(new Swift_Message_Part($body));
 
I think this is what you were recommending, but in a different way. Anyway it works and took me many hours to figure it out. The tutorials for sending forms and Swift Message weren't a lot of help. Thanks for your help and again I apologize for my tardiness. I'm going to mark this string [Solved].

I have a new problem now with bogus email from my form that I am going to post separately.

Re: [SOLVED] Failing to send all form data.

Posted: Fri Jun 13, 2008 4:33 am
by vargadanis
I am glad I managed to help u. This is what I thought of. Good job.