+/**
+ * Construct our "From:" header based on
+ * a supplied identity number.
+ * Will fall back when no sensible email address has been defined.
+ *
+ * @param int $identity identity# to use
+ * @since 1.5.2
+ */
+function build_from_header($identity = 0) {
+
+ global $domain;
+
+ $idents = get_identities();
+
+ if (! isset($idents[$identity]) ) $identity = 0;
+
+ if ( !empty($idents[$identity]['full_name']) ) {
+ $from_name = $idents[$identity]['full_name'];
+ }
+
+ $from_mail = $idents[$identity]['email_address'];
+ if (strpos($from_mail, '@') === FALSE)
+ $from_mail .= '@' . $domain;
+
+ if ( isset($from_name) ) {
+ $from_name_encoded = encodeHeader('"' . $from_name . '"');
+ if ($from_name_encoded != $from_name) {
+ return $from_name_encoded . ' <' . $from_mail . '>';
+ }
+ return '"' . $from_name . '" <' . $from_mail . '>';
+ }
+ return $from_mail;
+}
+
+/**
+ * Find a matching identity based on a set of emailaddresses.
+ * Will return the first identity to have a matching address.
+ * When nothing found, returns the default identity.
+ *
+ * @param needles array list of mailadresses
+ * @returns int identity
+ * @since 1.5.2
+ */
+function find_identity($needles) {
+ $idents = get_identities();
+ if ( count($idents) == 1 || empty($needles) ) return 0;
+
+ foreach ( $idents as $nr => $ident ) {
+ if ( isset($ident['email_address']) ) {
+ foreach ( $needles as $needle ) {
+ if ( strcasecmp($needle, $ident['email_address']) == 0 ) {
+ return $nr;
+ }
+ }
+ }
+ }
+ return 0;
+}