<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Dynamically Change Email Recipient? in Reporting</title>
    <link>https://community.splunk.com/t5/Reporting/Dynamically-Change-Email-Recipient/m-p/61828#M1349</link>
    <description>&lt;P&gt;To answer my own question, I had to do this through a custom python script that loops through email fields, then calling the sendemail command in each loop with the email address as input.  This actually works.&lt;/P&gt;

&lt;P&gt;Caveat is that when calling the sendemail command through the python script, you need to be authenticated through the commandline. I created a Splunk-only (not LDAP) user with limited capabilities that gets authenticated each time the script is called using the auth tag.&lt;/P&gt;

&lt;P&gt;Per request, here's the skeleton of the implementation (note, on Splunk 5):&lt;/P&gt;

&lt;OL&gt;
&lt;LI&gt;&lt;P&gt;In Splunk manager, set up the scheduled search&lt;/P&gt;

&lt;P&gt;&lt;CODE&gt;"whatever search that gets you the email addresses you want" | eval toEmail=fieldWithEmail | emailto&lt;/CODE&gt;&lt;/P&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;P&gt;On the server, in &lt;CODE&gt;bin&lt;/CODE&gt; (I forget the precise details of how to get the script to get recognized, but I recall having to change permissions), create &lt;CODE&gt;emailto.py&lt;/CODE&gt; with your custom bin locations and such:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;# this comment is here bc i couldnt figure out how to get code formatting to work otherwise
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;import splunk.Intersplunk&lt;BR /&gt;
import subprocess&lt;BR /&gt;
from subprocess import Popen, PIPE, STDOUT&lt;/P&gt;

&lt;P&gt;class CustomEmail:&lt;BR /&gt;
    def &lt;STRONG&gt;init&lt;/STRONG&gt;(self, to_email):&lt;BR /&gt;
        self.to = to_email&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;def send_email(self):
    cc = 'some_email@to_be_cc_ed.com'
    command = 'YOUR_APP_DIR/bin/custom_email.sh %s %s' \
              % (self.to, cc)
    output = 'none'
    try:
        p = Popen(command, shell=True, stdin=PIPE, stdout=PIPE,
                  stderr=PIPE, close_fds=True)
        output = p.stdout.read()
        if output == '':
            print p.stderr.read()
    except e:
        print e
    print('________________________________________________')
    print('Custom email ran for: %s' % self.to)
    print(output)
    return 'success'
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;H1&gt;Get the previous search results&lt;/H1&gt;

&lt;P&gt;(results, _, _) = splunk.Intersplunk.getOrganizedResults()&lt;/P&gt;

&lt;H1&gt;Process each result&lt;/H1&gt;

&lt;P&gt;for result in results:&lt;BR /&gt;
    ce = CustomEmail(result["toEmail"])&lt;BR /&gt;
    try:&lt;BR /&gt;
        result["emailResults"] = ce.send_email()&lt;BR /&gt;
    except Exception, e:&lt;BR /&gt;
        print(str(e))&lt;/P&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;P&gt;Now write the shell script &lt;CODE&gt;custom_email.sh&lt;/CODE&gt; contains the search that you want to run to be sent to the individual.  It's first argument is the to email, and the second is the cc email. Play around with it to test outside of production. Having a hard time with code-formatting, so block quoting here...&lt;/P&gt;&lt;/LI&gt;
&lt;/OL&gt;

&lt;BLOCKQUOTE&gt;
&lt;P&gt;export HOME=/opt/splunk&lt;BR /&gt;
/opt/splunk/bin/splunk login -auth dummyuser:somepassword&lt;BR /&gt;
/opt/splunk/bin/splunk search "sourcetype=\"whatever\" $1 earliest=-24h@h | sendemail to=\"$1\" bcc=\"$2\" from=\"from_address@you_re_using\" subject=\"Custom Email Report for $1\" sendresults=true"&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;</description>
    <pubDate>Mon, 28 Sep 2020 12:23:07 GMT</pubDate>
    <dc:creator>ytamura</dc:creator>
    <dc:date>2020-09-28T12:23:07Z</dc:date>
    <item>
      <title>Dynamically Change Email Recipient?</title>
      <link>https://community.splunk.com/t5/Reporting/Dynamically-Change-Email-Recipient/m-p/61827#M1348</link>
      <description>&lt;P&gt;Is there any easy to use the sendemail command to set the "to" field dynamically based on search results?&lt;/P&gt;

