# How to send a single email after csv file saved and attached to email output plugin

**URL:** <https://discuss.elastic.co/t/how-to-send-a-single-email-after-csv-file-saved-and-attached-to-email-output-plugin/186136>\
**Category:** Logstash\
**Created:** [June 17, 2019, 6:29pm UTC](https://discuss.elastic.co/t/how-to-send-a-single-email-after-csv-file-saved-and-attached-to-email-output-plugin/186136 "2019-06-17T18:29:24Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ennr](https://avatars.discourse-cdn.com/v4/letter/e/e274bd/32.png) [@Ennr](https://discuss.elastic.co/u/Ennr)\
**Post date:** [June 17, 2019, 6:29pm UTC](https://discuss.elastic.co/t/how-to-send-a-single-email-after-csv-file-saved-and-attached-to-email-output-plugin/186136/1 "2019-06-17T18:29:24Z")

</div>

My requirement is send a file ( extracting data from elastic search index is to save in CSV or excel format it contains 1000 records ) in a single mail through logstash output email plugin. is it possible or not?

weather it is possible using if else condition or filters ?

can any one help me on this issue and i am new to this tool.

---

<div class="post-metadata">

**Author:** ![ErikWarming](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/erikwarming/32/39525_2.png) [@ErikWarming](https://discuss.elastic.co/u/ErikWarming)\
**Post date:** [June 18, 2019, 12:36pm UTC](https://discuss.elastic.co/t/how-to-send-a-single-email-after-csv-file-saved-and-attached-to-email-output-plugin/186136/2 "2019-06-18T12:36:53Z")

</div>

I am also struggling with the output email.

But there is an output to create csv

```
    csv {   
        fields =>["case_id", "cpr"]
        csv_options => {
        # "write_headers" => true
        # "headers" =>["case_id","cpr"]
            "col_sep" => ";"
        }
        path => "cpr_sagsnummer_liste-%{+YYYY-MM-dd-kk-mm}.csv"
    }

```

Logstash will write a header for each line, its a mess, so I don't use the header option

For the email output, there is the attachments option. But I am struggling with sending emails so I haven't gotten to this bit yet.  
attachments =\> "cpr\_sagsnummer\_liste-%{+YYYY-MM-dd-kk-mm}.csv"

---

<div class="post-metadata">

**Author:** ![Ennr](https://avatars.discourse-cdn.com/v4/letter/e/e274bd/32.png) [@Ennr](https://discuss.elastic.co/u/Ennr)\
**Post date:** [June 18, 2019, 12:59pm UTC](https://discuss.elastic.co/t/how-to-send-a-single-email-after-csv-file-saved-and-attached-to-email-output-plugin/186136/3 "2019-06-18T12:59:54Z")

</div>

you can send email only when the file is available  
Attachment should be a existing file name with path

But in my case i have to save the csv file and send mail with the saved csv file, Both should be done one after another in in single logstash conf file

---

<div class="post-metadata">

**Author:** ![ErikWarming](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/erikwarming/32/39525_2.png) [@ErikWarming](https://discuss.elastic.co/u/ErikWarming)\
**Post date:** [July 5, 2019, 7:15am UTC](https://discuss.elastic.co/t/how-to-send-a-single-email-after-csv-file-saved-and-attached-to-email-output-plugin/186136/4 "2019-07-05T07:15:55Z")

</div>

Logstash saves/creates the csv at the end of the execution, so one config file can't create a csv AND send an email with the same csv, all in one go. You can have a group of conf files all running on a schedule. A.conf creates the csv and B.csv sends the csv a few minutes later.

I found a solution to my problem.  
I have row numbers in my log file. I'll send a mail when row\_nr == 1, that way I only send one mail pr conf execution. And then I create a csv file on a shared drive

---

<div class="post-metadata">

**Author:** ![Ennr](https://avatars.discourse-cdn.com/v4/letter/e/e274bd/32.png) [@Ennr](https://discuss.elastic.co/u/Ennr)\
**Post date:** [July 5, 2019, 6:57pm UTC](https://discuss.elastic.co/t/how-to-send-a-single-email-after-csv-file-saved-and-attached-to-email-output-plugin/186136/5 "2019-07-05T18:57:26Z")

</div>

I found a solution to my problem. one config file can create a csv AND send an email with the same csv, all in one A.conf. using if condition

---

<div class="post-metadata">

**Author:** ![ErikWarming](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/erikwarming/32/39525_2.png) [@ErikWarming](https://discuss.elastic.co/u/ErikWarming)\
**Post date:** [July 8, 2019, 12:04pm UTC](https://discuss.elastic.co/t/how-to-send-a-single-email-after-csv-file-saved-and-attached-to-email-output-plugin/186136/6 "2019-07-08T12:04:40Z")

</div>

Can I see your conf file?

---

<div class="post-metadata">

**Author:** ![Ennr](https://avatars.discourse-cdn.com/v4/letter/e/e274bd/32.png) [@Ennr](https://discuss.elastic.co/u/Ennr)\
**Post date:** [July 10, 2019, 10:28am UTC](https://discuss.elastic.co/t/how-to-send-a-single-email-after-csv-file-saved-and-attached-to-email-output-plugin/186136/7 "2019-07-10T10:28:55Z")

</div>

below is the blue print of my conf file

```
input {
##use the input plugin as per your requirement
}

filter {
#filters script as per your requirement
}

output{
csv {
      fields => ["userid","name","country"]
      path => "/home/clouduser/efg.csv"
    }
if [name] == "smith" { ##smith is the name field in csv file to send single email because it matches if conditon ##  
email {
  to => 'technical@example.com'
  from => 'monitor@example.com'
  subject => 'Alert - %{title}'
  body => "Tags: %{tags}\\n\\Content:\\n%{message}"
  template_file => "/tmp/email_template.mustache"
  domain => 'mail.example.com'
  port => 25
 attachments => "/home/clouduser/efg.csv"
   }
 }
}
}
```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [August 7, 2019, 10:28am UTC](https://discuss.elastic.co/t/how-to-send-a-single-email-after-csv-file-saved-and-attached-to-email-output-plugin/186136/8 "2019-08-07T10:28:58Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
