Skip to content

Commit

Permalink
Added flexibility for mailto links. Now uses semicolon as default sep…
Browse files Browse the repository at this point in the history
…arator.
  • Loading branch information
tbar0970 committed May 30, 2015
1 parent 3376f31 commit 48438f4
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 29 deletions.
32 changes: 16 additions & 16 deletions calls/call_email.class.php
Expand Up @@ -56,12 +56,9 @@ function run()
} else if ((count($emails) > EMAIL_CHUNK_SIZE) || !empty($blanks)) {
$this->launchPopupFromHiddenIframe($blanks);
} else if (count($emails) > 0) {
$my_email = $GLOBALS['user_system']->getCurrentUser('email');
$public = array_get($_REQUEST, 'method') == 'public';
$to = implode(',', $emails);
if (!$public) $to = $my_email.'?bcc='.$to;
$public = array_get($_REQUEST, 'method') == 'public';
?>
<a id="mailto" href="mailto:<?php echo $to; ?>" target="_parent">Send email</a>
<a id="mailto" href="<?php echo $this->getHref($emails, $public); ?>" target="_parent" <?php echo email_link_extras(); ?>>Send email</a>
<script>document.getElementById('mailto').click();</script>
<?php
} else {
Expand All @@ -71,21 +68,26 @@ function run()
}
}

