I want to use additional profile information fields - email and password to send to a webmail server to log in to webmail for a user.

I've looked at the mail modules but none seem to quite do what I want.

I've looked at how to setup extra profile variables here

http://drupal.org/handbook/modules/profile

But it says nothing about the correct way to access the variables when writing PHP code.

Please advise.

Comments

therobyouknow’s picture

I always like to find a solution to my own problem - so here goes:

How to provide automatic login/single-sign-in to POP3/webmail when logging into your drupal site.
This code is for a drupal theme block which will provide a button to auto-sign into email once logged into your drupal site.

You need to create 2 new user profile fields profile_yourdomain_email (use only text before the @ for this field e.g. first.last@yourdomain.com just use first.last in this field) an profile_yourdomain_email_password.

The code assumes that your webmail system can be logged into via the http form post cgi method.
It also assumes the following fields in the posted form data.
- username
- password
- pop3host e.g. yourdomain.com
- mailserver e.g. yourdomain.com

and other fields may/may not be required by your isp host
-login type (e.g. simple - standard off the shelf webmail systems like @mail offer different ui skins - for example, simple)
-mailtype e.g. pop3

the disadvantage of this code is security, as the code fetches over the network standard via plain text http the password from the drupal database and embeds it as hidden form field on the page, and then this is submitted over network via standard plain text http to the webmail server. So these are 2 places where the password could be sniffed and hacked over an unsecure connection. If your ISP host webmail accepts encrypted passwords, like using, md5 then this might be advisable.

This code snippet example is also an illustration of how to read use profile fields for which there is a glaring omission in the drupal documentation as to how to do this!

So, created the user profile fields.
For profile_yourdomain_email - (use only text before the @ for this field e.g. first.last@yourdomain.com just use first.last in this field)
and the profile_yourdomain_email_password.

(Some of your users may not require a webmail account, so if you leave the profile_yourdomain_email blank then the button wont appear.)

Then go to the blocks section of admin, create a new block and paste this code in.

You might need to adjust the formatting as the code renderer on this page shows some lines as multiple lines where they aren't - they are actually just one line.

global $user;
profile_load_profile($user->uid);

// Get yourdomain email address of user
//
// find out the id associated with the field 'profile_yourdomain_email'  in table profile_fields - this table is the definitions of the profile fields
$result = db_query("SELECT fid FROM {profile_fields} WHERE name = '%s' ", "profile_yourdomain_email");
$data = db_fetch_object($result);
$fid = $data->fid;
$result = db_query("SELECT value FROM {profile_values} WHERE fid= '%d' AND uid= '%d' ",$fid, $user->uid );
$data = db_fetch_object($result);
$useryourdomainEmail = $data->value;

if ($useryourdomainEmail)
{
//print  $useryourdomainEmail;

// find out the password for the yourdomain email address
// find out the id associated with the field 'profile_yourdomain_email_password'  in table profile_fields - this table is the definitions of the profile fields
$result = db_query("SELECT fid FROM {profile_fields} WHERE name = '%s' ", "profile_yourdomain_email_password");
$data = db_fetch_object($result);
$fid = $data->fid;
$result = db_query("SELECT value FROM {profile_values} WHERE fid= '%d' AND uid= '%d' ", $fid,  $user->uid );
$data = db_fetch_object($result);
$useryourdomainEmailPassword = $data->value;
//print  $useryourdomainEmailPassword;


$form_action = "<FORM action=\"http://webmail.supanames.com/atmail.pl\" method=\"post\" name=\"loginPage\" target=\"_blank\">";

$form_field_username = "<input name=\"username\" type=\"hidden\" class=\"logininput\" id=\"username\" value=\"".$useryourdomainEmail."\">";

$form_field_pop3host = "<input name=\"pop3host\" type=hidden class=\"logininput\" value=\"yourdomain.com\">";

$form_field_mailserver = "<input name=\"MailServer\" type=\"hidden\" class=\"logininput\" id=\"MailServer\" value=\"yourdomain.com\">";

$form_field_password = "<input type='hidden' input name=\"password\" type=password class=\"logininput\" id=\"password\" value=\"".$useryourdomainEmailPassword."\">";

$form_field_logintype = "<input type='hidden' name='LoginType' class=\"loginselect\" value='simple'>";

$form_field_mailtype = "<input type='hidden' name='MailType' value='pop3'>"; $form_submit_button = "<input type=\"submit\" name=\"Submit\" value=".$useryourdomainEmail."@yourdomain.com class=\"loginsubmit\">";

// careful - this is one single line of code without carriage returns
$form = $form_action.$form_field_username.$form_field_pop3host. $form_field_mailserver.$form_field_password. $form_field_logintype.$form_field_mailtype.$form_submit_button."</form>";

print "Login to email:";
print $form;
}

I'd welcome your opinions - like if you can find a better way than this solution. I looked at the mail modules but they were still in beta and not for production or they were for IMAP only.