&lt;P&gt;For example, if I have search results with the field toEmail="&lt;A href="mailto:blah@blah.com"&gt;blah@blah.com&lt;/A&gt;" how can I use this with sendemail to send an email with results to &lt;A href="mailto:blah@blah.com"&gt;blah@blah.com&lt;/A&gt;? When I do the following&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| sendemail to=toEmail
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;it seems to take toEmail as a literal string.  I'm hoping to avoid writing a complicated custom python script to be able to do this. Thanks!&lt;/P&gt;</description>
      <pubDate>Mon, 28 May 2012 19:50:04 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Reporting/Dynamically-Change-Email-Recipient/m-p/61827#M1348</guid>
      <dc:creator>ytamura</dc:creator>
      <dc:date>2012-05-28T19:50:04Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamically Change Email Recipient?</title>
      <link>https://community.splunk.com/t5/Reporting/Dynamically-Change-Email-Recipient/m-p/61828#M1349</link>
      <description>&lt;P&gt;To answer my own question, I had to do this through a custom python script that loops through email fields, then calling the sendemail command in each loop with the email address as input.  This actually works.&lt;/P&gt;

&lt;P&gt;Caveat is that when calling the sendemail command through the python script, you need to be authenticated through the commandline. I created a Splunk-only (not LDAP) user with limited capabilities that gets authenticated each time the script is called using the auth tag.&lt;/P&gt;

&lt;P&gt;Per request, here's the skeleton of the implementation (note, on Splunk 5):&lt;/P&gt;

&lt;OL&gt;
&lt;LI&gt;&lt;P&gt;In Splunk manager, set up the scheduled search&lt;/P&gt;

&lt;P&gt;&lt;CODE&gt;"whatever search that gets you the email addresses you want" | eval toEmail=fieldWithEmail | emailto&lt;/CODE&gt;&lt;/P&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;P&gt;On the server, in &lt;CODE&gt;bin&lt;/CODE&gt; (I forget the precise details of how to get the script to get recognized, but I recall having to change permissions), create &lt;CODE&gt;emailto.py&lt;/CODE&gt; with your custom bin locations and such:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;# this comment is here bc i couldnt figure out how to get code formatting to work otherwise
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;import splunk.Intersplunk&lt;BR /&gt;
import subprocess&lt;BR /&gt;
from subprocess import Popen, PIPE, STDOUT&lt;/P&gt;

&lt;P&gt;class CustomEmail:&lt;BR /&gt;
    def &lt;STRONG&gt;init&lt;/STRONG&gt;(self, to_email):&lt;BR /&gt;
        self.to = to_email&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;def send_email(self):
    cc = 'some_email@to_be_cc_ed.com'
    command = 'YOUR_APP_DIR/bin/custom_email.sh %s %s' \
              % (self.to, cc)
    output = 'none'
    try:
        p = Popen(command, shell=True, stdin=PIPE, stdout=PIPE,
                  stderr=PIPE, close_fds=True)
        output = p.stdout.read()
        if output == '':
            print p.stderr.read()
    except e:
        print e
    print('________________________________________________')
    print('Custom email ran for: %s' % self.to)
    print(output)
    return 'success'
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;H1&gt;Get the previous search results&lt;/H1&gt;

&lt;P&gt;(results, _, _) = splunk.Intersplunk.getOrganizedResults()&lt;/P&gt;

&lt;H1&gt;Process each result&lt;/H1&gt;

&lt;P&gt;for result in results:&lt;BR /&gt;
    ce = CustomEmail(result["toEmail"])&lt;BR /&gt;
    try:&lt;BR /&gt;
        result["emailResults"] = ce.send_email()&lt;BR /&gt;
    except Exception, e:&lt;BR /&gt;
        print(str(e))&lt;/P&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;P&gt;Now write the shell script &lt;CODE&gt;custom_email.sh&lt;/CODE&gt; contains the search that you want to run to be sent to the individual.  It's first argument is the to email, and the second is the cc email. Play around with it to test outside of production. Having a hard time with code-formatting, so block quoting here...&lt;/P&gt;&lt;/LI&gt;
&lt;/OL&gt;

&lt;BLOCKQUOTE&gt;
&lt;P&gt;export HOME=/opt/splunk&lt;BR /&gt;
/opt/splunk/bin/splunk login -auth dummyuser:somepassword&lt;BR /&gt;
/opt/splunk/bin/splunk search "sourcetype=\"whatever\" $1 earliest=-24h@h | sendemail to=\"$1\" bcc=\"$2\" from=\"from_address@you_re_using\" subject=\"Custom Email Report for $1\" sendresults=true"&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;</description>
      <pubDate>Mon, 28 Sep 2020 12:23:07 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Reporting/Dynamically-Change-Email-Recipient/m-p/61828#M1349</guid>
      <dc:creator>ytamura</dc:creator>
      <dc:date>2020-09-28T12:23:07Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamically Change Email Recipient?</title>
      <link>https://community.splunk.com/t5/Reporting/Dynamically-Change-Email-Recipient/m-p/61829#M1350</link>
      <description>&lt;P&gt;Hi,&lt;BR /&gt;
I am facing the problem while trying dynamic recipients for sendmail command. If you could share your script and relevant commands.conf file so that I check the arguments and options the sendmail.py, it will be great.&lt;BR /&gt;
Thanks&lt;/P&gt;</description>
      <pubDate>Sat, 29 Aug 2015 11:13:13 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Reporting/Dynamically-Change-Email-Recipient/m-p/61829#M1350</guid>
      <dc:creator>RichaSingh</dc:creator>
      <dc:date>2015-08-29T11:13:13Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamically Change Email Recipient?</title>
      <link>https://community.splunk.com/t5/Reporting/Dynamically-Change-Email-Recipient/m-p/61830#M1351</link>
      <description>&lt;P&gt;Hi,&lt;BR /&gt;
