# Sending emails from Julia using sendmail

**URL:** https://discourse.julialang.org/t/sending-emails-from-julia-using-sendmail/26672
**Category:** General Usage
**Created:** [July 23, 2019, 8:38am UTC](https://discourse.julialang.org/t/sending-emails-from-julia-using-sendmail/26672 "2019-07-23T08:38:44Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [July 23, 2019, 8:38am UTC](https://discourse.julialang.org/t/sending-emails-from-julia-using-sendmail/26672/1 "2019-07-23T08:38:45Z")

</div>

Hello, which is the simplest way to send basic text emails from a julia script in a server that already has sendmail (PostFix sendmail interface) but without setting up a SMTP server (i.e. without [https://github.com/aviks/SMTPClient.jl](https://github.com/aviks/SMTPClient.jl)) ?

I am trying with:

```julia
sendEmailCmd = `sendmail \-t \< testEmail`
run(sendEmailCmd)

```

where testEmail is a file with:

```julia
To: myTestAddress@gmail.com
From: myFromAdress@myServer.org
Subject: One-line subject

This is the content of the email.

```

(would be better if it could be just a string in Julia, so I don’t need to save it to disk)

But it stucks and it doesn’t work (on the command line it works).

If I then I type `mail`, I notice an undelivered mail message where the key point is:

```julia
<testEmail@myServer.org> (expanded from <testEmail>): unknown user:
    "testemail"

<"<"@myServer.org> (expanded from <"<">): unknown user: "<"

```

---

<div class="post-metadata">

### Author: ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)
#### Post date: [July 23, 2019, 8:57am UTC](https://discourse.julialang.org/t/sending-emails-from-julia-using-sendmail/26672/2 "2019-07-23T08:57:09Z")

</div>

For pipes in Julia, you explicitly need to use `pipeline`, `<`, `>` and `|` don’t work in commands yet, like they do in Bash. You probably want:

```julia
sendEmailCmd = pipeline("testEmail", `sendmail -t`)
run(sendEmailCmd)

```

---

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [July 23, 2019, 9:11am UTC](https://discourse.julialang.org/t/sending-emails-from-julia-using-sendmail/26672/3 "2019-07-23T09:11:10Z")

</div>

Thank you, it works.  
Is there a way I can replace the file testEmail with a string composed in Julia ?

---

<div class="post-metadata">

### Author: ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)
#### Post date: [July 23, 2019, 9:14am UTC](https://discourse.julialang.org/t/sending-emails-from-julia-using-sendmail/26672/4 "2019-07-23T09:14:36Z")

</div>

Yes, you can use an `IOBuffer` in the `pipeline` like so:

```julia
message = """
       To: myTestAddress@gmail.com
       From: myFromAdress@myServer.org
       Subject: One-line subject

       This is the content of the email."""
sendEmailCmd = pipeline(IOBuffer(message), `sendmail -t`)
run(sendEmailCmd)

```

---

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [July 23, 2019, 9:20am UTC](https://discourse.julialang.org/t/sending-emails-from-julia-using-sendmail/26672/5 "2019-07-23T09:20:20Z")

</div>

uhg, I got a MethodError…

```julia
julia> message = """
              To: myTestAddress@gmail.com
              From: myFromAdress@myServer.org
              Subject: One-line subject

              This is the content of the email."""
"To: myTestAddress@gmail.com\nFrom: myFromAdress@myServer.org\nSubject: One-line subject\n\nThis is the content of the email."

julia> sendEmailCmd = pipeline(IOBuffer(message), `sendmail -t`)
pipeline(`sendmail -t`, stdin=IOBuffer(data=UInt8[...], readable=true, writable=false, seekable=true, append=false, size=120, maxsize=Inf, ptr=1, mark=-1))

julia> run(sendEmailCmd)
ERROR: MethodError: no method matching rawhandle(::Base.GenericIOBuffer{Array{UInt8,1}})
Closest candidates are:
  rawhandle(::RawFD) at process.jl:149
  rawhandle(::Base.DevNull) at process.jl:148
  rawhandle(::Base.Filesystem.File) at filesystem.jl:69
  ...
Stacktrace:
 [1] _jl_spawn(::String, ::Array{String,1}, ::Cmd, ::Tuple{Base.GenericIOBuffer{Array{UInt8,1}},RawFD,RawFD}) at ./process.jl:345
 [2] (::getfield(Base, Symbol("##493#494")){Cmd})(::Tuple{Base.GenericIOBuffer{Array{UInt8,1}},RawFD,RawFD}) at ./process.jl:509
 [3] setup_stdio(::getfield(Base, Symbol("##493#494")){Cmd}, ::Tuple{Base.GenericIOBuffer{Array{UInt8,1}},RawFD,RawFD}) at ./process.jl:490
 [4] #_spawn#492(::Nothing, ::Function, ::Cmd, ::Tuple{Base.GenericIOBuffer{Array{UInt8,1}},RawFD,RawFD}) at ./process.jl:508
 [5] (::getfield(Base, Symbol("#kw##_spawn")))(::NamedTuple{(:chain,),Tuple{Nothing}}, ::typeof(Base._spawn), ::Cmd, ::Tuple{Base.GenericIOBuffer{Array{UInt8,1}},RawFD,RawFD}) at ./none:0
 [6] #_spawn#489(::Nothing, ::Function, ::Base.CmdRedirect, ::Tuple{RawFD,RawFD,RawFD}) at ./process.jl:398
 [7] _spawn at ./process.jl:398 [inlined]
 [8] #run#503(::Bool, ::Function, ::Base.CmdRedirect) at ./process.jl:662
 [9] run(::Base.CmdRedirect) at ./process.jl:661
 [10] top-level scope at none:0

```

---

<div class="post-metadata">

### Author: ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)
#### Post date: [July 23, 2019, 9:38am UTC](https://discourse.julialang.org/t/sending-emails-from-julia-using-sendmail/26672/6 "2019-07-23T09:38:31Z")

</div>

That does seem odd. Could you try if

```julia
run(pipeline(IOBuffer(message), `cat`))

```

works on your machine? What is your os, maybe the output of `versioninfo()` can give some more clues. If you’re running Julia 1.0, it may be the same as [this](https://github.com/JuliaDocs/DocumentationGenerator.jl/issues/2) issue. Does

```julia
pipeline(`sendmail -t`, stdin=IOBuffer(message))

```

work?

---

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [July 23, 2019, 9:41am UTC](https://discourse.julialang.org/t/sending-emails-from-julia-using-sendmail/26672/7 "2019-07-23T09:41:53Z")

</div>

Yes… I was looking as well… I think this has to do with [this feature request](https://github.com/JuliaLang/julia/issues/15479) implemented on master [in this merge of 7 Dec 2018](https://github.com/JuliaLang/julia/pull/30278), so it should works on recent Julias, but I have 1.0.3.

`pipeline(`sendmail -t`, stdin=IOBuffer(message))` has been merged in February 20189, so it also doesn’t work with Julia 1.0… ok, I’ll update Julia on the server…

UPDATE: I can report the suggestions given by @ [simeonschaub](https://discourse.julialang.org/u/simeonschaub) (i.e. `sendEmailCmd = pipeline(IOBuffer(message), `sendmail -t`)` or `run(pipeline(IOBuffer(message), `cat`))`) works with Julia 1.1 (but not with Julia 1.0.4). Thank you again.

---

<div class="post-metadata">

### Author: ![Eric\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eric_smith/32/8088_2.png) [@Eric\_Smith](https://discourse.julialang.org/u/Eric_Smith)
#### Post date: [July 7, 2020, 2:35pm UTC](https://discourse.julialang.org/t/sending-emails-from-julia-using-sendmail/26672/8 "2020-07-07T14:35:39Z")

</div>

Hi,  
Jumping in on this thread.,  
Is there a nice way to encode a graphics file and send it as an attachment with this library?

Thanks for any pointer and best wishes  
Eric

---

<div class="post-metadata">

### Author: ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)
#### Post date: [July 7, 2020, 2:56pm UTC](https://discourse.julialang.org/t/sending-emails-from-julia-using-sendmail/26672/9 "2020-07-07T14:56:29Z")

</div>

There are multiple ways to do this proposed in this StackOverflow thread:

[https://unix.stackexchange.com/questions/223636/sendmail-attachment/223650](https://unix.stackexchange.com/questions/223636/sendmail-attachment/223650)

I would just check, if any of these proposed alternatives are installed on your system.

---

<div class="post-metadata">

### Author: ![Eric\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eric_smith/32/8088_2.png) [@Eric\_Smith](https://discourse.julialang.org/u/Eric_Smith)
#### Post date: [July 8, 2020, 7:45am UTC](https://discourse.julialang.org/t/sending-emails-from-julia-using-sendmail/26672/10 "2020-07-08T07:45:59Z")

</div>

Thanks @simeonschaub!  
Yes, I was wondering if there was a higher level way than the method referred to here.  
So will build up the base64 and separators and send with `SMTPClient.jl`  
🙂
