A few things: 1. create a savefd function, so that if you have a file open, you can call this function instead of closing and reopening it 2. argv0 incorrectly used, change it to name instead 3. don't say "we don't accept executable attachments" when it's really an internal error 4. The big one: don't free buf, since you use it for strdup below. mktemp does not do a strdup; smprint does. So the odds of trashing name, once you free buf, is pretty good.