I found Sunmailer today, and I fell in love instantly. Now I want to import all my Simplenews subscription (>800 ) into Sunmailer. I can't find a way to import email addresses who do not have account in my drupal website. Is to there a neat way do it?

Only thing that I like about simplenews is subscription management, for every thing else Sunmailer is great! I wish sunmailer will also (1) allow to subscribe non-registered users, and (2) and offer a unique direct link for each user to unsubscribe (without logging into drupal website).

Thanks for great module!

Comments

Peng.Pif’s picture

Hi mclaren,

I agree with you, this is a great module. I would also like to be able to import and subscribe a large volume of email addresses belonging to non-registered users and also make it easier for users in general to unsubscribe from the newsletter.

For a project I am working on that uses the SunMailer module, the scenario is:

Admins of the site, a Recriutment company, currently have a large volume of email addresses that they have collected via telephone, hand written application forms etc. The email addresses supplied to them in this format have been uploaded to a data management system on their local machines. The email addresses can be exported in csv format.

If email addresses are imported and the recipients do not have an account on the website, I think it would be a problem for them to unsubscribe from the mailing list if they decided to. From my understanding, they would first have to create an account on the site and then unsubscribe, which some might find a long winded process. Especially in cases when more than a username and email address is required to register to the site.

If the process to unsubscibe was easier I think it would be beneficial in these instances and a good addition to this great module.

mclaren’s picture

Borrowing heavily from simplenews importer, I did two changes. I added following after line 124 of sunmailer.module to create new menu item "Import emails":

   $items["$base/configure/import"] = array(
    'title' => 'Import emails',
    'description' => 'Import emails in bulk',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('sunmailer_mail_import'),
    'file' => 'sunmailer.admin.inc',
    'access arguments' => array('administer sunmailer'),
    'weight' => 5,
  );

Then, I added following at the end of sunmailer.admin.inc:

function sunmailer_mail_import() {

  $form['emails'] = array(
    '#type' => 'textarea',
    '#title' => t('Email addresses'),
    '#cols' => 60,
    '#rows' => 5,
    '#description' => t('Email addresses must be separated by comma, space or newline.'),
  );
  $sections = array();
  $sections['all'] = t('All sections');
  foreach (sunmailer_get_sections() as $sid => $section) {
    $sections[$sid] = $section['name'];
  }
  if (!count($sections)) {
    drupal_set_message('There are no sections to subscribe.');
    return;
  }
  $formats = sunmailer_get_formats();
  $format_options = array();
  foreach ($formats as $xid => $format) {
    $format_options[$xid] = $format['name'];
  }

  $form['sunmailer_sections'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Subscribe to'),
    '#options' => $sections,
    '#required' => TRUE,
  );

  $form['sunmailer_format'] = array(
    '#type' => 'radios',
    '#title' => t('Format'),
    '#options' => $format_options,
    '#default_value' => variable_get('sunmailer_default_format', SUNMAILER_HTML_FORMAT),
  );
  
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Subscribe'),
  );
  return $form;
}

function sunmailer_mail_import_submit($form, &$form_state) {
	$values = $form_state['values'];
	
	$mails = preg_split("/[\s,]+/", $values['emails']);
  
  $tree = taxonomy_get_tree(variable_get('simplenews_vid', ''));
  $added = array();
  $invalid = array();
  
  foreach ($mails as $mail) {
    $mail = trim($mail);
    if (valid_email_address($mail)) {
        //Prevent mismatches from accidental capitals in mail address
  			$mail = strtolower($mail);

  			// Get current subscriptions if any.
  			$account = (object) array('mail' => $mail);
  			$subscription = sunmailer_get_user_subscription($account);

  			// If user is not subscribed to ANY sections, create a subscription account
  			if ($subscription->snid == 0) {
    			$account = user_load(array('mail' => $mail));
    
    			if (db_result(db_query("SELECT COUNT(uid) FROM {sunmailer_user_format} WHERE uid = %d", $account->uid))) {
      			db_query("UPDATE {sunmailer_user_format} SET xid = %d WHERE uid = %d", $values['sunmailer_format'], $account->uid);
   			 	}
    			else {
      			db_query("INSERT INTO {sunmailer_user_format}(uid, xid) VALUES (%d, %d)", $account->uid, $values['sunmailer_format']);
    			}
    
   				// Update user's sections.
  				$sids = array();
  				$role_sids = sunmailer_get_role_sections($account);
  				if ($values['sunmailer_format']) {
    				if ($values['sunmailer_sections']) {
      				foreach (array_filter($values['sunmailer_sections']) as $sid) {
        			$sids[] = $sid;
      				}
    				}
    				if (!count($sids) && !count($role_sids)) {
      				$sids = array_keys(sunmailer_get_sections());
    				}
  				}
    			db_query("DELETE FROM {sunmailer_user_sections} WHERE uid = %d", $account->uid);
  				if (count($sids)) {
    				$placeholders = array();
    				$args = array();
    				foreach ($sids as $sid) {
      				$placeholders[] = '(%d, %d)';
      				$args[] = $account->uid;
      				$args[] = $sid;
    				}
    			db_query("INSERT INTO {sunmailer_user_sections}(uid, sid) VALUES " . implode(', ', $placeholders), $args);
  			}
  		}
        $added[] = $email;
    }
    else {
      $invalid[] = $email;
    }
  }
  if ($added) {
    $added = implode(", ", $added);
    drupal_set_message(t('The following addresses were added or updated: %added.', array('%added' => $added)));
  }
  else {
    drupal_set_message(t('No addresses were added.'));
  }
  if ($invalid) {
    $invalid = implode(", ", $invalid);
    drupal_set_message(t('The following addresses were invalid: %invalid.', array('%invalid' => $invalid)), 'error');
  }
}

Now, it imports email address, and when I go to user account page at user/*/edit/sunmailer, it shows subscribed. But when I go to Statistics page at admin/settings/sunmailer/stats, it shows following error "Unsupported operand types in sunmailer.mail.inc on line 728". Can somebody help me debug this?

mclaren’s picture

This error only occurs when I choose "All sections" as an option. When I chose a specific section, the error went away! Anyway, somebody with more knowledge of drupal can clean up the code, and commit to CVS. But, statistics page is only showing 364 emails, I don't know why, I checked mysql, sunmailer_user_sections table has 800+ rows.

Now only thing I want is to provide unsubsribe link that does not need login.

Mike Wacker’s picture

There's a module called User Import, and I'm tempted to build a solution off of that. If User Import can assign roles to imported users, one viable option right now is to use role-subscribe (but you'll have to provide an easy way for users to unsubscribe by removing that role).

As for unregistered users and unsubscribing, there was a long discussion of that in #684066: (6.x-1.x) Comparison to Simplenews?. I think the philosophy of having the registration form double up as the subscription form still has some life left in it. The unsubscribe scenario is a high priority for the next release (both emails with an unsubscribe link if individual emails are sent and also a password-less unsubscribe option if bulk emails are sent).

Mike Wacker’s picture

Version: 6.x-1.5 » 6.x-2.x-dev

Some sort of bulk-import option is a possible candidate for inclusion by the beta release. Haven't made a call either way, though.

Mike Wacker’s picture

Status: Active » Fixed

This has been added in the 6.x-2.x branch

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.