How to Set automatic sending and receiving of emails using linux scripts

Source: Internet
Author: User

1. Send mail in Command Line Mode
1.1 install sendemail
2.2 Use sendemail and Examples

2. receive emails in Command Line Mode
2.1 install getmail4
2.2 configure getmail4 and a simple example
2.3 use munpack to extract attachments from emails

1. Send mail in Command Line Mode

1.1 install sendemail
In Ubuntu, you can search for the installation by using the new software package manager, or run the following command on the terminal:
Sudo apt-get install sendemail
We recommend that you install the other two packages before installation: libio-socket-ssl-perl and libnet-ssleay-perl.

2.2 Use sendemail and Examples

If you want to use your mailbox christ@gmail.com to send a mail to the buddha@qq.com, enter in the terminal:
Sendemail-s smtp.gmail.com-f christ@gmail.com-t buddha@qq.com-u hello-m "A hello from Christans to buddhists via gmail"-xu christ-xp password-o tls = auto

Explanation:
-S smtp.gmail.com specifies the server domain name. Mail is generally sent through the SMTP protocol. Its Domain Name is generally smtp. ***. com. For example, if the server of QQ mailbox is smtp.qq.com, and the server of 163 mailbox is smtp.163.com
-F christ@gmail.com specifies the mail address
-T buddha@qq.com specifies the destination mailbox address
-U hello mail title
-M "A hello from Christans to buddhists via gmail" is the mail main text. A longer main text can first exist in the main file. If it is not named mail.txt, use-o message-file=mail.txt.
-Xu christ specifies the mailbox user name, that is, the mailbox address before @
-Xp password: Specifies the password of the email Sending address.
-O tls = auto encryption is automatically selected in none, tls, and ssl.
If you want to paste the attachment in the email
-A attachment_file1 attachment_file2 attachment_file3

2. receive emails in Command Line Mode

There are two main email receiving methods through the mail client: POP3 and IMAP. The mail client downloads emails from the server through POP3, but IMAP only downloads the subject of the email by default. It is more suitable to use POP3 because of the significance of command line Automation.

2.1 install getmail4

In Ubuntu, you can use the new software package manager to search for the installation, or
Sudo apt-get install getmail4

2.2 configure getmail4 and a simple example

Run the following command on the terminal after installation.Copy codeThe Code is as follows: cd ~
Mkdir. getmail
Cd. getmail
Mkdir maildir
Cd maildir
Mkdir new cur tmp

After. getmail/to create a configuration file, if this configuration file is configured for the mailbox to receive buddha@qq.com, you may wish to name it getmailrc. buddha, and then edit the file, # followed by a comment:

Copy codeThe Code is as follows: # This is a configuration file for the buddha@qq.com
[Retriever]
Type = SimplePOP3Retriever
Server = pop.qq.com # change to pop.gmail.com if gmail is used.
Username = budda
Password = password

[Destination]
Type = Maildir
Path = ~ /. Getmail/maildir/# is just in ~ /. Getmail/The Created directory. Note that there must be three subdirectories: new, cur, and tmp.

[Options]
Read_all = False # Only emails not previously received are accepted. If it is changed to True, all emails in the mailbox are received.
Delete = False # The downloaded email is not deleted on the server. If it is changed to True, the email is deleted.

# Configuration file ends here
After editing, run the script on the terminal:
Getmail -- rcfile = getmailrc. buddha

Getmail will automatically receive emails, and the downloaded emails will be saved in ~ /. Getmail/maildir/new.

2.3 use munpack to extract attachments from emails

The mail body and attachments are stored as a whole file. attachments are attached to the whole file in MIME format and must be extracted by a program.
In Ubuntu, you can use the new package manager to search for and install mpack, or sudo apt-get install mpack.
Run: munpack mail_file on the terminal
The program automatically identifies and extracts attachments.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.