Qgelm

How To Send Mail From the MacOS Terminal

Originalartikel

Backup

<html> <section class=„dh di dj dk dl“><div class=„n p“ readability=„5“><div class=„z ab ac ae af dm ah ai“ readability=„7“><p><h2 class=„aq ci ei et ek eu em ev eo ew eq ex av“>Automate email sending &#8212; no installation necessary</h2></p><div class=„ey“><div class=„n ez fa fb fc“><div class=„o n“><div><a rel=„noopener“ href=„https://medium.com/@mt_williams?source=post_page-----dad1756b166f———————-“><img alt=„Matthew Williams“ class=„r fd fe ff“ src=„https://miro.medium.com/fit/c/96/96/2*Kq_FcYPjWVrGAFLGqXneVg.jpeg“ width=„48“ height=„48“/></a></div><div class=„fg ai r“><div class=„n“><div><div class=„fh n o fi“><a class=„cm cn az ba bb bc bd be bf bg fp bj bk cq cr“ rel=„noopener“ href=„https://medium.com/@mt_williams?source=post_page-----dad1756b166f———————-“>Matthew Williams</a></div></div></div><div><a class=„cm cn az ba bb bc bd be bf bg fp bj bk cq cr“ rel=„noopener“ href=„https://medium.com/better-programming/how-to-send-mail-from-the-macos-terminal-dad1756b166f?source=post_page-----dad1756b166f———————-“>Jan 15</a> <!– –>&#183;<!– –> <!– –>5<!– –> min read<svg class=„star-15px_svg__svgIcon-use“ width=„15“ height=„15“ viewbox=„0 0 15 15“/></div></div></div></div></div></div></div><div class=„gu“><div class=„n p“><div class=„gv gw gx gy gz ha ae hb af hc ah ai“><figure class=„he hf hg hh hi gu hj hk paragraph-image“><figcaption class=„av fj ib ic id da cy cz ie if aq ci“>Photo by <a href=„https://unsplash.com/@webaroo?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText“ class=„cm gi ig ih ii ij“ target=„_blank“ rel=„noopener nofollow“>Webaroo.com.au</a> on <a href=„https://unsplash.com/s/photos/email?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText“ class=„cm gi ig ih ii ij“ target=„_blank“ rel=„noopener nofollow“>Unsplash</a></figcaption></figure></div></div></div><div class=„n p“ readability=„10“><div class=„z ab ac ae af dm ah ai“ readability=„15“><p id=„4a1d“ class=„ik il dp ar im b ei in ek io ip iq ir is it iu iv dh“>Have you ever been writing a script and wanted to receive automated emails if the script fails? Or are you new to the command line, and looking for something more exciting to do than just changing directories and listing files?</p><p id=„3c37“ class=„ik il dp ar im b ei in ek io ip iq ir is it iu iv dh“>Well, if you&#8217;re working on a Mac, you have everything you need to send an email directly from the command line to the inbox of your choice.</p></div></div></section><hr class=„iw ci ix iy iz id ja jb jc jd je“/><section class=„dh di dj dk dl“><div class=„n p“ readability=„14“><div class=„z ab ac ae af dm ah ai“ readability=„25“><p id=„6311“ class=„ik il dp ar im b ei jr ek js ip jt ir ju it jv iv dh“>Your Mac should already be set up to send email out of the box, so you don&#8217;t even need to configure anything. You just need to ensure you&#8217;re logged in to a user account, and the Mac will handle sending the email.</p><p id=„6bd1“ class=„ik il dp ar im b ei in ek io ip iq ir is it iu iv dh“>To send your first email, open up Terminal and run:</p><pre class=„he hf hg hh hi jw jx bz“>echo „Hello World“ | mail -s „Test email“ someone@example.com</pre><p id=„ab83“ class=„ik il dp ar im b ei in ek io ip iq ir is it iu iv dh“>Obviously, you&#8217;ll need to replace

kd ke kf jz b"
someone@example.com

