Page 1 of 1

Swiftmailer + Attached images + multiple HTML forms.

Posted: Mon Mar 31, 2008 6:49 pm
by damainman
Hello and greetings to all!!!

I'm not sure what version of Swift I am using as it was almost a year ago or possibly slightly more when I downloaded it and used it for one of my clients websites. I created a form processor to collect data from an html based application form to process with Swift Mailer. (Awesome product by the way)

On the form, I allowed the user to upload 3 images. These images were then renamed from the original file images to the users first name and last name, to help organize images from applicants. However I'm creating a completely different website which I plan on using the same functionality.

I'm fairly new to PHP and I've seen this done with other scripts, but I was curious how would I go about setting up Swiftmailer so I don't have to each individual form field. The reason for this is I plan to use multiple forms, and would like to not have to customize a form processor for each form.

I want where, no matter what fields my form has... Swift Mailer collects data from all the fields that had data... and organizes them in an email. Without me having to declare each field in the processor manually. Godaddy has a form processor with similar functionality. But I'm not on godaddy lol.

Here is the modified version of my processor:

Code: Select all

<?php
 
require('Swift.php');
require('Swift/Connection/SMTP.php');
$mailer = new Swift(new Swift_Connection_SMTP('relay-hosting.secureserver.net'));
 
if (($_FILES["photoupload"]["type"] == "image/gif")
|| ($_FILES["photoupload"]["type"] == "image/jpeg")
|| ($_FILES["photoupload"]["type"] == "image/pjpeg")
|| ($_FILES["photoupload"]["type"] == "'image/bmp")
|| ($_FILES["photoupload"]["type"] == "image/x-png")
&& ($_FILES["photoupload"]["size"] < 800000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Error: " . $_FILES["photoupload"]["error"] . "<br />";
    }
 
  else
    {
 
$file = $_FILES["photoupload"]["tmp_name"];
 
$known_photo_types = array(  
    'image/pjpeg' => 'jpg',
    'image/jpeg' => 'jpg',
    'image/gif' => 'gif',
    'image/bmp' => 'bmp',
    'image/x-png' => 'png'
     );
     
//Image Rename Variables
$filetype = $_FILES["photoupload"]["type"];  
$extension = $known_photo_types[$filetype];
$newfilename_firstname = $_POST["First_Name"];
$newfilename_lastname = $_POST["Surname"];
$newfilename_full = "$newfilename_lastname"."-"."$newfilename_firstname".".$extension";
$nophoto = "$_POST[photoupload]".".$extension";
 
//Application form Variables
$Appresults_Contact_method1 = $_POST[Contact_method1].", ";
$Appresults_Contact_method2 = $_POST[Contact_method2].", ";
$Appresults_Contact_method3 = $_POST[Contact_method3].", ";
$Appresults_Contact_method4 = $_POST[Contact_method4].", ";
$Appresults_Contact_method5 = $_POST[Contact_method5].", ";
$Appresults_First_Name = $_POST[First_Name];
$Appresults_Surname = $_POST[Surname];
$Appresults_Nickname = $_POST[Nickname];
$Appresults_Address = $_POST[Address];
$Appresults_City_Town = $_POST[City_Town];
$Appresults_State = $_POST[State];
$Appresults_Zipcode = $_POST[Zipcode];
$Appresults_Email = $_POST[Email];
$Appresults_Contact_Num = $_POST[Contact_Num];
$Appresults_Best_Time = $_POST[Best_Time];
$Appresults_Citizen = $_POST[Citizen];
$Appresults_Legal_age = $_POST[Legal_age];
$Appresults_Birth_month = $_POST[Birth_month];
$Appresults_Birth_day = $_POST[Birth_day];
$Appresults_Birth_year = $_POST[Birth_year];
$Appresults_Age = $_POST[Age];
$Appresults_Height_personal = $_POST[Height_personal];
$Appresults_Weight_personal = $_POST[Weight_personal];
$Appresults_Hair_personal = $_POST[Hair_personal];
$Appresults_Appearance_personal = $_POST[Appearance_personal];
$Appresults_Dress_personal = $_POST[Dress_personal];
$Appresults_Physical_traits = $_POST[Physical_traits];
$Appresults_Employment1 = $_POST[Employment1].", ";
$Appresults_Employment2 = $_POST[Employment2].", ";
$Appresults_Employment3 = $_POST[Employment3].", ";
$Appresults_Employment4 = $_POST[Employment4].", ";
$Appresults_Employment5 = $_POST[Employment5].", ";
$Appresults_Occupation = $_POST[Occupation];
$Appresults_Employer = $_POST[Employer];
$Appresults_Recentjob_one = $_POST[Recentjob_one];
$Appresults_Recentjob_two = $_POST[Recentjob_two];
$Appresults_Recentjob_three = $_POST[Recentjob_three];
$Appresults_Education_level = $_POST[Education_level];
$Appresults_Education_degrees = $_POST[Education_degrees];
$Appresults_Education_schools = $_POST[Education_schools];
$Appresults_Arrested = $_POST[Arrested];
$Appresults_Arrest_details = $_POST[Arrest_details];
$Appresults_Restrainingorder = $_POST[Restrainingorder];
$Appresults_Restrainingorder_detail = $_POST[Restrainingorder_detail];
$Appresults_Modeled = $_POST[Modeled];
$Appresults_Modeled_details = $_POST[Modeled_details];
$Appresults_ProPerformer = $_POST[ProPerformer];
$Appresults_ProPerformer_details = $_POST[ProPerformer_details];
$Appresults_Union = $_POST[Union];
$Appresults_Union_details = $_POST[Union_details];
$Appresults_Married = $_POST[Married];
$Appresults_Married_details = $_POST[Married_details];
$Appresults_Divorced = $_POST[Divorced];
$Appresults_Divorced_details = $_POST[Divorced_details];
$Appresults_Kids = $_POST[Kids];
$Appresults_Kids_details = $_POST[Kids_details];
$Appresults_Smoker = $_POST[Smoker];
$Appresults_Alcohol = $_POST[Alcohol];
$Appresults_Drugs = $_POST[Drugs];
$Appresults_Own_car = $_POST[Own_car];
$Appresults_Live_alone = $_POST[Live_alone];
$Appresults_Languages = $_POST[Languages];
$Appresults_Personality1 = $_POST[Personality1].", ";
$Appresults_Personality2 = $_POST[Personality2].", ";
$Appresults_Personality3 = $_POST[Personality3].", ";
$Appresults_Personality4 = $_POST[Personality4].", ";
$Appresults_Personality5 = $_POST[Personality5].", ";
$Appresults_Personality6 = $_POST[Personality6].", ";
$Appresults_Personality7 = $_POST[Personality7].", ";
$Appresults_Personality8 = $_POST[Personality8].", ";
$Appresults_Personality9 = $_POST[Personality9].", ";
$Appresults_Personality10 = $_POST[Personality10].", ";
$Appresults_Personality11 = $_POST[Personality11].", ";
$Appresults_Personality12 = $_POST[Personality12].", ";
$Appresults_Personality13 = $_POST[Personality13].", ";
$Appresults_Characteristics = $_POST[Characteristics];
$Appresults_Hobbies = $_POST[Hobbies];
$Appresults_Proud_Talents = $_POST[Proud_Talents];
$Appresults_Proud_accomplishments = $_POST[Proud_accomplishments];
$Appresults_Meet_minage = $_POST[Meet_minage];
$Appresults_Meet_maxage = $_POST[Meet_maxage];
$Appresults_Meet_minheight = $_POST[Meet_minheight];
$Appresults_Meet_maxheight = $_POST[Meet_maxheight];
$Appresults_Meet_Equalities = $_POST[Meet_Equalities];
$Appresults_Meet_Dqualities = $_POST[Meet_Dqualities];
$Appresults_Additional_comments = $_POST[Additional_comments];
$Appresults_Referredby = $_POST[Referredby];
 
    //If anything goes wrong you can see what happened in the logs
     if ($mailer->isConnected())
     {
    //Add as many parts as you need here
        
$plain_part = <<<EOD
Contact Information:
- Firstname: $Appresults_First_Name
- Surname: $Appresults_Surname
- Nickname: $Appresults_Nickname
- Contact Method: $Appresults_Contact_method1 $Appresults_Contact_method2 $Appresults_Contact_method3 $Appresults_Contact_method4 $Appresults_Contact_method5 
- Address: $Appresults_Address , $Appresults_City_Town , $Appresults_State $Appresults_Zipcode
- Email Address: $Appresults_Email
- Contact Numbers: $Appresults_Contact_Num
- Best Time to phone: $Appresults_Best_Time
 
Personal Details:
- U.S. Citizen: $Appresults_Citizen
- 18 years of age or older: $Appresults_Legal_age
- Birthday: $Appresults_Birth_month / $Appresults_Birth_day / $Appresults_Birth_year
- Age: $Appresults_Age
- Height: $Appresults_Height_personal ft.
- Weight: $Appresults_Weight_personal lb.
- Hair Color: $Appresults_Hair_personal
- Appearance: $Appresults_Appearance_personal
- Dress: $Appresults_Dress_personal
- Ever married: $Appresults_Married
- Times married if yes: $Appresults_Married_details
- Ever divorced: $Appresults_Divorced
- divorced details if yes: $Appresults_Divorced_details
- Kids: $Appresults_Kids
- How many kids and how old: $Appresults_Kids_details
 
Employment Details:
- Is currently: $Appresults_Employment1 $Appresults_Employment2 $Appresults_Employment3 $Appresults_Employment4 $Appresults_Employment5 
- Occupation: $Appresults_Occupation
- Employer: $Appresults_Employer
- Recent 3 jobs:
1. $Appresults_Recentjob_one
2. $Appresults_Recentjob_two
3. $Appresults_Recentjob_three
 
Education Details:
- Education level: $Appresults_Education_level
- Degree(s): $Appresults_Education_degrees
- School(s) Attended: $Appresults_Education_schools
 
Personality/Characteristic/Interests Details:
- Personality type: $Appresults_Personality1 $Appresults_Personality2 $Appresults_Personality3 $Appresults_Personality4 $Appresults_Personality5 $Appresults_Personality6 $Appresults_Personality7 $Appresults_Personality8 $Appresults_Personality9 $Appresults_Personality10 $Appresults_Personality11 $Appresults_Personality12 $Appresults_Personality13 
- Characteristics worth noting: $Appresults_Characteristics
- Languages spoken: $Appresults_Languages
- Own car: $Appresults_Own_car
- Live alone: $Appresults_Live_alone
- Smoker: $Appresults_Smoker
- Drinker: $Appresults_Alcohol
- Recreational drug user: $Appresults_Drugs
- Interests and hobbies: $Appresults_Hobbies
 
Misc Information:
- Ever been Arrested: $Appresults_Arrested 
- Arrest Details if Yes: $Appresults_Arrest_details 
 
- Ever had a restraining order: $Appresults_Restrainingorder 
- Restraining order details if yes: $Appresults_Restrainingorder_detail 
 
- Ever modeled: $Appresults_Modeled 
- Modeling details if yes: $Appresults_Modeled_details 
 
- Pro Performer: $Appresults_ProPerformer 
- Pro Performer details: $Appresults_ProPerformer_details 
 
- Union: $Appresults_Union 
- Union details: $Appresults_Union_details 
 
Proud and Best Traits/Accomplishments/Talents:
- Best physical traits: $Appresults_Physical_traits
- Accomplishments proud of: $Appresults_Proud_accomplishments
- Talents proud of: $Appresults_Proud_Talents
 
--------
 
Type of person I wish to meet:
- Min age: $Appresults_Meet_minage
- Max age: $Appresults_Meet_maxage
- Min Height: $Appresults_Meet_minheight ft.
- Max Height: $Appresults_Meet_maxheight ft.
- Essential Qualities: $Appresults_Meet_Equalities
- Desirable Qualities: $Appresults_Meet_Dqualities
 
--------
 
How did you hear about us: $Appresults_Referredby
Additional comments: $Appresults_Additional_comments
 
 
EOD;
 
$mailer->addPart($plain_part);      
        
            
    //Add attachments as raw strings
if ((empty($_POST["First_Name"]) == false) && (empty($_POST["Surname"]) == false))
    $mailer->addAttachment(file_get_contents($file),$newfilename_full, $_FILES["photoupload"]["type"]);
else
    $mailer->addAttachment(file_get_contents($file),$nophoto, $_FILES["photoupload"]["type"]);
    
 
    //Leaving the body out of send() makes the mailer send a multipart message
    $mailer->send('info@clientside.com','info@clientside','Member Application');
    $mailer->close();
    header('Location: http://www.clientsite.com/thankyou.html'); 
     }
    else 
    {
    echo "The mailer failed to connect. Errors: <pre>".print_r($mailer->errors, 1)."</pre><br />
    Log: <pre>".print_r($mailer->transactions, 1)."</pre>";
    }
 
    
    }
  }