private function getPrivateTo($emails) {
$my_email = $GLOBALS['user_system']->getCurrentUser('email');
return $my_email.'?bcc='.implode(',', array_diff($emails, Array($my_email)));
private function getHref($emails, $public) {
if ($public) {
$href = get_email_href($emails);
} else {
$my_email = $GLOBALS['user_system']->getCurrentUser('email');
$href = get_email_href($my_email, NULL, array_diff($emails, Array($my_email)));
}
return $href;
}

private function printModal($emails, $archived, $blanks) {
$my_email = $GLOBALS['user_system']->getCurrentUser('email');
$chunks = array_chunk($emails, EMAIL_CHUNK_SIZE);
$this->printArchivedWarning($archived);

if (count($chunks) == 1) {
?>
<p>
<a href="mailto:<?php echo $this->getPrivateTo($emails); ?>" class="btn btn-primary">Email privately</a>
<a href="<?php echo $this->getHref($emails, FALSE); ?>" class="btn btn-primary" <?php echo email_link_extras(); ?>>Email privately</a>
&nbsp;
<a href="mailto:<?php echo implode(',', $emails); ?>" class="btn btn-danger" title="WARNING: this will let all group membes see each other's addresses, and should be used with care">Email publicly</a>
<a href="<?php echo $this->getHref($emails, TRUE); ?>" class="btn btn-danger" <?php echo email_link_extras(); ?> title="WARNING: this will let all group members see each other's addresses, and should be used with care">Email publicly</a>
</p>
<?php
} else {
Expand All @@ -94,7 +96,7 @@ private function printModal($emails, $archived, $blanks) {
<?php
foreach ($chunks as $i => $chunk) {
?>
<a href="mailto:<?php echo $this->getPrivateTo($chunk); ?>" class="btn" onclick="this.style.textDecoration='line-through'">Email Batch #<?php echo $i; ?></a>
<a href="<?php echo $this->getHref($chunk, FALSE); ?>" class="btn" onclick="this.style.textDecoration='line-through'" <?php echo email_link_extras(); ?>>Email Batch #<?php echo $i; ?></a>
<?php
}
?>
Expand All @@ -121,10 +123,9 @@ private function printPopup($emails, $archived, $blanks) {
$chunks = array_chunk($emails, EMAIL_CHUNK_SIZE);
if (count($chunks) == 1) {
$this->printArchivedWarning($archived);
$to = $public ? implode(',', $emails) : $this->getPrivateTo($emails);
?>
<br />
<div class="align-center"><a class="btn btn-primary" href="mailto:<?php echo $to ?>">Email selected persons now</a></div>
<div class="align-center"><a class="btn btn-primary" href="<?php echo $this->getHref($emails, $public); ?>" <?php echo email_link_extras(); ?>>Email selected persons now</a></div>
<?php
} else {
?>
Expand All @@ -133,9 +134,8 @@ private function printPopup($emails, $archived, $blanks) {
<p style="line-height: 50px">
<?php
foreach ($chunks as $i => $chunk) {
$to = $public ? implode(',', $chunk) : $this->getPrivateTo($chunk);
?>
<a class="btn" href="mailto:<?php echo $to; ?>" onclick="this.style.textDecoration='line-through'">Email Batch #<?php echo ($i+1); ?></a>&nbsp;&nbsp;
<a class="btn" href="<?php echo $this->getHref($emails, $public); ?>" onclick="this.style.textDecoration='line-through'" <?php echo email_link_extras(); ?>>Email Batch #<?php echo ($i+1); ?></a>&nbsp;&nbsp;
<?php
}
?>
Expand Down
11 changes: 11 additions & 0 deletions conf.php.sample.au
Expand Up @@ -220,3 +220,14 @@ define('CHUNK_SIZE', 100);
// Extra HTML to add to the <head> of every page such as additional CSS
// For example <link rel="stylesheet" type="text/css" href="/extrastuff/jethro-extra.css" >
// define('EXTRA_HEAD_HTML', '');

// Separator character used for combining multiple email addresses in a link
// define('MULTI_EMAIL_SEPARATOR', ';');

// Custom function for building email URLs.
// Write your own, or use the sample below to go direct to gmail.
// function custom_email_href($to, $name, $bcc, $subject) { return 'https://mail.google.com/mail/?view=cm&fs=1&tf=1&to='.$to.'&bcc='.$bcc.'&su='.$subject; }

// Custom function for adding attributes to email URLs.
// Write your own, or use the sample below to make sure all mailto links open in a new window.
// function custom_email_extras() { return 'target="_mail"'; }
11 changes: 11 additions & 0 deletions conf.php.sample.in
Expand Up @@ -220,3 +220,14 @@ define('CHUNK_SIZE', 100);
// Extra HTML to add to the <head> of every page such as additional CSS
// For example <link rel="stylesheet" type="text/css" href="/extrastuff/jethro-extra.css" >
// define('EXTRA_HEAD_HTML', '');

// Separator character used for combining multiple email addresses in a link
// define('MULTI_EMAIL_SEPARATOR', ';');

// Custom function for building email URLs.
// Write your own, or use the sample below to go direct to gmail.
// function custom_email_href($to, $name, $bcc, $subject) { return 'https://mail.google.com/mail/?view=cm&fs=1&tf=1&to='.$to.'&bcc='.$bcc.'&su='.$subject; }

// Custom function for adding attributes to email URLs.
// Write your own, or use the sample below to make sure all mailto links open in a new window.
// function custom_email_extras() { return 'target="_mail"'; }
13 changes: 12 additions & 1 deletion conf.php.sample.uk
Expand Up @@ -226,4 +226,15 @@ define('CHUNK_SIZE', 100);

// Extra HTML to add to the <head> of every page such as additional CSS
// For example <link rel="stylesheet" type="text/css" href="/extrastuff/jethro-extra.css" >
// define('EXTRA_HEAD_HTML', '');
// define('EXTRA_HEAD_HTML', '');

// Separator character used for combining multiple email addresses in a link
// define('MULTI_EMAIL_SEPARATOR', ';');

// Custom function for building email URLs.
// Write your own, or use the sample below to go direct to gmail.
// function custom_email_href($to, $name, $bcc, $subject) { return 'https://mail.google.com/mail/?view=cm&fs=1&tf=1&to='.$to.'&bcc='.$bcc.'&su='.$subject; }

// Custom function for adding attributes to email URLs.
// Write your own, or use the sample below to make sure all mailto links open in a new window.
// function custom_email_extras() { return 'target="_mail"'; }
13 changes: 12 additions & 1 deletion conf.php.sample.usa
Expand Up @@ -219,4 +219,15 @@ define('CHUNK_SIZE', 100);

// Extra HTML to add to the <head> of every page such as additional CSS
// For example <link rel="stylesheet" type="text/css" href="/extrastuff/jethro-extra.css" >
// define('EXTRA_HEAD_HTML', '');
// define('EXTRA_HEAD_HTML', '');

// Separator character used for combining multiple email addresses in a link
// define('MULTI_EMAIL_SEPARATOR', ';');

// Custom function for building email URLs.
// Write your own, or use the sample below to go direct to gmail.
// function custom_email_href($to, $name, $bcc, $subject) { return 'https://mail.google.com/mail/?view=cm&fs=1&tf=1&to='.$to.'&bcc='.$bcc.'&su='.$subject; }

// Custom function for adding attributes to email URLs.
// Write your own, or use the sample below to make sure all mailto links open in a new window.
// function custom_email_extras() { return 'target="_mail"'; }
2 changes: 1 addition & 1 deletion db_objects/roster_view.class.php
Expand Up @@ -603,7 +603,7 @@ function updateClashesForRow(row)
if (!empty($emails)) {
?>
<p class="smallprint no-print">
<a href="mailto:<?php echo $my_email; ?>?bcc=<?php echo implode(',', $emails); ?>&subject=<?php echo (date('jS F', strtotime($date))); ?>">Email All</a>
<a href="<?php echo get_email_href($my_email, NULL, $emails, date('jS F', strtotime($date))); ?>" <?php echo email_link_extras(); ?>>Email All</a>
<?php
if (defined('SMS_HTTP_URL') && constant('SMS_HTTP_URL') && $GLOBALS['user_system']->havePerm(PERM_SENDSMS)) {
?>
Expand Down
2 changes: 1 addition & 1 deletion include/db_object.class.php
Expand Up @@ -644,7 +644,7 @@ public function printFieldValue($name, $value=null)
echo '<a href="tel:'.$value.'">'.ents($this->getFormattedValue($name, $value)).'</a>';
} else if (($this->fields[$name]['type'] == 'email')) {
$personName = ($this->values[$name] == $value) ? $this->values['first_name'].' '.$this->values['last_name'] : '';
echo '<a href="'.get_mailto_url($value, $personName).'">'.ents($value).'</a>';
echo '<a href="'.get_email_href($value, $personName).'" '.email_link_extras().'>'.ents($value).'</a>';
} else if (($this->fields[$name]['type'] == 'html')) {
echo $this->getFormattedValue($name, $value);
} else {
Expand Down
40 changes: 36 additions & 4 deletions include/general.php
Expand Up @@ -568,15 +568,47 @@ function format_phone_number($x, $formats)
}
}

function get_mailto_url($addresses, $name) {
/**
* Generates a mailto href that can be clicked to send an email.
* Can be overridden by defining custom_email_href($to, $name, $bcc, $subject)
* @param array|string $to
* @param string|null $name Only applicable if a single to-address is specified
* @param array|string $bcc optional
* @param string $subject optional
* @return string
*/
function get_email_href($to, $name=NULL, $bcc=NULL, $subject=NULL)
{
$sep = defined('MULTI_EMAIL_SEPARATOR') ? MULTI_EMAIL_SEPARATOR : ';';
if (!empty($to)) $to = implode($sep, (array)$to);
if (!empty($bcc)) $bcc = implode($sep, (array)$bcc);

if (function_exists('custom_email_href')) return custom_email_href($to, $name, $bcc, $subject);

// Chrome on mac with mac:mail as the mailto handler cannot cope with fullname in the address
$is_chrome_mac = (FALSE !== strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'chrome/'))
&& (FALSE !== strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'macintosh'));
if (empty($name) || $is_chrome_mac) {
return 'mailto:'.$addresses;

if (!$is_chrome_mac && !empty($name) && empty($bcc) && empty($subject)) {
$res = rawurlencode($name).' <'.rawurlencode($to).'>';
} else {
return 'mailto:'.rawurlencode($name).' <'.$addresses.'>';
$res = rawurlencode($to);
$extras = Array();
if ($bcc) $extras[] = 'bcc='.rawurlencode($bcc);
if ($subject) $extras[] = 'subject='.rawurlencode($subject);
if ($extras) $res .= '?'.implode('&', $extras);
}
return 'mailto:'.$res;
}

/**
* Returns any additional attributes to be added to an email link, eg target=blank
* Can be overridden by defining custom_email_extras()
* @return string
*/
function email_link_extras()
{
if (function_exists('custom_email_extras')) return custom_email_extras();
}

// From http://stackoverflow.com/questions/1182584/secure-random-number-generation-in-php
Expand Down
2 changes: 1 addition & 1 deletion templates/view_family.template.php
Expand Up @@ -83,7 +83,7 @@
}
$all_emails = $family->getAllEmailAddrs();
if (!empty($all_emails)) {
$links[] = '<a href="mailto: '.implode(', ', $all_emails).'"><i class="icon-email">@</i>Email All</a>';
$links[] = '<a href="'.get_email_href($all_emails).'" '.email_link_extras().'><i class="icon-email">@</i>Email All</a>';
}
echo implode(' &nbsp; ', $links);
?>
Expand Down
8 changes: 4 additions & 4 deletions views/view_5_groups.class.php
Expand Up @@ -12,12 +12,12 @@ function getTitle()

function processView()
{
if (!empty($_REQUEST['person_groupid'])) {
$_REQUEST['groupid'] = $_REQUEST['person_groupid'];
}
if (!empty($_REQUEST['groupid'])) {
$this->_group =& $GLOBALS['system']->getDBObject('person_group', (int)$_REQUEST['groupid']);
}
if (!empty($_REQUEST['person_groupid'])) {
$this->_group =& $GLOBALS['system']->getDBObject('person_group', (int)$_REQUEST['person_groupid']);
}
if (isset($_REQUEST['show_archived'])) {
$_SESSION['show_archived_group_members'] = (int)$_REQUEST['show_archived'];
}
Expand Down Expand Up @@ -133,7 +133,7 @@ function printView()
if (!empty($persons)) {
?>
<div class="email-link">
<a href="<?php echo build_url(Array('view' => NULL, 'call' => 'email', 'show_modal' => 1)); ?>" data-target="#email-modal" data-toggle="modal"><i class="icon-email">@</i>Email members</a>
<a href="<?php echo build_url(Array('view' => NULL, 'call' => 'email', 'groupid' => $this->_group->id, 'show_modal' => 1)); ?>" data-target="#email-modal" data-toggle="modal"><i class="icon-email">@</i>Email members</a>
</div>
<?php
}
Expand Down

0 comments on commit 48438f4

Please sign in to comment.