Bypassing the Communicate Module: How to Let Your Users Send Attachments in ExpressionEngine

While ExpressionEngine is a great CMS, and it’s quite good at member management, it’s not much of a bulk e-mail system. That’s by design, I think: people don’t (or shouldn’t, anyway) expect their content management system to replace software like CampaignMonitor, MailChip, Constant Contact or PHPlist. That said, one of the things that would be nice to be able to do with ExpressionEngine is to let your users send each other e-mails, with attachments. EE’s Communicate function is easy to use, but you can’t send attachments with it.

I’ve found a relatively simple, hack-free solution to that problem. It uses SolSpace‘s free FreeForm module, one embedded template, and ExpressionEngine’s Query Module.

Click “read more” below to see the rest of this article.

First, you should probably read FreeForm’s documentation, especially the “Form” part. You won’t be using the Count or Entries parts of the module. Then, install FreeForm - it’s pretty easy to install.

Once you have FreeForm installed, you’re going to need to set it up. Click on “Fields” and delete all of the default fields. You’re going to need to add four new fields: sending_name, sending_email, subject, and body. The body field should have the textarea type. Then, create a new template by clicking on “Templates”. (Not the main EE control panel Templates tab - the FreeForm Templates tab.) I named my new template “email”, and put the following values in the fields it asked for: {sending_name} in “From Name”, {sending_email} in “From Email”, {subject} in “Subject” and just {body} in the “Email Message” part. Click “Save”, and you’re done.

Now, on to the templates.

I set up two ExpressionEngine (not FreeForm) templates, and called them main/email and main/email_embed. main/email basically contains just the Query module code you’ll use to get the e-mail addresses you want to send to. The actual form and the opening and closing {exp:freeform:form} tags are in main/email_embed.

main/email - Choose Your E-mail Addresses

{embed="main/email_embed" emails="{exp:query sql="SELECT email FROM exp_members WHERE group_id='2' " backspace="1"}{email}|{/exp:query}"

The SQL in the Query statement returns all email addresses for the members that are in the member group that has an ID of 2. Change and customize that SQL to be anything you want. For example, the SQL to select all of the e-mail addresses in a particular mailing list using EE’s Mailing List module would be something similar to “SELECT email FROM exp_mailing_list WHERE list_id=‘X’”, where X is the list_id of the mailing list you want to send to.

main/email_embed - Build the Form

{if logged_in}
<p>Your e-mail will send as <b>{screen_name}</b>, using the e-mail address <b>{email}</b>.</p>

{exp:freeform:form form_name="email" file_upload ="Main Upload Directory" notify="{embed:emails}" template="email" }

<input type="hidden" name="sending_name" value="{logged_in_screen_name}" />
<
input type="hidden" name="sending_email" value="{logged_in_email}" />

<
p>Subject Line<br />
<
input type="text" name="subject" value="" />
</
p>

<
p>Body of Email<br />
<
textarea name="body" cols="90" rows="15" value=""></textarea>
</
p>

<
p>Upload Files<br />
<
input type="file" name="file1" /><br />
<
input type="file" name="file2" /></p>

<
p>
<
input type="submit" name="submit" value="submit" />
</
p>

{/exp:freeform:form}

{if
:else}

<p>You are not logged inYou need to be logged in to see this page!</p>
{/if} 

Customize the form as much as you want; just make sure that the input names match the names of the fields in the FreeForm control panel that you set up. You may also want to define another Upload Directory from within ExpressionEngine: attachments are uploaded to that directory, and are accessible to the public unless you limit it with a .htaccess file.

That’s about it. .(JavaScript must be enabled to view this email address) if you have any questions, or check out the ExpressionEngine or Solspace forums! The ExpressionEngine forum thread relating specifically to this article is at http://expressionengine.com/forums/viewthread/102374/.


Posted on Jan 14, 2009 - 05:24 PM

Comments:

This is great, but does it work for members who are authorised (or able) to send emails to EE mailing lists? Can the form be set up to mail to a mailing list set up / managed from with EE?

Thanks!

By .(JavaScript must be enabled to view this email address) on Feb 11, 2009 - 11:34 PM


Comments:

Hi Nik,

This can be used to send emails to EE mailing lists. Your main template would look something like:

{embed=“main/email_embed” emails=”{exp:query sql=“SELECT email FROM exp_mailing_list WHERE list_id=‘X’ ” backspace=“1”}{email}|{/exp:query}”}

where X is the ID of your mailing list. So, your users would sign up for the mailing list like they normally would, however that is, but you would send to the mailing list (with attachments) using the Freeform module and the template code I posted above.

Let me know if you have any questions!

By .(JavaScript must be enabled to view this email address) on Feb 12, 2009 - 12:21 AM


Comments:

Hi Luke,

Interesting solution. I’m wondering how the opt out part would work? Can you still add the opt out link?

Thanks!

By .(JavaScript must be enabled to view this email address) on Feb 17, 2009 - 04:20 PM


Comments:

Hm, hadn’t thought about opting out. I originally made this to send attachments to a group of users who had already expressed consent, and wouldn’t have any reason to unsubscribe.

One way would be to add text in your Freeform template asking users to go to a page on your site to unsubscribe. You’d have to build that template, of course, but it would be easier since you could use all of the member variables.

By .(JavaScript must be enabled to view this email address) on Feb 17, 2009 - 05:08 PM


Comments:

Can the recipient email addresses be those of non-members? We’re building a site for prospects who be managed as entries, not members, and site admins who are logged-in members need to be able to send them attachments. Also, does the module log all messages sent?

By Mark J. Reeves on Jul 09, 2009 - 03:39 PM


Comments:

You would be able to send e-mails with attachments to non-members using this Freeform technique. You would have to change the SQL query, though. Assuming that you’re storing the e-mail address in a custom field in a weblog entry, your SQL would look something like this:

{exp:query sql="SELECT field_id_15 FROM exp_weblog_data WHERE weblog_id='2'" backspace="1"}{email}|{/exp:query}"} 

This is assuming that the e-mail addresses are in the custom field with the id of 15, and that the weblog_id is 2. (You’ll have to check the database structure to be sure.)

You might also want to beef up the “WHERE” statement: make sure it’s not blank, etc.

All of the e-mails sent will show up as entries in the Freeform module in the EE Control Panel.

By .(JavaScript must be enabled to view this email address) on Jul 09, 2009 - 03:56 PM


Leave A Comment!!

Please enter the letters or word you see in the image below:

Name:

Email:

Location:

URL:

Remember this information for next time

E-mail me about follow-up comments