How difficult would it be to redirect some messages from Privatemsg.module to SMTP?

The reason I ask, is that I am developing a system that will receive POP3 messages and put them into the Privatemsg database as a new Privatemsg. This is not theoretical, its almost ready and I should have it finished by tommrow. I would like users to be able to reply to those new POP messages and it seems that the only thing preventing that is to change the "check for recipient" to redirect the real email addresses to the SMTP server if the recipient is a real address. Seems easy enough.

Any mail that is sent to messages-uid@edomain.com is parsed and put into the DB. My other module notifies them that they have a new message waiting. The check their messages and see the new email. They reply and send to the SMTP server:

user: hardcode
pass: hardcode
server: hardcode
return: messages-uid@edomain.com
from: username@domain.com

This way they will only get messages that have been replied to, which is just fine. No user configuration changes needed. Maybe a checkbox to disable this feature in the Admin settings.

Comments

Fusion_Sushi’s picture

Wish I could edit that post, a couple typos.. I was not being clever, all domains should be the same.

Fusion_Sushi’s picture

whadda think? I'll take a crack at it myself if you can point me in the right direction... thanks!

Fusion_Sushi’s picture

Here is the code that throws the error:

  if (!$recipient->uid) {
    form_set_error('recipient', t('The <em>Recipient</em> does not exist.'));
    return _privatemsg_form((object)$edit);
  }
Fusion_Sushi’s picture

According to the docs this is the code to send mail via Drupal:

function user_mail($mail, $subject, $message, $header) { 
  if (variable_get('smtp_library', '') && file_exists(variable_get('smtp_library', ''))) { 
   include_once './' . variable_get('smtp_library', ''); 
    return user_mail_wrapper($mail, $subject, $message, $header); 
  } 
  else { 
    return mail( 
      $mail, 
      mime_header_encode($subject), 
      str_replace("\r", '', $message), 
      "MIME-Version: 1.0\nContent-Type: text/plain; charset=UTF-8; format=flowed\nContent-transfer-encoding: 8Bit\n" . $header 
    ); 
  } 
} 

So I need to merge the two..

Fusion_Sushi’s picture

Something like this?? *not tested*

if (!$recipient->uid) {
function user_mail($recipient, $subject, $privatemsgbody) { 
  if (variable_get('smtp_library', '') && file_exists(variable_get('smtp_library', ''))) { 
  include_once './' . variable_get('smtp_library', ''); 
    return user_mail_wrapper($recipient, $subject, $privatemsgbody); 
  } 
  else { 
    return mail( 
      $mail, 
      mime_header_encode($subject), 
      str_replace("\r", '', $privatemsgbody), 
      "MIME-Version: 1.0\nContent-Type: text/plain; charset=UTF-8; format=flowed\nContent-transfer-encoding: 8Bit\n"
    ); 
  } 
} 
}
Fusion_Sushi’s picture

Seems that this is more like what I am looking for but, this code hoses the module:

  if (!$recipient->uid) {
 user_mail($user->$recipient, $subject, $privatemsgbody, "From: $from\nReply-to: $from\nX-Mailer: Drupal\nReturn-path: $from\nErrors-to: $from");
}
Fusion_Sushi’s picture

K, that did'nt work. I think I was trying to send mail from the wrong line can someone help me catch the form data and send it?

user_mail($recipient, $subject, $privatemsgbody, "From: user-($user->uid@domain.com)");
header("Location: $HTTP_REFERER");

Something like that?

Fusion_Sushi’s picture

A little progress:

if (!$recipient->uid) {
    form_set_error('recipient', t('The <em>Recipient</em> does not exist.'));
    return _privatemsg_form((object)$edit);
  }

just removed this code. then put the send mail request at the end of the else statements right before the sql and it is now sending but.. I'm not catching the form data yet.

Fusion_Sushi’s picture

Ok, here is the way to send email via this module:

on or about line 520 remove exactly these chars:

  if (!$recipient->uid) {
    form_set_error('recipient', t('The <em>Recipient</em> does not exist.'));
    return _privatemsg_form((object)$edit);
  }
  else

then right before the original line 541:

$result = db_query("INSERT INTO {privatemsg} (author, recipient, subject, message, timestamp, newmsg, hostname, format)

add this:

$myfrom = "user-$user->uid@domain.com";
user_mail($edit['recipient'], $edit['subject'], $edit['privatemsgbody'], "From: $myfrom\nReply-to: $myfrom\nX-Mailer: Drupal\nReturn-path: $myfrom\nErrors-to: $myfrom");
Fusion_Sushi’s picture

Need a function to decide if a message is a PM or an email. On the new line 540, put this in to check for email... this does not put the email it into the database.

else {
$sendbyemail = array(".com",".net",".edu",".org","@");
if(in_array($edit['recipient'], $sendbyemail)) {
$myfrom = "email-$user->uid-@domain.com";
user_mail($edit['recipient'], $edit['subject'], $edit['privatemsgbody'], "From: $myfrom\nReply-to: $myfrom\nX-Mailer: Drupal\nReturn-path: $myfrom\nErrors-to: $myfrom");
}
  else {
    $result = db_query("INSERT INTO {privatemsg} (author, recipient, subject, message, timestamp, newmsg, hostname, format) VALUES ('%d', '%d', '%s', '%s', '%d', '%d', '%s', '%d')", $user->uid, $recipient->uid, $edit['subject'], $edit['privatemsgbody'], time(), 1, getenv("REMOTE_ADDR"), $edit['format']);
    drupal_set_message(t('Message sent.'));
    drupal_goto('privatemsg');
}
  }
}
Fusion_Sushi’s picture

Alright #10 was a misstep, don't use that. Here is an Alpha that has worked a couple times so far:

if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $edit['recipient'])) {  
$myfrom = "email-$user->uid-@domain.com";
user_mail(trim($edit['recipient']), $edit['subject'], $edit['privatemsgbody'], "From: $myfrom\nReply-to: $myfrom\nX-Mailer: Drupal\nReturn-path: $myfrom\nErrors-to: $myfrom");
}
else {  
$result = db_query("INSERT INTO {privatemsg} (author, recipient, subject, message, timestamp, newmsg, hostname, format) VALUES ('%d', '%d', '%s', '%s', '%d', '%d', '%s', '%d')", $user->uid, $recipient->uid, $edit['subject'], $edit['privatemsgbody'], time(), 1, getenv("REMOTE_ADDR"), $edit['format']);
}
drupal_set_message(t('Message sent.'));
drupal_goto('privatemsg');
}
}

Fusion_Sushi’s picture

Change this line:

$myfrom = "$user->name <email-$user->uid-@domain.com>";
Fusion_Sushi’s picture

Change line 417 to include hostname:

SELECT subject, message,hostname, u.name AS 

Change line 434 to send to the host(I'm using that field to store the actual recipient) you'll need my other script that retrieves the email and puts it into the database to have that field populated.

      '#default_value' => $message->hostname,

Finally edit line 507 to remove the use of the hostname. I would like to save that function but it does not really do anything so, it's better used to store email addresses:

$result = db_query("INSERT INTO {privatemsg} (author, recipient, subject, message, timestamp, newmsg, format) VALUES ('%d', '%d', '%s', '%s', '%d', '%d', '%s', '%d')", $user->uid, $recipient->uid, $edit['subject'], $edit['privatemsgbody'], time(), 1,  $edit['format']);
Fusion_Sushi’s picture

And change, from line 429

  if ($message->hostname != '') {
  $form['recipient'] = array(
      '#prefix' => '<div class="container-inline">',
      '#type' => 'textfield',
      '#title' => t("To"),
      '#default_value' => $message->hostname,
      '#size' => 40,
      '#maxlength' => 64
      );
}
else {
  $form['recipient'] = array(
      '#prefix' => '<div class="container-inline">',
      '#type' => 'textfield',
      '#title' => t("To"),
      '#default_value' => $message->recipient,
      '#size' => 40,
      '#maxlength' => 64
      );
}

Fusion_Sushi’s picture

One small change, I missed removeing this symbol in post #13

'%s',
litwol’s picture

Status: Active » Closed (fixed)

I apologize for pinging the participants, i'm closing out old issues. Privatemsg already implements some of these features. Please file a feature request against the latest DRUPAL-6 version if still relevant.

Specific to routing messages. privatemsg module now has API that allows other messages to expand it's functionality to react to events. one relevant event here is "user is receiving message", so then you can route it to mail. please submit feature request (and patch :) ) if this is still relevant.