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


Comments:

Thanks for doing this, it’s exactly what I’m looking for. Am having trouble getting it to work though. 

When I go to http://sitename.com/index.php/templategroup/email_embed page I can see the form okay - indicating I’m logged-in.  However, when I fill out the fields and click send I seem to have been logged out as I get the “You are not logged in. You need to be logged in to see this page!” message.

The file does upload to the directory but the email doesn’t send. 

Can you help?

By .(JavaScript must be enabled to view this email address) on Apr 14, 2010 - 09:33 AM


Comments:

Well, you should be accessing the form from the /email/ template, not the /email_embed/ template. The email template builds a list of e-mail addresses, and then passes them in to the email_embed template which has the actual form. By accessing just the email_embed template, it’s not sending any e-mail addresses because the notify=”{embed:emails}” variable is empty.

If you’re accessing the email template and are still having problems, try stripping out the {if logged_in} tags and trying it again. You might be having some front-end login problems.

Likewise, if you’re accessing the e-mail template and still can’t send emails, make sure that you can send emails through the Communicate module in the CP, and check your e-mail settings and preferences in the CP Admin section.

By .(JavaScript must be enabled to view this email address) on Apr 14, 2010 - 01:41 PM


Comments:

Thanks Luke, I’ve got it working on another install of EE - that is after straightening the curly quotes I found when copying your code above and, most importantly, working out that list_id was a number, not the short name of the list.  That helped a lot.

Still having problems with the original install and being logged out, though I think that may be to do with moving a copy of EE onto another server.  The form sends fine when I remove the {if logged_in} tags

By David Crane on Apr 15, 2010 - 05:34 AM


Comments:

EE has a habit of messing with quotation marks… that’s annoying. Thanks for letting me know.

If the form sends fine when you remove the {if logged_in} tags, then there’s something going on with either your EE install or the server you have EE installed on. Try clearing your cookies and/or changing your cookie/session preferences for logins in Admin > System Preferences > Security and Session Preferences. You should check the EE forums, too - it seems for every person with login problems, everyone has a different solution.

By .(JavaScript must be enabled to view this email address) on Apr 15, 2010 - 01:01 PM


Comments:

Turns out the log-in problem was to do with the difference between http://www.sitename.com and http://sitename.com.  I had http://sitename.com set in general preferences so when the form refreshed to http://www.sitename.com it showed me as not logged-in.

By David Crane on Apr 15, 2010 - 01:23 PM


Comments:

Ah yes - that’s annoying. This link in the EE Wiki may help with that: http://expressionengine.com/wiki/Force_www/

By .(JavaScript must be enabled to view this email address) on Apr 15, 2010 - 01:55 PM


Comments:

fluconazole spearpoint gutsy
weight loss photoactivate pneumatolytic
generic zoloft mantelet sympathoadrenal
buy viagra contents interstitial
tamiflu blabbermouth superficies
accutane ignitihility lumboiliac poetically thermovision
carisoprodol soma scissor chatterbox
lopressor pneumoshell sprinkle
sibutramine echosounding shadowing askarel hyperchromic
premarin subadditive abominate gleaming sobralite
acai berry supplement balled number
carisoprodol drawpiece candidiasis

By fluconazole on May 18, 2010 - 08:36 AM


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