Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

All other Modules /

Discuss all other Modules here.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Userforms 0.1 - "From" Field in email empty


Go to End


4 Posts   1751 Views

Avatar
Double-A-Ron

Community Member, 607 Posts

23 June 2009 at 1:42pm

Hi all.

We have Userforms 0.1 installed on 2.3.1 and have a problem where the email comes through, but the "From" field of the email is blank. Both the confirmation to the user and to the person set as "Email submission to:" in the CMS have this problem. And now it seems that some email systems are blocking these emails for lack of a "From" header.

It's strange, I could have sworn that I have another site with the same setup where the "From" address for the user, is the site owners, and the "From" address for the person set as "Email submission to:" is the user who filled out the form (allowing a quick reply).

Using Email::setAdminEmail in _config.php fixes the issue somewhat, but we want our customers to be able to hit reply when they get a contact message from their site.

Any ideas?

Avatar
Willr

Forum Moderator, 5523 Posts

23 June 2009 at 4:28pm

Has your form got a EmailField form field which SS could detect a from address? Has to be a EmailField (not a TextField)

Avatar
Web Designer Perth

Community Member, 49 Posts

23 June 2009 at 5:56pm

Same issue here.

@willr - yes, form has EmailField.

Avatar
Double-A-Ron

Community Member, 607 Posts

24 June 2009 at 9:56am

I am wondering if this is a bug or oversight now. Yes I have an email field, and in the code for userforms/code/UserDefinedForms.php, this is checked and all email addresses on the form are added to an array called $recipientAddresses.

Function Process - line 332

switch($field->ClassName){
					
					case "EditableEmailField" : 
					
						if($field->SendCopy){
							$recipientAddresses[] = $data[$field->Name];
							$sendCopy = true;
							$values[$field->Title] = '<a style="white-space: nowrap" href="mailto:'.$data[$field->Name].'">'.$data[$field->Name].'</a>';
						}
					
					break;

The thing is, nothing is done with this array. The emails that are setup and sent further down the method do not have calls to the setFrom() method at all. Just populateTemplate(), setSubject(), setContent() and setTo().

So I edited my local UserDefinedFrom.php class to do this and it works as desired. My changes are between the MODADC comments:

Line 381

if( $this->EmailOnSubmit ) {
			$email = new UserDefinedForm_SubmittedFormEmail($submittedFields);			
			$email->populateTemplate($emailData);
			$email->setTo( $this->EmailTo );
			$email->setSubject( $this->Title );
			
			// MODADC - This block sets the from field of the email (for site owner) to be that of the user (if one was entered)
			// If the form doesn't ask for an email address, the from field will be set to that of the "Email Submissions to:" address in the CMS
      if(!empty($recipientAddresses)) {
        $email->setFrom( $recipientAddresses[0] );
      } else {
        $email->setFrom( $this->EmailTo );
      }
      // END MODADC

			// add attachments to email (<1MB)
			if($attachments){
				foreach($attachments as $file){
					$email->attachFile($filename,$filename);
				}
			}
			
			$email->send();
		}

Around line 400:

if($sendCopy) {
			// send to each of email fields
			$emailToSubmiter = new UserDefinedForm_SubmittedFormEmailToSubmitter($submittedFields);
			$emailToSubmiter->populateTemplate($emailData);
			$emailToSubmiter->setSubject( $this->Title );
			
			foreach( $recipientAddresses as $addr ) {
				$emailToSubmiter->setBody( $this->EmailMessageToSubmitter );
				$emailToSubmiter->setTo( $addr );
				// MODADC - Set from header
				$emailToSubmiter->setFrom( $this->EmailTo ); 
				// MODADC - Set from header
				$emailToSubmiter->send();
			}
		}

Hope that helps someone. Can anyone confirm a bug at all and is the issue still the same for 0.2?

Cheers
Aaron