else
 {
 include('noattachment.php');
 header('Location: http://www.clientsite.com/thankyou.html');
 }
?>
I'm willing to read and research if anyone can link me someplace that might have information that will assist me. But thank everyone in advance for any comments, replies, links, and assistance :)

Update:
I just found out what version of Swift I'm using :). Haven't tried using the new version with my form processor though.

- 2.1.17-php4

Re: Swiftmailer + Attached images + multiple HTML forms.

Posted: Tue Apr 01, 2008 3:55 am
by Chris Corbyn
With your new project I'd suggest downloading the newer version and re-reading the docs since you'll probably want to refresh your memory anyway.

Looking at your email it looks as if you format it and lay it all out. There's not really a way to do that without changing the processing code each time the fields change. GoDaddy's form2mail processor probably just outputs something like this:

Code: Select all

Username: someone
DoB: 3 May, 1983
E-Mail: person@place.tld
Favorite Color: Red
If that's all you need then you can just loop over the $_POST array outputting "key : value":

Code: Select all

$message = '';
foreach ($_POST as $key => $value) {
  $message .= $key . ':' . $value . "\n";
}
EDIT | It's a good job you're not using GoDaddy since they're anal about people using things like Swift or PHPMailer because they connect to external servers.

Re: Swiftmailer + Attached images + multiple HTML forms.

