[support] loops?

Larry Garfield larry at garfieldtech.com
Sat Dec 2 20:08:39 UTC 2006


Also have a look at the Send module, I think it's called.  It's a more robust 
emailing system that a lot of other stuff is built off of.  I've not used it, 
but it could be useful.

On Saturday 02 December 2006 07:40, MichelleX wrote:
> Hi Larry - Thanks for the input. I think I got ya.
> I'll certainly look into Webform - anything other than this method is
> more than welcome, as long as it can do file attachments to the email.
>
> Thanks again!
> Michelle
>
> Larry Garfield wrote:
> > First of all, the data structure for the select is not a simple array. 
> > It's an associative array.  The key is the value that gets submitted by
> > the form while the value for each key is the label to display.  The
> > actual renderer for the form does something like this:
> >
> > foreach ($options as $key => $value) {
> >   print "<option value='$key'>$value</option>\m";
> > }
> >
> > (That's NOT the actual Drupal code, but that's the sort of logic it
> > does.)
> >
> > So if you want to have a string value submitted, you'd need an option
> > array like this:
> >
> >  $experience = array(
> > t('Amateur/ Hobbyist') => t('Amateur/ Hobbyist'),
> > t('Experienced Hobbyist') => t('Experienced Hobbyist'),
> > t('Semi Professional') => t('Semi Professional'),
> > t('Professional') => t('Professional'),
> > );
> >
> > Note that the "select one" option is not necessary; Drupal will add it
> > for you if appropriate.
> >
> > Secondly, it looks like you're doing things the very hard way. :-)  Check
> > out the webforms module, or similar; it can probably build you a form
> > that will do what you need without you writing any actual code, and
> > certainly not messing around with mail headers.  Alternatively, you can
> > have people submit a CCK-created node with image fields and use the
> > actions module to send you a notice email to go view the node on the
> > site.  That way you even get a nice permanent record out of it.
> >
> > When in doubt, make Drupal do it for you. :-)
> >
> > Cheers.
> >
> > On Friday 01 December 2006 18:59, MichelleX wrote:
> >> I apologize if this isn't the right place to post this.. but I was
> >> hoping someone could help me out with a foreach loop.
> >>
> >> I'm using the code from here http://drupal.org/node/68265 for a custom
> >> form in a page.
> >> I've modified it to display select menus - and they work just great
> >> except I can't get it to send anything other than a number. I've been
> >> all through the api stuff and have tried various methods for writing a
> >> foreach loop for the select menus, but everything seems to conflict with
> >> common.inc.
> >> Here's my modified code (hopefully this will display correctly):
> >>
> >> <?php
> >>
> >> $form['name'] = array(
> >>     '#type' => 'textfield',
> >>     '#title' => t('Username'),
> >>     '#default_value' => $object['name'],
> >>     '#size' => 30,
> >>     '#maxlength' => 128,
> >>     '#required' => TRUE,
> >> );
> >>
> >> $form['eMail'] = array(
> >>     '#type' => 'textfield',
> >>     '#title' => t('E-mail Address'),
> >>     '#default_value' => $object['eMail'],
> >>     '#size' => 30,
> >>     '#maxlength' => 128,
> >>     '#required' => TRUE,
> >> );
> >>
> >> //account option
> >>  $account = array(
> >> '--'.t('Select').'--',
> >> t('Model'),
> >> t('Photographer'),
> >> t('Crew'),
> >> );
> >>
> >> $form['assign'][$i] = array(
> >>     '#type' => 'select',
> >>     '#title' => t('Account purpose'),
> >>     '#default_value' => $object['assign'][$i],
> >>     '#options' => $account,
> >>     '#required' => TRUE,
> >>     '#description' => t('Choose which account your\'re applying for.'),
> >> );
> >>
> >>
> >> $form['location'] = array(
> >>     '#type' => 'textfield',
> >>     '#title' => t('Location'),
> >>     '#default_value' => $object['location'],
> >>     '#size' => 30,
> >>     '#maxlength' => 128,
> >>     '#required' => TRUE,
> >> '#description' => t('Enter your city, state and country'),
> >> );
> >>
> >> $form['age'] = array(
> >>     '#type' => 'textfield',
> >>     '#title' => t('Age'),
> >>     '#default_value' => $object['age'],
> >>     '#size' => 15,
> >>     '#maxlength' => 128,
> >>     '#required' => TRUE,
> >> '#description' => t('Enter your age'),
> >> );
> >>
> >> //experience options
> >>  $experience = array(
> >> '--'.t('Select').'--',
> >> t('Amateur/ Hobbyist'),
> >> t('Experienced Hobbyist'),
> >> t('Semi Professional'),
> >> t('Professional'),
> >> );
> >>
> >> $form['exlevel'] = array(
> >>     '#type' => 'select',
> >>     '#title' => t('Experience Level'),
> >>     '#default_value' => $object['exlevel'],
> >>     '#options' => $experience,
> >> '#required' => TRUE,
> >> '#description' => t('Enter your experience level.'),
> >> );
> >>
> >> //travel options
> >>  $travel = array(
> >> '--'.t('Select').'--',
> >> t('Yes'),
> >> t('w/ expenses paid'),
> >> t('w/o expenses paid'),
> >> t('Only in my state'),
> >> t('Only in my city'),
> >> t('Depends on assignment'),
> >> t('No'),
> >> );
> >>
> >> $form['willtravel'] = array(
> >>     '#type' => 'select',
> >>     '#title' => t('Will travel'),
> >>     '#default_value' => $object['willtravel'],
> >>     '#options' => $travel,
> >> '#required' => TRUE,
> >> '#description' => t('Are you willing to travel?'),
> >> );
> >>
> >> $form['nudity'] = array(
> >>     '#type' => 'textfield',
> >>     '#title' => t('Nudity Level'),
> >>     '#default_value' => $object['nudity'],
> >>     '#size' => 30,
> >>     '#maxlength' => 128,
> >>     '#required' => TRUE,
> >> '#description' => t('Enter your level of experience or willingness for
> >> nude assignments.'),
> >> );
> >> $form['compensation'] = array(
> >>     '#type' => 'textfield',
> >>     '#title' => t('Compensation'),
> >>     '#default_value' => $object['compensation'],
> >>     '#size' => 30,
> >>     '#maxlength' => 128,
> >>     '#required' => TRUE,
> >> '#description' => t('Example: DOA (Depends on Assignment), TFP, TFCD,
> >> Paid only, etc.'),
> >> );
> >> $form['website'] = array(
> >>     '#type' => 'textfield',
> >>     '#title' => t('Web Site'),
> >>     '#default_value' => $object['website'],
> >>     '#size' => 30,
> >>     '#maxlength' => 128,
> >>     '#required' => FALSE,
> >> '#description' => t('If you have a personal web site, enter it here.'),
> >> );
> >> $form['message'] = array(
> >>     '#type' => 'textarea',
> >>     '#title' => t('additional information'),
> >>     '#default_value' => $object['message'],
> >>     '#size' => 30,
> >>     '#maxlength' => 128,
> >>     '#rows'    => 7,
> >>     '#required' => FALSE,
> >> );
> >>
> >> $form['file1'] = array(
> >>     '#type' => 'file',
> >>       '#title' => t('Four images are required for review.'),
> >> );
> >> $form['file2'] = array(
> >>     '#type' => 'file',
> >> );
> >> $form['file3'] = array(
> >>     '#type' => 'file',
> >> );
> >> $form['file4'] = array(
> >>     '#type' => 'file',
> >> );
> >>
> >> $form['submit'] = array(
> >>     '#type' => 'submit',
> >>     '#value' => t('send email'),
> >> );
> >>
> >> $form['#attributes']['enctype'] = 'multipart/form-data';
> >>
> >> $output = drupal_get_form('contactform', $form);
> >> return $output;
> >>
> >> // validation function for the contact form
> >> function contactform_validate($form_id, $form_values) {
> >>     // first we validate if there is a email injection
> >>     $finds = array("/bcc:/i",
> >>             "/Content-Type:/i",
> >>             "/Mime-Type:/i",
> >>             "/MIME-Version:/i",
> >>             "/multipart\/mixed/i",
> >>             "/boundary=/i",
> >>             "/subject:/i",
> >>             "/cc:/i",
> >>             "/to:/i");
> >>     foreach($form_values as $value)
> >>           foreach($finds as $find)
> >>                 if(preg_match($find,$value))
> >>                     form_set_error('', '<h2 class="red center">Stop
> >> spamming</h2>');
> >>
> >>     // then we validate the email-adress
> >>     if (!valid_email_address($form_values['eMail']) &&
> >> !empty($form_values['eMail']))
> >>         form_set_error('', t('Please check the spelling of your
> >> email-adress.'));
> >> }
> >>
> >>
> >> // submit function for the contact form
> >> function contactform_submit($form_id, $form_values) {
> >>
> >>     $from         = $form_values['name'].' <'.$form_values['eMail'].'>';
> >>     $recipient    = 'MX <michelle at steelveils.com>';
> >>     $subject    = 'Application';
> >>     $account      = $form_values['assign'];
> >>     $location      = $form_values['location'];
> >>     $age      = $form_values['age'];
> >>     $experience      = $form_values['exlevel'];
> >>     $travel      = $form_values['willtravel'];
> >>     $nudity      = $form_values['nudity'];
> >>     $compensation      = $form_values['compensation'];
> >>     $website      = $form_values['website'];
> >>     $body         = wordwrap($form_values['message']);
> >>     $reply        = 'Thank you for your application. Your application
> >> for an account is currently pending approval. Once it has been granted,
> >> you will receive further instructions in eMail.';
> >>     $goto        = 'app-rec';
> >>
> >>     if (!flood_is_allowed('contact',
> >> variable_get('contact_hourly_threshold', 8))) {
> >>         $output = t("You cannot send more than %number messages per
> >> hour. Please try again later.", array('%number' =>
> >> variable_get('contact_hourly_threshold', 3)));
> >>         drupal_set_message('<h3 class="red center">'.$output.'</h3>');
> >>         drupal_goto($goto);
> >>     }else{
> >>         $attachment     = FALSE;
> >>         $trenner     = md5(uniqid(time()));
> >>         $headers    .= "MIME-Version: 1.0\n";
> >>         $headers     .= "From: $from\nReply-to: $from\nReturn-path:
> >> $from\nErrors-to: $from\nX-Mailer: Drupal\n";
> >>         $headers     .= "Content-Type:
> >> multipart/mixed;\n\tboundary=$trenner\n";
> >>         $message     .= "\n--$trenner\n";
> >>         $message     .= "Content-Type: text/plain;
> >> charset=UTF-8;"."\n\n"; // sets the mime type
> >>         $message     .= "Account type: " .$account. "\n";
> >>         $message     .= "Location: " .$location."\n";
> >>         $message     .= "Age: " .$age."\n";
> >>         $message     .= "Experience Level: " .$experience."\n";
> >>         $message     .= "Will Travel: " .$travel."\n";
> >>         $message     .= "Nudity Level: " .$nudity."\n";
> >>         $message     .= "Compensation: " .$compensation."\n";
> >>         $message     .= "Web Site: " .$website."\n";
> >>         $message     .= $body. "\n";
> >>         $message     .= "\n\n";
> >>
> >>
> >>              for($i=1;$i<=4;$i++){
> >>             $file = file_check_upload('file'.$i);
> >>             if($file->filename){
> >>                 $file->filepath =
> >> str_replace("\\","\\\\",$file->filepath); $message    .=
> >> "--$trenner"."\n";
> >>                 $message    .=
> >> "Content-Type:$file->filemime;\n\tname=$file->filename\n";
> >>                 $message    .= "Content-Transfer-Encoding: base64\n";
> >>                 $message    .= "Content-Disposition:
> >> attachment;\n\tfilename=$file->filename\n\n";
> >>                 $filedata    = fread(fopen($file->filepath, "rb"),
> >> $file->filesize);
> >>                 $message    .= chunk_split(base64_encode($filedata));
> >>                 $message    .= "\n\n";
> >>                 $attachment    = TRUE;
> >>             }
> >>         }
> >>         $message .= "--$trenner--";
> >>
> >>         // send Mail
> >>         if($attachment) // use the php mail function if we have
> >> attachments mail($recipient, $subject, $message, $headers);
> >>         else
> >>             user_mail($recipient, $subject, $account, $location, $age,
> >> $experience, $travel, $nudity, $compensation, $website, $body, "From:
> >> $from\nReply-to: $from\nX-Mailer: Drupal\nReturn-path: $from\nErrors-to:
> >> $from");
> >>
> >>         // Reply
> >>         user_mail($from, $subject, wordwrap($reply), "From:
> >> $recipient\nReply-to: $recipient\nX-Mailer: Drupal\nReturn-path:
> >> $recipient\nErrors-to: $recipient");
> >>
> >>         // Log the operation:
> >>         flood_register_event('contact');
> >>         watchdog('mail', t('%name-from use contact form',
> >> array('%name-from' => theme('placeholder', $form_values['name'] ."
> >> <$from>"),)));
> >>
> >>         drupal_set_message('Your message has been sent to us');
> >>         drupal_goto($goto);
> >>         }
> >> }
> >>
> >> ?>
> >>
> >> Thanks!
> >> Michelle
> >>
> >>
> >>
> >> --
> >> Steel Veils
> >> Photography. Digital Design. Custom Art.
> >> ________________________________________________________
> >>
> >> www.SteelVeils.com | michelle at SteelVeils.com | 314.283.1081
>
> --
> Steel Veils
> Photography. Digital Design. Custom Art.
> ________________________________________________________
>
> www.SteelVeils.com | michelle at SteelVeils.com | 314.283.1081

-- 
Larry Garfield			AIM: LOLG42
larry at garfieldtech.com		ICQ: 6817012

"If nature has made any one thing less susceptible than all others of 
exclusive property, it is the action of the thinking power called an idea, 
which an individual may exclusively possess as long as he keeps it to 
himself; but the moment it is divulged, it forces itself into the possession 
of every one, and the receiver cannot dispossess himself of it."  -- Thomas 
Jefferson


More information about the support mailing list