Can you please share the python script to send mail to dynamic recipients? It will be very helpful for me.&lt;/P&gt;

&lt;P&gt;Thanks,&lt;BR /&gt;
Santosh&lt;/P&gt;</description>
      <pubDate>Thu, 17 Sep 2015 17:28:00 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Reporting/Dynamically-Change-Email-Recipient/m-p/61830#M1351</guid>
      <dc:creator>vysyarajusantos</dc:creator>
      <dc:date>2015-09-17T17:28:00Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamically Change Email Recipient?</title>
      <link>https://community.splunk.com/t5/Reporting/Dynamically-Change-Email-Recipient/m-p/61831#M1352</link>
      <description>&lt;P&gt;In Splunk 6.2 and above Just use &lt;CODE&gt;mysearch | eval eMail="my@email.com" | sendemail to=$result.eMail$&lt;/CODE&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 10 Nov 2015 10:47:35 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Reporting/Dynamically-Change-Email-Recipient/m-p/61831#M1352</guid>
      <dc:creator>tfruru</dc:creator>
      <dc:date>2015-11-10T10:47:35Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamically Change Email Recipient?</title>
      <link>https://community.splunk.com/t5/Reporting/Dynamically-Change-Email-Recipient/m-p/61832#M1353</link>
      <description>&lt;P&gt;Is it possible to get a copy of your script?&lt;/P&gt;</description>
      <pubDate>Mon, 06 Mar 2017 20:51:52 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Reporting/Dynamically-Change-Email-Recipient/m-p/61832#M1353</guid>
      <dc:creator>trumpjk</dc:creator>
      <dc:date>2017-03-06T20:51:52Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamically Change Email Recipient?</title>
      <link>https://community.splunk.com/t5/Reporting/Dynamically-Change-Email-Recipient/m-p/61833#M1354</link>
      <description>&lt;P&gt;yikes i just saw these requests.  i'll see if i can find it / strip out stuff from it. no guarantees i will get back to this thread before another 5 years pass though &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 06 Mar 2017 20:57:26 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Reporting/Dynamically-Change-Email-Recipient/m-p/61833#M1354</guid>
      <dc:creator>ytamura</dc:creator>
      <dc:date>2017-03-06T20:57:26Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamically Change Email Recipient?</title>
      <link>https://community.splunk.com/t5/Reporting/Dynamically-Change-Email-Recipient/m-p/61834#M1355</link>
      <description>&lt;P&gt;&lt;A href="https://docs.splunk.com/Documentation/Splunk/7.3.0/Alert/Emailnotification#Example_-_Send_email_to_different_recipients_based_on_search_results"&gt;https://docs.splunk.com/Documentation/Splunk/7.3.0/Alert/Emailnotification#Example_-_Send_email_to_different_recipients_based_on_search_results&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 01 Jul 2019 21:36:51 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Reporting/Dynamically-Change-Email-Recipient/m-p/61834#M1355</guid>
      <dc:creator>Lowell</dc:creator>
      <dc:date>2019-07-01T21:36:51Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamically Change Email Recipient?</title>
      <link>https://community.splunk.com/t5/Reporting/Dynamically-Change-Email-Recipient/m-p/61835#M1356</link>
      <description>&lt;P&gt;If you want to send all results to a single destination per alert, then you can do that by using the &lt;CODE&gt;$result.&amp;lt;to_email&amp;gt;$&lt;/CODE&gt; notation in the "To" field using modern version of Splunk (6.2, and later according to @tfruru), as mentioned in the &lt;A href="https://docs.splunk.com/Documentation/Splunk/7.3.0/Alert/Emailnotification#Example_-_Send_email_to_different_recipients_based_on_search_results"&gt;docs&lt;/A&gt;.   However, this is somewhat limited.  It appears that Splunk just grabs &lt;CODE&gt;$result.&amp;lt;field&amp;gt;$&lt;/CODE&gt; based on the first output row.&lt;/P&gt;

&lt;P&gt;If you need to get fancy and send emails to different to address on a per-row basis, then you should checkout the &lt;A href="https://splunkbase.splunk.com/app/1794/#/details"&gt;Sendresults&lt;/A&gt; add-on by Discovered Intelligence.  Emails with the same &lt;CODE&gt;email_to&lt;/CODE&gt; field will be grouped into a single email.  Very nice!&lt;/P&gt;</description>
      <pubDate>Mon, 01 Jul 2019 21:56:52 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Reporting/Dynamically-Change-Email-Recipient/m-p/61835#M1356</guid>
      <dc:creator>Lowell</dc:creator>
      <dc:date>2019-07-01T21:56:52Z</dc:date>
    </item>
  </channel>
</rss>

