El seguënt document explica com descarregar automàticament els fitxers d’una bústia de correu POP3 i extreure’n els fitxers adjunts.
Hem de tenir instal·lats els següents paquets…
apt-get install mpack fetchmail procmail
Per anar bé, hem de crear un usuari, per no fer-ho des de root …
useradd -m usuaripop3
Ens convertim amb el nou usuari. Tot seguit creem i omplim els fitxers de configuració (exemple amb Gmail)…
su usuaripop3 touch /home/usuaripop3/.fetchmailrc echo>/home/usuaripop3/.fetchmailrc poll pop.gmail.com with proto pop3: echo>>/home/usuaripop3/.fetchmailrc user "bustia@domini.tld" there has password "XXXXXXX" echo>>/home/usuaripop3/.fetchmailrc is usuaripop3 here and wants ssl nokeep echo>>/home/usuaripop3/.fetchmailrc mda "/usr/bin/procmail -t /home/usuaripop3/.procmailrc" chmod 700 .fetchmailrc touch /home/usuaripop3/.procmailrc echo>/home/usuaripop3/.procmailrc MAILDIR=/home/usuaripop3/ echo>>/home/usuaripop3/.procmailrc VERBOSE=on echo>>/home/usuaripop3/.procmailrc :0 echo>>/home/usuaripop3/.procmailrc mail/
Ja ho tenim.
Comprovem si hi ha nous correus…
fetchmail -cvN fetchmail: --check mode enabled, not fetching mail fetchmail: 6.3.26 querying pop.gmail.com (protocol POP3) at Tue 29 Jan 2019 08:49:17 AM CET: poll started Trying to connect to 2a00:1450:400c:c0b::6d/995...connected. fetchmail: Server certificate: fetchmail: Issuer Organization: Google Trust Services fetchmail: Issuer CommonName: Google Internet Authority G3 fetchmail: Subject CommonName: pop.gmail.com fetchmail: Subject Alternative Name: pop.gmail.com fetchmail: pop.gmail.com key fingerprint: BC:65:CC:F1:F4:94:4C:9E:CA:3F:93:65:6A:8C:AE:5D fetchmail: SSL/TLS: using protocol TLSv1.2, cipher ECDHE-RSA-AES128-GCM-SHA256, 128/128 secret/processed bits fetchmail: POP3< +OK Gpop ready for requests from 2001:8d8:812:4c00::6c:f4be z22mb4093391lfi fetchmail: POP3> CAPA fetchmail: POP3< +OK Capability list follows fetchmail: POP3< USER fetchmail: POP3< RESP-CODES fetchmail: POP3< EXPIRE 0 fetchmail: POP3< LOGIN-DELAY 300 fetchmail: POP3< TOP fetchmail: POP3< UIDL fetchmail: POP3< X-GOOGLE-RICO fetchmail: POP3< SASL PLAIN XOAUTH2 OAUTHBEARER fetchmail: POP3< . fetchmail: POP3> USER bustia@domini.tld fetchmail: POP3< +OK send PASS fetchmail: POP3> PASS * fetchmail: POP3< +OK Welcome. fetchmail: POP3> STAT fetchmail: POP3< +OK 1 69054 fetchmail: POP3> LAST fetchmail: POP3< -ERR Not supported fetchmail: Not supported fetchmail: POP3> UIDL fetchmail: POP3< +OK fetchmail: POP3< 1 GmailId1689894f05b3e623 fetchmail: POP3< . 1 message for bustia@domini.tld at pop.gmail.com (69054 octets). fetchmail: POP3> QUIT fetchmail: POP3< +OK Farewell. fetchmail: 6.3.26 querying pop.gmail.com (protocol POP3) at Tue 29 Jan 2019 08:49:19 AM CET: poll completed fetchmail: normal termination, status 0
Veiem que tenim un email nou (1 message for bustia@domini.tld at pop.gmail.com (69054 octets)).
Provem de descarregar els nous correus…
fetchmail -vN fetchmail: 6.3.26 querying pop.gmail.com (protocol POP3) at Tue 29 Jan 2019 08:49:56 AM CET: poll started Trying to connect to 2a00:1450:400c:c0c::6d/995...connected. fetchmail: Server certificate: fetchmail: Issuer Organization: Google Trust Services fetchmail: Issuer CommonName: Google Internet Authority G3 fetchmail: Subject CommonName: pop.gmail.com fetchmail: Subject Alternative Name: pop.gmail.com fetchmail: pop.gmail.com key fingerprint: BC:65:CC:F1:F4:94:4C:9E:CA:3F:93:65:6A:8C:AE:5D fetchmail: SSL/TLS: using protocol TLSv1.2, cipher ECDHE-RSA-AES128-GCM-SHA256, 128/128 secret/processed bits fetchmail: POP3< +OK Gpop ready for requests from 2001:8d8:812:4c00::6c:f4be l124mb4547005lfe fetchmail: POP3> CAPA fetchmail: POP3< +OK Capability list follows fetchmail: POP3< USER fetchmail: POP3< RESP-CODES fetchmail: POP3< EXPIRE 0 fetchmail: POP3< LOGIN-DELAY 300 fetchmail: POP3< TOP fetchmail: POP3< UIDL fetchmail: POP3< X-GOOGLE-RICO fetchmail: POP3< SASL PLAIN XOAUTH2 OAUTHBEARER fetchmail: POP3< . fetchmail: POP3> USER bustia@domini.tld fetchmail: POP3< +OK send PASS fetchmail: POP3> PASS * fetchmail: POP3< +OK Welcome. fetchmail: POP3> STAT fetchmail: POP3< +OK 1 69054 fetchmail: POP3> LAST fetchmail: POP3< -ERR Not supported fetchmail: Not supported fetchmail: POP3> UIDL fetchmail: POP3< +OK fetchmail: POP3< 1 GmailId1689894f05b3e623 fetchmail: POP3< . 1 message for bustia@domini.tld at pop.gmail.com (69054 octets). fetchmail: POP3> LIST 1 fetchmail: POP3< +OK 1 69054 fetchmail: POP3> TOP 1 99999999 fetchmail: POP3< +OK message follows reading message bustia@domini.tld@gmail-pop.l.google.com:1 of 1 (69054 octets) #***************************************************.**********************************.************************.**************.**************.****************.*******************.*****************.******************.*************.**************.*************.*************.*************.*************.*************.*************.*************.**************.*************.*************.*************.*************.*************.*************.*************.**************.*************.*************.*************.*************.*************.*************.*************.**************.*************.*************.*************.*************.*************.*************.*************.**************.*************.*************.*************.*************.*************.*************.**************.*************.*************.*************.*************.*************.*************.*************.**************.*************.*************.*************.*************.*************.***procmail: [11658] Tue Jan 29 08:49:59 2019 procmail: Assigning "LASTFOLDER=mail/new/1548748199.11658_0.s20627470" Subject: prova Folder: mail/new/1548748199.11658_0.s20627470 68283 flushed fetchmail: POP3> DELE 1 fetchmail: POP3< +OK marked for deletion fetchmail: POP3> QUIT fetchmail: POP3< +OK Farewell. fetchmail: 6.3.26 querying pop.gmail.com (protocol POP3) at Tue 29 Jan 2019 08:49:59 AM CET: poll completed fetchmail: normal termination, status 0
Trobarem el correu a la carpeta /home/usuaripop3/mail/new
Només ens falta extreure els adjunts dels correus nous…
cd /home/usuaripop3/mail/ munpack /home/usuaripop3/mail/new/* tempdesc.txt: File exists Did not find anything to unpack from /home/usuaripop3/mail/new/1548746227.7221_0.s20627470 tempdesc.txt: File exists 337102797792-1.pdf (application/pdf)
A la carpeta /home/usuaripop3/mail/ hi tindrem els fitxers adjunts
Si convé podem crear un script que ho faci tot plegat i mogui els fitxer al destí que ens convingui. Aquest script, l’hauriem d’afegir al crontab de l’usuari per tal que s’executi periodicament…
[/code lang="bash"]crontab -e -u usuaripop3
0 6 * * * /ruta/a/l/script.sh[/code]