I want to send out a newsletter using Simplenews but need to do some PHP processing to create specific content for the User to whom, THE MAIL IS BEING SENT. i.e. each mail will be silightly different.

If I use

global $user;
$user-uid;

I only get the userID of the Cron-Job sendind ing the Mail or My-UserID when I send the Newsletter manulally.

Does anyone have a snippet or suggestion of how I can obtain the UserID of the person to whom the mail is being sent?

Thanks

Andy Potter

Comments

ap’s picture

As far as can tell it looks like what I need is not possible.

The current logic of Simplenews is such that the Newsletter is prepared as if it were a node. This includes any PHP/HTML
filters etc. During this phase the ID of users who will recieve the Newletter is unknown.

After the Newsletter-preparation the subscription list is searched to see who should receive the letter. At this point one could insert special recipient-related content but it is to late. Any PHP code which was part of the Node has now been evaluated.

Well. I'll have to do what I want some other way.

porelrio’s picture

Haven't looked at simplenews, yet. But planning on it in the near future.

If it calls the node during each emailing, you might be able to add a query to the .tpl

Something like:

$email = [next email address to be sent];
$user_info = db_query("SELECT uid, name FROM users WHERE mail LIKE '" . $email . "';");
print ("Hello, " . $user_info[1]);

I'll actually look at it later this week.

ap’s picture

AFAIK this routine builds the node

/**
* Prepare node for sending
*/
function simplenews_node_prepare($nid, $tid) {
  $node = node_load(array('nid' => $nid), NULL, TRUE);
  $node = simplenews_replace_vars($node, FALSE);

  // To play well with other modules that add content to the node,
  // simplenews_node_prepare() must mimic node_view() as far as possible.
  // Following is adapted from node_view().
  $node = node_build_content($node, false, true);
  $content = drupal_render($node->content);
  $node->body = $content;
  unset($node->teaser);
  node_invoke_nodeapi($node, 'alter', false, true);

  $node = theme('simplenews_newsletter', $node, $tid);

  // TODO: Probably should refactor the whole processing, but check here for
  // mimemail and don't mess with body if we're gonna pass it to mimemail().
  if ($node->s_format == 'plain' || !module_exists('mimemail')) {
    $node->body = simplenews_html_to_text($node->body, variable_get('simplenews_hyperlinks_'. $tid, 1));
  }

  simplenews_set_from($node, $tid);
  return $node;
}

these two while loops do the sending.

function _simplenews_send($timer = FALSE) {

...

  $result = db_query(db_rewrite_sql('SELECT n.nid, s.tid, n.created FROM {node} n INNER JOIN {simplenews_newsletters} s ON n.nid = s.nid WHERE s.s_status = %d ORDER BY n.created ASC'), 1);
  while ($nid = db_fetch_object($result)) {
    $term = taxonomy_get_term($nid->tid);
    $node = simplenews_node_prepare($nid->nid, $nid->tid);

    $result2 = db_query('SELECT s.mail, s.snid FROM {simplenews_subscriptions} s INNER JOIN {simplenews_snid_tid} t ON s.snid = t.snid WHERE s.s_status = %d AND s.a_status = %d AND t.tid = %d ORDER BY s.snid ASC', 0, 1, $nid->tid);

    while ($mail = db_fetch_object($result2)) {
      $hash = _simplenews_generate_hash($mail->mail, $mail->snid, $nid->tid);

      // Add themable footer to message as this changes per user.
      $user_node = drupal_clone($node);
      $user_node = theme('simplenews_newsletter_footer', $user_node, $hash);

      $user_node->to = $mail->mail;

...

The outer loop is only concerned about building the Node(email). Not until the query for the inner loop is performed is any information about the user available. The only additional call is to theme. I'm not quite sure if this is what you mean. I'm still a little weak on understanding the flow of the Drupal architecture. But nonetrheless, to me it looks like all node preparation is done in the routine simplenews_node_prepare

Let me know if you find anything after you have a look at thre code.

I've kind of given up on this approach for my current project. I need to get finished rather quickly and It will take me less time to write something from scratch for what I want to do.

Thanks

Andy