with the actual email address you want to send the mail to.</p><p id=„b0f9“ class=„ik il dp ar im b ei in ek io ip iq ir is it iu iv dh“>After running the command above, check the account you sent the message to, and your email should have arrived. Make sure you check your spam inbox as well, as it&#8217;s likely your email provider will assume the message is unsafe.</p></div></div><div class=„gu“><div class=„n p“><div class=„gv gw gx gy gz ha ae hb af hc ah ai“><figure class=„he hf hg hh hi gu hj hk paragraph-image“><figcaption class=„av fj ib ic id da cy cz ie if aq ci“>Easy as pie</figcaption></figure></div></div></div><div class=„n p“ readability=„7“><div class=„z ab ac ae af dm ah ai“ readability=„10“><p id=„8309“ class=„ik il dp ar im b ei in ek io ip iq ir is it iu iv dh“>There you have it &#8212; that&#8217;s all it takes to get started. Now, let&#8217;s dive a little deeper into some of the commands, so we can produce more in-depth emails in the terminal.</p></div></div></section><hr class=„iw ci ix iy iz id ja jb jc jd je“/><section class=„dh di dj dk dl“><div class=„n p“ readability=„30“><div class=„z ab ac ae af dm ah ai“ readability=„60“><p id=„cdd3“ class=„ik il dp ar im b ei jr ek js ip jt ir ju it jv iv dh“>There are a number of methods we can use to create the actual body of the email. The length of your message and the way you prefer to use the command line may influence which of the following methods you end up using:</p><h2 id=„8c2e“ class=„jy jg dp ar aq jh ki kj kk kl km kn ko kp kq kr ks“>Command-line method</h2><p id=„e1aa“ class=„ik il dp ar im b ei jr ek js ip jt ir ju it jv iv dh“>The simplest way of writing the email body is to simply call

kd ke kf jz b"
mail

and type it in line by line. When there is no predefined message given to mail &#8212; it&#8217;ll default to Terminal&#8217;s standard input.</p><p id=„c8a9“ class=„ik il dp ar im b ei in ek io ip iq ir is it iu iv dh“>You can add as many lines as you like by pressing enter, and when your message is complete, you finish by pressing

kd ke kf jz b"
Ctrl + D

(yes, Control, not Command).</p><p id=„65c4“ class=„ik il dp ar im b ei in ek io ip iq ir is it iu iv dh“>The syntax to use this basic version is as follows:</p><pre class=„he hf hg hh hi jw jx bz“>mail someone@example.com</pre><p id=„dda5“ class=„ik il dp ar im b ei in ek io ip iq ir is it iu iv dh“>When using this default version, Terminal will prompt you to enter a subject line. It&#8217;s important to note that the following methods won&#8217;t prompt for a subject line, but one can still be added using an option discussed below.</p><h2 id=„1b0d“ class=„jy jg dp ar aq jh ki kj kk kl km kn ko kp kq kr ks“>Echo method</h2><p id=„3ced“ class=„ik il dp ar im b ei jr ek js ip jt ir ju it jv iv dh“>Believe it or not, if you ran the first example command, you&#8217;ve already used this method. Scroll back up, and take another look at the command. You can see it starts with

kd ke kf jz b"
echo

not

kd ke kf jz b"
mail

, and we used a pipe operator,

kd ke kf jz b"
|

, to send the message from the first part of the command into the second.</p><p id=„f532“ class=„ik il dp ar im b ei in ek io ip iq ir is it iu iv dh“>We can

kd ke kf jz b"
echo

whatever text we like and then pipe it into the email body using the

kd ke kf jz b"
mail

command:</p><pre class=„he hf hg hh hi jw jx bz“>echo „This is not the same message as before“ | mail -s „Echo test email“ someone@example.com</pre><h2 id=„16ac“ class=„jy jg dp ar aq jh ki kj kk kl km kn ko kp kq kr ks“>Cat method</h2><p id=„b32c“ class=„ik il dp ar im b ei jr ek js ip jt ir ju it jv iv dh“>Finally, since we now know we&#8217;re able to pipe results into the

kd ke kf jz b"
mail

command, we can use the

kd ke kf jz b"
cat

command to copy the contents of a file as the message. This could be really useful if you&#8217;re wanting to send error logs or something that&#8217;s already contained within a file.</p><p id=„dc64“ class=„ik il dp ar im b ei in ek io ip iq ir is it iu iv dh“>To demonstrate, firstly, let&#8217;s create a new text file with some content:</p><pre class=„he hf hg hh hi jw jx bz“>echo „This is a test file with a message to email“ &gt; file.txt</pre><p id=„6ca5“ class=„ik il dp ar im b ei in ek io ip iq ir is it iu iv dh“>After that, we&#8217;re able to pipe the results of

kd ke kf jz b"
cat

into

kd ke kf jz b"
mail

to send the file&#8217;s contents to our email:</p><pre class=„he hf hg hh hi jw jx bz“>cat file.txt | mail -s „File test email“ someone@example.com</pre><p id=„c1ec“ class=„ik il dp ar im b ei in ek io ip iq ir is it iu iv dh“>Checking the message in our inbox, we can see that the file has successfully been copied into the body of the email.</p></div></div><div class=„gu“><div class=„n p“><div class=„gv gw gx gy gz ha ae hb af hc ah ai“><figure class=„he hf hg hh hi gu hj hk paragraph-image“><figcaption class=„av fj ib ic id da cy cz ie if aq ci“>Works like a charm!</figcaption></figure></div></div></div></section><hr class=„iw ci ix iy iz id ja jb jc jd je“/><section class=„dh di dj dk dl“><div class=„n p“ readability=„12“><div class=„z ab ac ae af dm ah ai“ readability=„20“><p id=„5fce“ class=„ik il dp ar im b ei jr ek js ip jt ir ju it jv iv dh“>An important aspect of any email is the subject line. As mentioned earlier, though, the echo and cat methods don&#8217;t prompt you for a subject. For those who&#8217;ve been watching closely, you&#8217;ll have seen the email examples above all have different subject lines due to the different values we have passed into the