Posted: Tue Apr 01, 2008 4:26 am
by damainman
Wow, thank you very much for your quick reply. I'll let you know how it goes. :)

Re: Swiftmailer + Attached images + multiple HTML forms.

Posted: Fri Apr 04, 2008 10:16 am
by damainman
Chris Corbyn wrote:
If that's all you need then you can just loop over the $_POST array outputting "key : value":

Code: Select all

$message = '';
foreach ($_POST as $key => $value) {
  $message .= $key . ':' . $value . "\n";
}
I downloaded the new swift, but unable to understand exactly to add the body of the email, especially to pick up the multiple fields in the form.

How would I use your example above using the following:

Code: Select all

//Create the message to send
$message =& new Swift_Message("New comment: " . $title);
$message->attach(new Swift_Message_Part($body));

Re: Swiftmailer + Attached images + multiple HTML forms.

Posted: Fri Apr 04, 2008 7:26 pm
by Chris Corbyn
Replace $message above with $body so that you're building a string with the variable $body, the attaching that body as a message part.

Code: Select all

$body = '';
foreach ($_POST as $key => $value) {
  $body .= $key . ':' . $value . "\n";
}
 
$message->attach(new Swift_Message_Part($body));

Re: Swiftmailer + Attached images + multiple HTML forms.

Posted: Sat Apr 05, 2008 3:28 am
by damainman
lol i figured it out on my own and was about to post the solution here for other people. It's the same exact thing you did though.

Thank you very much!!!!