Jump to content

Sending emails via command line: Difference between revisions

Line 66: Line 66:
===Method 1===
===Method 1===
----
----
For this first example, lets send an email containing the message "'''Hello, JustTesting'''", from the hypothetical address '''[email protected]''' to '''[email protected]''' using Gmail's SMTP settings:
This method is useful when sending short emails. As an example, lets send an email containing the message "'''Hello, JustTesting'''", from the hypothetical address '''[email protected]''' to '''[email protected]''' using Gmail's SMTP settings:


  echo -e "subject:Test\nfrom:[email protected]\nHello,\n\nJustTesting" | sendmail -v -H "exec openssl s_client -quiet -connect smtp.gmail.com:587 -tls1 -starttls smtp" -au"[email protected]" -ap"senders.email.password" [email protected]
  :~# echo -e "subject:Test\nfrom:[email protected]\nHello,\n\nJustTesting" | sendmail -v -H "exec openssl s_client -quiet -connect smtp.gmail.com:587 -tls1 -starttls smtp" -f [email protected] au"[email protected]" -ap"senders.email.password" [email protected]


Let's examine this command in detail. First, this part:
Let's examine this command in detail. First, this part:
Line 89: Line 89:
So, in short, the part beginning with '''echo''' and ending just before the column ('''|''') represents the email's header and body of text. Now lets The next part (beginning after the column):
So, in short, the part beginning with '''echo''' and ending just before the column ('''|''') represents the email's header and body of text. Now lets The next part (beginning after the column):


'''sendmail <span style=color:green>-v -H</span> "<span style=color:blue>exec openssl s_client -quiet -connect smtp.gmail.com:587 -tls1 -starttls smtp</span>" <span style=color:purple>-au"[email protected]" -ap"senders.email.password"</span> <span style=color:brown>[email protected]</span>'''
'''sendmail <span style=color:green>-v -H</span> "<span style=color:blue>exec openssl s_client -quiet -connect smtp.gmail.com:587 -tls1 -starttls smtp</span>" <span style=color:red>-f [email protected]</span> <span style=color:purple>-au"[email protected]" -ap"senders.email.password"</span> <span style=color:brown>[email protected]</span>'''


* <span style=color:green>'''-v'''</span> - verbose mode
* <span style=color:green>'''-v'''</span> - verbose mode
* <span style=color:green>'''-H'''</span> - runs connection helper; connection helper allows you to specify additional commands regarding the email (in this case, OpenSSL connection information)
* <span style=color:green>'''-H'''</span> - runs connection helper; connection helper allows you to specify additional commands regarding the email (in this case, OpenSSL connection information)
* <span style=color:blue>'''exec openssl s_client -quiet -connect smtp.gmail.com:587 -tls1 -starttls smtp'''</span> - OpenSSL connection information; <span style=color:blue>'''smtp.gmail.com:587'''</span> specifies the SMTP server and port. Replace with email service provider's relevant SMTP settings.
* <span style=color:blue>'''exec openssl s_client -quiet -connect smtp.gmail.com:587 -tls1 -starttls smtp'''</span> - OpenSSL connection information; <span style=color:blue>'''smtp.gmail.com:587'''</span> specifies the SMTP server and port. Replace with email service provider's relevant SMTP settings
* <span style=color:red>-f senders.[email protected]</span> - sender's email address. This should correspond with the '''from:''' part in the echo command
* <span style=color:purple>'''-au"[email protected]" -ap"senders.email.password"'''</span> - what follows after <span style=color:purple>'''-au'''</span> inside the quotation marks is the email service's login username and by analogy <span style=color:purple>'''-ap'''</span> specifies the email service's login password (<span style=color:purple>'''[email protected]'''</span> and <span style=color:purple>'''senders.email.password'''</span>, in this case)
* <span style=color:purple>'''-au"[email protected]" -ap"senders.email.password"'''</span> - what follows after <span style=color:purple>'''-au'''</span> inside the quotation marks is the email service's login username and by analogy <span style=color:purple>'''-ap'''</span> specifies the email service's login password (<span style=color:purple>'''[email protected]'''</span> and <span style=color:purple>'''senders.email.password'''</span>, in this case)
* <span style=color:brown>'''[email protected]'''</span> - specifies the recipient's email address
* <span style=color:brown>'''[email protected]'''</span> - specifies the recipient's email address


sendmail -v -H "exec openssl s_client -quiet -connect smtp.gma
To sump up, this part executes the connection to the SMTP server and sends out an email to the specified recipient.
il.com:587 -tls1 -starttls smtp" <mail.txt -f [email protected] -au"d
 
ziugas.matrosovas@gmail.com" -ap"pass" dziugas.matrosovas@teltonika.lt
'''Note''': don't forget switch out the given information with your own relevant data.
 
===Method 2===
----
This next method is superior when sending longer messages. Instead of using the echo command, we'll store our email header and body information into a text file. Just as in the example above, lets send an email from the hypothetical address '''[email protected]''' to '''[email protected]''' using Gmail's SMTP settings, but without using echo:
 
:~# sendmail -v -H "exec openssl s_client -quiet -connect smtp.gmail.com:587 -tls1 -starttls smtp" <span style=color:red>'''<mail.txt'''</span> -f [email protected] -au"senders.email@gmail.com" -ap"pass"[email protected]
 
 
<span style=color:red>'''<mail.txt'''</span>
subject:test
from:dziugas.matrosovas@gmail.com
Testing