kd ke kf jz b"
-s

option.</p><p id=„1956“ class=„ik il dp ar im b ei in ek io ip iq ir is it iu iv dh“>This option allows us to easily create the subject line for an email, and again, we can change it to be whatever we like using the following syntax:</p><pre class=„he hf hg hh hi jw jx bz“>mail -s „Enter your subject here“ someone@example.com</pre></div></div></section><hr class=„iw ci ix iy iz id ja jb jc jd je“/><section class=„dh di dj dk dl“><div class=„n p“ readability=„14“><div class=„z ab ac ae af dm ah ai“ readability=„24“><p id=„b6b0“ class=„ik il dp ar im b ei jr ek js ip jt ir ju it jv iv dh“>Sometimes, you&#8217;ll want to include more than one email address when sending mail. Terminal&#8217;s

kd ke kf jz b"
mail

functionality allows both carbon copies (CC) and blind carbon copies (BCC) to be sent using command-line options.</p><h2 id=„b03f“ class=„jy jg dp ar aq jh ki kj kk kl km kn ko kp kq kr ks“>CC someone in</h2><p id=„032a“ class=„ik il dp ar im b ei jr ek js ip jt ir ju it jv iv dh“>In order to CC someone, add the

kd ke kf jz b"
-c

option followed by a comma-separated list of email addresses, like so:</p><pre class=„he hf hg hh hi jw jx bz“>echo „Hello World“ | mail -s „CC Testing email“ someone@example.com -c second@example.com, third@example.com</pre><h2 id=„b3a8“ class=„jy jg dp ar aq jh ki kj kk kl km kn ko kp kq kr ks“>BCC someone in</h2><p id=„19a9“ class=„ik il dp ar im b ei jr ek js ip jt ir ju it jv iv dh“>In order to BCC someone, add the

kd ke kf jz b"
-b

option followed by a comma separated list of email addresses, like so:</p><pre class=„he hf hg hh hi jw jx bz“>echo „Hello World“ | mail -s „BCC Testing email“ someone@example.com -b second@example.com, third@example.com</pre></div></div></section><hr class=„iw ci ix iy iz id ja jb jc jd je“/><section class=„dh di dj dk dl“><div class=„n p“ readability=„15“><div class=„z ab ac ae af dm ah ai“ readability=„26“><p id=„3ea8“ class=„ik il dp ar im b ei jr ek js ip jt ir ju it jv iv dh“>So there you have it: a simple way to send mail from Terminal without needing to install or configure anything. Obviously, using the command line is not the most effective way to create and send email on a regular basis, and the setup shown here has issues regarding email encryption and validation that&#8217;ll often get your messages sent to a spam inbox.</p><p id=„18a3“ class=„ik il dp ar im b ei in ek io ip iq ir is it iu iv dh“>If, however, you want to whitelist the address and send yourself some automated emails from a script on a Mac, this is a simple method to get the messages sent. Likewise, if you just enjoy trying new things on the command line for fun, this is a very easy way to get started using Terminal for more than just listing files and directories.</p></div></div></section><hr class=„iw ci ix iy iz id ja jb jc jd je“/><section class=„dh di dj dk dl“><div class=„n p“ readability=„9“><div class=„z ab ac ae af dm ah ai“ readability=„14“><p id=„4c26“ class=„ik il dp ar im b ei jr ek js ip jt ir ju it jv iv dh“>Unfortunately, there aren&#8217;t many online resources regarding the commands. You can, however, run

kd ke kf jz b"
man mail

to see what&#8217;s possible.</p><p id=„2668“ class=„ik il dp ar im b ei in ek io ip iq ir is it iu iv dh“>If you find any other resources or any exciting uses of the commands, please drop them in the comments below for others to see.</p></div></div></section> </html>

Cookies helfen bei der Bereitstellung von Inhalten. Diese Website verwendet Cookies. Mit der Nutzung der Website erklären Sie sich damit einverstanden, dass Cookies auf Ihrem Computer gespeichert werden. Außerdem bestätigen Sie, dass Sie unsere Datenschutzerklärung gelesen und verstanden haben. Wenn Sie nicht einverstanden sind, verlassen Sie die Website.Weitere Information