Git Product home page Git Product logo

Comments (3)

emersion avatar emersion commented on June 15, 2024

Please use code blocks and copy-paste the source code, instead of posting screenshots.

Can you try setting Options.DebugWriter = os.Stdout to dump the raw IMAP?

from go-imap.

aohanhongzhi avatar aohanhongzhi commented on June 15, 2024

@emersion I want get all message from inbox , but I did not find the source code about it.

* 35 FETCH (UID 17746 ENVELOPE ("Mon, 29 Apr 2024 08:08:11 -0700" "=?utf-8?B?UmU6IFtlbWVyc2lvbi9nby1pbWFwXSBGRVRDSCBjb21tYW5kIGRpZCBub3QgcmV0dXJuIGFueSBtZXNzYWdlKElzc3VlICM2MTcp?=" (("=?utf-8?B?U2ltb24gU2Vy?=" NIL "notifications" "github.com")) (("=?utf-8?B?U2ltb24gU2Vy?=" NIL "notifications" "github.com")) (("=?utf-8?B?U2ltb24gU2Vy?=" NIL "notifications" "github.com")) (("=?utf-8?B?Z28taW1hcA==?=" NIL "go-imap" "noreply.github.com")) (("=?utf-8?B?YW9oYW5ob25nemhp?=" NIL "aohanhongzhi" "qq.com")("=?utf-8?B?YXV0aG9y?=" NIL "author" "noreply.github.com")) NIL NIL "<emersion/go-imap/issues/617/[email protected]>") BODY[] {6430}
Received: from out-22.smtp.github.com (out-22.smtp.github.com [192.30.252.205])
        by newxmmxszb5-6.qq.com (NewMX) with SMTP id 20C11C33
        for <aohanhongzhi@qq.com>; Mon, 29 Apr 2024 23:08:12 +0800
X-QQ-mid: xmmxszb5-6t1714403292t56x1mfn8
Sender: noreply@github.com
X-QQ-CSender: noreply@github.com
X-QQ-XMAILINFO: OFUSm/RbiNCPwl82fSt1GA6B1exMKOfCGsyvzJsIhSmPwQHoTGezRVCijw+BYN
         Iom31FO0zppOZVWMNqpIZ5T17vOHxw9cppxpOVAvHFTbhAw3NzrQX/03wKj67I0Ukik54DHU3PtN
         zuf0+VKQWXPbXFGZfm+8s4MbMehk9v1kZG/dYAqyThLhgHLUPnTU5eYloBQapkOjbeujOOcaX/v7
         zlAmyHabb5GENnmwYhPQkJLgUciiYz8H3ZKuQV03LzPM3f9szN0J8kCe6FH8qPEejA4sOyBrNaFA
         t2Q+xMvo2Qdr6jSy4g/epFqEo69czAADV5rKuT504Ip/YSWG9d1IsgZ36M2wSAxdF8sKvE1tCHnH
         cZbi4BP1rUo/cxxZRK7w6Sq+5u+dGx1jmn7m73ztBo1NWpGNaHE/GlokWIx99A+Wfx7P9Q8aK10s
         bU7l/HpRfutQG8lFPg8KNk4k3Yu61l8OTi3a6uQblSTeDG1ki9/j1qdfXj6EwswlQZtvaaoUAZQc
         yDeF4IN9R6aQHCC5U1P51585MOgzJ4Gmn0bd7PSSXoIFMxBXjj7aekxcA6JuE1aIKNriNVev7zuk
         JeGFccnAIvmaZOlqVKtBW0KcUd45B6FYxvO+78SJYVqu/piGG/kAWaAKqDyt+NoVtldyaeR3rWMq
         758QtqkoPaAsuX/IqySe/5bd9h5D4sc3FbCc6jNghpV+r+/fl5uP31twliDHVQQ5f4+3Pjk7KIdM
         lhOxkzHCKkILNCQgo99prplkx2B3UwBGlLB+xI/OCK9rqTuGr/Qmx7jRpj0IquwWPDqoRKQbIiPF
         mEyXA1OUnu2juKFPYGT+lcKIrQSg1sHPz69xIzmEJCamz2HYgxOqwsACQ8BoZvlc/Je5JyHucqno
         ifwWSsoE+YpAbgkbJnkOgOjgWRNZgufyQBDRzGOEQU2+rR97xUKpa9CkEfS4shaGxBwxdXE/Gl9x
         YTSsVHQRDml6Okqje2v/7g5eE1d0IOanaFJgH9yf2pLsWX2u8UJT3/rhoMjRzdCbnitcbyvfG8YS
         29T4FjsaEc/IMyNLC2FDRzxVRCGSrR63sRHJIl9Bza+BUD5K8W5URbRyqVnloiPtZBJVOLnmaP7x
         H+OvpTEsWk/8HH5tf8QAQlXeL4kD0qX2BDkaSYpCjrAuNaG1aiwx0arvVU7SqndRhhEeG/s93DY1
         DPQPJSWBqBOflvpxzogZcqtQnB6ipjFtVc6faFcgXuV0bCzA==
X-QQ-XMRINFO: OD9hHCdaPRBwq3WW+NvGbIU=
Authentication-Results: mx.qq.com; spf=pass(192.30.252.205) smtp.mailfrom=<nor
         eply@github.com>; dkim=pass(signature was verified) header.d=github.com; dma
         rc=pass(p=REJECT sp=REJECT pct=100) header.from=github.com
Received: from github.com (hubbernetes-node-40c10e6.ac4-iad.github.net [10.52.201.26])
        by smtp.github.com (Postfix) with ESMTPA id D3C325E0816
        for <aohanhongzhi@qq.com>; Mon, 29 Apr 2024 08:08:11 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=github.com;
        s=pf2023; t=1714403291;
        bh=7zSITh2YdIGvNacOB9F9neeDI8txjM/pFy5j5TYygLE=;
        h=Date:From:Reply-To:To:Cc:In-Reply-To:References:Subject:List-ID:
         List-Archive:List-Post:List-Unsubscribe:List-Unsubscribe-Post:
         From;
        b=inkD1rA/APqX0dqB4+RGldP9zeApc4P60cFQe8VJ88FmOC6ckFpaNd8ClDBl3BpKX
         H/2OvdBNotUAM0whxait1lgjXJwzzFkKVjymLeTSc66Y7/YOiQxtagiyuvQJZCWbIB
         exzhlmLIpm62amnLkvs1q8G7ZlRIQUA7GCTXsMdM=
Date: Mon, 29 Apr 2024 08:08:11 -0700
From: Simon Ser <notifications@github.com>
Reply-To: emersion/go-imap <reply+AI4XFF6GNOCPPE4WDOHD3KWEHOMNXEVBNHHIOJ2PUI@reply.github.com>
To: emersion/go-imap <go-imap@noreply.github.com>
Cc: aohanhongzhi <aohanhongzhi@qq.com>, 
 Author <author@noreply.github.com>
Message-ID: <emersion/go-imap/issues/617/2082994692@github.com>
In-Reply-To: <emersion/go-imap/issues/617@github.com>
References: <emersion/go-imap/issues/617@github.com>
Subject: Re: [emersion/go-imap] FETCH command did not return any message
 (Issue #617)
Mime-Version: 1.0
Content-Type: multipart/alternative;
 boundary="--==_mimepart_662fb7dbc7580_9817fc22298d";
 charset=UTF-8
Content-Transfer-Encoding: 7bit
Precedence: list
X-GitHub-Sender: emersion
X-GitHub-Recipient: aohanhongzhi
X-GitHub-Reason: author
List-ID: emersion/go-imap <go-imap.emersion.github.com>
List-Archive: https://github.com/emersion/go-imap
List-Post: <mailto:reply+AI4XFF6GNOCPPE4WDOHD3KWEHOMNXEVBNHHIOJ2PUI@reply.github.com>
List-Unsubscribe: <mailto:unsub+AI4XFF6GNOCPPE4WDOHD3KWEHOMNXEVBNHHIOJ2PUI@reply.github.com>,
 <https://github.com/notifications/unsubscribe/one-click/AI4XFFYVBLEHE7JBIX2ZETDY7ZO5XANCNFSM6AAAAABG45IJWQ>
List-Unsubscribe-Post: List-Unsubscribe=One-Click
X-Auto-Response-Suppress: All
destinations: aohanhongzhi@qq.com
X-GitHub-Recipient-Address: aohanhongzhi@qq.com


----==_mimepart_662fb7dbc7580_9817fc22298d
Content-Type: text/plain;
 charset=UTF-8
Content-Transfer-Encoding: 7bit

Please use code blocks and copy-paste the source code, instead of posting screenshots.

Can you try setting `Options.DebugWriter = os.Stdout` to dump the raw IMAP?

-- 
Reply to this email directly or view it on GitHub:
https://github.com/emersion/go-imap/issues/617#issuecomment-2082994692
You are receiving this because you authored the thread.

Message ID: <emersion/go-imap/issues/617/2082994692@github.com>
----==_mimepart_662fb7dbc7580_9817fc22298d
Content-Type: text/html;
 charset=UTF-8
Content-Transfer-Encoding: 7bit

<p></p>
<p dir="auto">Please use code blocks and copy-paste the source code, instead of posting screenshots.</p>
<p dir="auto">Can you try setting <code class="notranslate">Options.DebugWriter = os.Stdout</code> to dump the raw IMAP?</p>

<p style="font-size:small;-webkit-text-size-adjust:none;color:#666;">&mdash;<br />Reply to this email directly, <a href="https://github.com/emersion/go-imap/issues/617#issuecomment-2082994692">view it on GitHub</a>, or <a href="https://github.com/notifications/unsubscribe-auth/AI4XFF3TKHOOIJWEH4MKM3DY7ZO5XAVCNFSM6AAAAABG45IJWSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDAOBSHE4TINRZGI">unsubscribe</a>.<br />You are receiving this because you authored the thread.<img src="https://github.com/notifications/beacon/AI4XFFYFRNYQ3ZWMG4SLQRTY7ZO5XA5CNFSM6AAAAABG45IJWSWGG33NNVSW45C7OR4XAZNMJFZXG5LFINXW23LFNZ2KUY3PNVWWK3TUL5UWJTT4E75AI.gif" height="1" width="1" alt="" /><span style="color: transparent; font-size: 0; display: none; visibility: hidden; overflow: hidden; opacity: 0; width: 0; height: 0; max-width: 0; max-height: 0; mso-hide: all">Message ID: <span>&lt;emersion/go-imap/issues/617/2082994692</span><span>@</span><span>github</span><span>.</span><span>com&gt;</span></span></p>
<script type="application/ld+json">[
{
"@context": "http://schema.org",
"@type": "EmailMessage",
"potentialAction": {
"@type": "ViewAction",
"target": "https://github.com/emersion/go-imap/issues/617#issuecomment-2082994692",
"url": "https://github.com/emersion/go-imap/issues/617#issuecomment-2082994692",
"name": "View Issue"
},
"description": "View this Issue on GitHub",
"publisher": {
"@type": "Organization",
"name": "GitHub",
"url": "https://github.com"
}
}
]</script>
----==_mimepart_662fb7dbc7580_9817fc22298d--
)
T56 OK FETCH Completed
2024-04-30 08:22:38 [INFO] Date: 2024-04-29 08:08:11 -0700 -0700 /Users/HOX4SGH/Project/Go/go-resume/mailv2.go:132
2024-04-30 08:22:38 [INFO] Subject: Re: [emersion/go-imap] FETCH command did not return any message (Issue #617) /Users/HOX4SGH/Project/Go/go-resume/mailv2.go:137
2024-04-30 08:22:38 [INFO] From: ["Simon Ser" <notifications@github.com>] /Users/HOX4SGH/Project/Go/go-resume/mailv2.go:144
T57 MOVE 35 "DONE"
* OK [COPYUID 1546245200 17746 266]
* 35 EXPUNGE
T57 OK MOVE Done
T58 FETCH 36 (UID ENVELOPE BODY[])
* 36 FETCH (UID 17748 ENVELOPE ("Mon, 29 Apr 2024 17:17:09 -0700" "=?utf-8?B?W0dpdEh1Yl0gQSB0aGlyZC1wYXJ0eSBPQXV0aCBhcHBsaWNhdGlvbiBoYXMgYmVlbiBhZGRlZCB0byB5b3VyYWNjb3VudA==?=" (("=?utf-8?B?R2l0SHVi?=" NIL "noreply" "github.com")) (("=?utf-8?B?R2l0SHVi?=" NIL "noreply" "github.com")) (("=?utf-8?B?R2l0SHVi?=" NIL "noreply" "github.com")) (("=?utf-8?B?YW9oYW5ob25nemhp?=" NIL "aohanhongzhi" "qq.com")) NIL NIL NIL "<66303885163ab_a917fc10225d@lowworker-79bdf5fdb-lntqv.mail>") BODY[] {3264}
Received: from out-17.smtp.github.com (out-17.smtp.github.com [192.30.252.200])
        by newxmmxsza11-0.qq.com (NewMX) with SMTP id 44983A98
        for <aohanhongzhi@qq.com>; Tue, 30 Apr 2024 08:17:09 +0800
X-QQ-mid: xmmxza11-0t1714436229tehkox6vg
X-QQ-XMAILINFO: M6rgrbOk+BGKpzYmujfRaGX4h229qOAfivvf34GcrsVHHXHT/2lO/uSZRlcjkn
         f2bMzsvFXTlrMa2AZEJ+SYIzdf/ufsPJ9/9jgq8GsmE9xxAfw6ATWVr0WW05/M2bJGqkC92HpqQy
         byv067BafaDpT8MFzR0IcI+sbPmvAeymP6Itozf8goYFTCKlJd51h6pX6Za7de+6Ul6em8g9ohvC
         L4qUgs1bfOILOZZJG6x1Ezh1duNWS+8TlD+hWXfsXP3W16s0PCs71id0sreBfVfNht+NX83n10gb
         n6dM8+JP2GzQ2MfhzpvN/ShVCXfdgc6cuoMJlNUy1EV/0yWPujSpSzsQYVr4pqW609IDXYPOdEgR
         V6cAjmAaEm2NM3GU09AMlYzoklfbIX8heag2iLKgRaLnDZ3wnF8ONI4MfEZ2ZrLJYfXqDJvGiWYl
         Q9MiN4tyszV6mS4k4JUyMw/cjP2Zdf5FPxGsedflDWvuqGt0xYoNPU7KUt0ncmy4dqdTLUNbVRRi
         XRspYf4tml9relA2SXvFXH+WXsGkOkudhynKcoeNef4hsOpu6NiBiYqMf9qrYGJvN4CkGcq63ZZO
         xvjSAy4JHTHmdcQP+sg02htsqaK2oEHXaIumnkwanOGDY5EWNnrp8krhisYXPQN1t/0GT9vzH4uT
         JxcsyvbciA2hTKEArnUe9lq091rMcy3NP2x/vGmPxqeNxBvtC2gZbxJuY8rkahlmR4C/f6KWS9h1
         Gu3drvYSDAzAZHUg5Q4MjjYjzNC0vFvj2DfgGS3PvUQpFiFoyoMSbh5owU3gkadJQCTRWmdi9D5/
         PFqR1zLajEjQUnAheak5pGQSzM062pfvQHB12cuX2NNVpZtKm9si5hyqOJPoY6q3z9rifQhY+kV0
         j6O8F8xllCP29oE990qeqIvCvmUR8jfYIvtRAqYS5UVLhju4IHPh7dfLICKA1FEFqP88EUZHGApL
         q+QFbUABctXD0h4JFMuFeFbhJrGlAQ6zgb0zHLgApbgJNF0fNvcGBcs9Z2wtTDf1dYzNYQzwhDOu
         TbkG7xPUTxc4N4mf5+elqconpcWKG0NX17F045EsikrSiPVZZZjzetUyMTGSQ9o902Cb3GLs985F
         0fjfoLzF4ryxKVhDU5ENMWvDei1sX3Y8c5O5UTkqsSo9XpGHrBXOvz2iZKGo2+V8HJogMG9HudVX
         rfFGTTP9MJmK2T3nChhE80EUqqM4mHp1nADG8=
X-QQ-XMRINFO: NyFYKkN4Ny6FSmKK/uo/jdU=
Authentication-Results: mx.qq.com; spf=pass(192.30.252.200) smtp.mailfrom=<nor
         eply@github.com>; dkim=pass(signature was verified) header.d=github.com; dma
         rc=pass(p=REJECT sp=REJECT pct=100) header.from=github.com
Received: from github.com (hubbernetes-node-f2ec2b3.va3-iad.github.net [10.48.220.39])
        by smtp.github.com (Postfix) with ESMTPA id 17C984E105C
        for <aohanhongzhi@qq.com>; Mon, 29 Apr 2024 17:17:09 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=github.com;
        s=pf2023; t=1714436229;
        bh=LOsKR8gdHmMekG4dceJrS4qtpspQDlBXavrN//gFSHI=;
        h=Date:From:To:Subject:From;
        b=ii8Ols3/SKIkhw5AsLL9/Hy0rGVDZZmxEK747HlHjyRmmChxpjOkAsUp48JzxDUib
         Jl15sdbMt5zSYEjm2APSDpvV6lcykKs3oLnRKZTjKzIXtZ7vziZdHGSlffyvxNs18U
         /WfVZXiK62XKqI/wjLufrAwA17x2a4Y9a443F2o0=
Date: Mon, 29 Apr 2024 17:17:09 -0700
From: GitHub <noreply@github.com>
To: aohanhongzhi <aohanhongzhi@qq.com>
Message-ID: <66303885163ab_a917fc10225d@lowworker-79bdf5fdb-lntqv.mail>
Subject: [GitHub] A third-party OAuth application has been added to your
 account
Mime-Version: 1.0
Content-Type: text/plain;
 charset=UTF-8
Content-Transfer-Encoding: 7bit
X-Auto-Response-Suppress: All

Hey aohanhongzhi!

A third-party OAuth application (Gitpod ) with user:email scopes was recently authorized to access your account.
Visit https://github.com/settings/connections/applications/484069277e293e6d2a2a for more information.

To see this and other security events for your account, visit https://github.com/settings/security-log

If you run into problems, please contact support by visiting https://github.com/contact

Thanks,
The GitHub Team

)
T58 OK FETCH Completed
2024-04-30 08:22:44 [INFO] Date: 2024-04-29 17:17:09 -0700 -0700 /Users/HOX4SGH/Project/Go/go-resume/mailv2.go:132
2024-04-30 08:22:44 [INFO] Subject: [GitHub] A third-party OAuth application has been added to your account /Users/HOX4SGH/Project/Go/go-resume/mailv2.go:137
2024-04-30 08:22:44 [INFO] From: ["GitHub" <noreply@github.com>] /Users/HOX4SGH/Project/Go/go-resume/mailv2.go:144
T59 MOVE 36 "DONE"
* OK [COPYUID 1546245200 17748 267]
* 36 EXPUNGE
T59 OK MOVE Done
T60 FETCH 37 (UID ENVELOPE BODY[])
T60 OK Mails not exist!
2024-04-30 08:22:49 [ERRO] 37 FETCH command did not return any message /Users/HOX4SGH/Project/Go/go-resume/mailv2.go:96
T61 FETCH 38 (UID ENVELOPE BODY[])
T61 OK Mails not exist!
2024-04-30 08:23:04 [ERRO] 38 FETCH command did not return any message /Users/HOX4SGH/Project/Go/go-resume/mailv2.go:96
T62 FETCH 39 (UID ENVELOPE BODY[])
T62 OK Mails not exist!
2024-04-30 08:23:19 [ERRO] 39 FETCH command did not return any message /Users/HOX4SGH/Project/Go/go-resume/mailv2.go:96
T63 FETCH 40 (UID ENVELOPE BODY[])
T63 OK Mails not exist!
2024-04-30 08:23:34 [ERRO] 40 FETCH command did not return any message /Users/HOX4SGH/Project/Go/go-resume/mailv2.go:96
T64 FETCH 41 (UID ENVELOPE BODY[])
T64 OK Mails not exist!
2024-04-30 08:23:49 [ERRO] 41 FETCH command did not return any message /Users/HOX4SGH/Project/Go/go-resume/mailv2.go:96
T65 FETCH 42 (UID ENVELOPE BODY[])
T65 OK Mails not exist!
2024-04-30 08:24:04 [ERRO] 42 FETCH command did not return any message /Users/HOX4SGH/Project/Go/go-resume/mailv2.go:96
T66 FETCH 43 (UID ENVELOPE BODY[])
T66 OK Mails not exist!
2024-04-30 08:24:19 [ERRO] 43 FETCH command did not return any message /Users/HOX4SGH/Project/Go/go-resume/mailv2.go:96
T67 FETCH 44 (UID ENVELOPE BODY[])
T67 OK Mails not exist!
2024-04-30 08:24:34 [ERRO] 44 FETCH command did not return any message /Users/HOX4SGH/Project/Go/go-resume/mailv2.go:96
T68 FETCH 45 (UID ENVELOPE BODY[])
T68 OK Mails not exist!
2024-04-30 08:24:49 [ERRO] 45 FETCH command did not return any message /Users/HOX4SGH/Project/Go/go-resume/mailv2.go:96
T69 FETCH 46 (UID ENVELOPE BODY[])
T69 OK Mails not exist!
2024-04-30 08:25:04 [ERRO] 46 FETCH command did not return any message /Users/HOX4SGH/Project/Go/go-resume/mailv2.go:96
T70 FETCH 47 (UID ENVELOPE BODY[])
T70 OK Mails not exist!
2024-04-30 08:25:19 [ERRO] 47 FETCH command did not return any message /Users/HOX4SGH/Project/Go/go-resume/mailv2.go:96
Exiting.

my code

package main

import (
	"crypto/tls"
	"encoding/csv"
	"os"
	"regexp"
	"strings"
	"time"

	nested "github.com/aohanhongzhi/nested-logrus-formatter"
	log "github.com/sirupsen/logrus"

	"io"

	"github.com/emersion/go-imap/v2"
	"github.com/emersion/go-imap/v2/imapclient"
	"github.com/emersion/go-message/mail"
)

func GetEmailCSV() {
	File, err := os.OpenFile("Resume12.csv", os.O_RDWR|os.O_APPEND|os.O_CREATE, 0666)
	if err != nil {
		log.Error(err)
	}
	defer File.Close()
	//创建写入接口
	WriterCsv := csv.NewWriter(File)

	nested.LogInit(false)
	c, err := imapclient.DialTLS("imap.qq.com:993", &imapclient.Options{
		TLSConfig: &tls.Config{
			InsecureSkipVerify: true,
		},
		DebugWriter: os.Stdout,
	})
	if err != nil {
		log.Fatalf("failed to dial IMAP server: %v", err)
	}
	defer c.Close()

	if err := c.Login("[email protected]", "password, I can send to your email").Wait(); err != nil {
		log.Fatalf("failed to login: %v", err)
	}

	// 这里新建文件夹是有效的。 处理过的邮件,可以分类下。就好了。这样没有处理的就会在INBOX里。
	//if err := c.Create("DONE", nil).Wait(); err != nil {
	//	t.Fatalf("Create() = %v", err)
	//}

	mailboxes, err := c.List("", "%", nil).Collect()
	if err != nil {
		log.Fatalf("failed to list mailboxes: %v", err)
	}
	log.Printf("Found %v mailboxes", len(mailboxes))
	for _, mbox := range mailboxes {
		log.Printf(" - %v", mbox.Mailbox)
	}

	mailBox := "INBOX"
	selectedMbox, err := c.Select(mailBox, nil).Wait()
	if err != nil {
		log.Fatalf("failed to select INBOX: %v", err)
	}
	log.Printf("%v contains %v messages", mailBox, selectedMbox.NumMessages)

	if selectedMbox.NumMessages > 0 {
		// 只读取没有读取过的邮件
		total := int(selectedMbox.NumMessages)
		for i := 1; i <= total; i++ {
			email(total, i, c, WriterCsv)
			time.Sleep(5 * time.Second)
			if i%30 == 0 {
				time.Sleep(10 * time.Second)
			}
		}
	}

	if err := c.Logout().Wait(); err != nil {
		log.Fatalf("failed to logout: %v", err)
	}
}

func email(total, i int, c *imapclient.Client, WriterCsv *csv.Writer) {

	seqSet := imap.SeqSetNum(uint32(i))
	fetchOptions := &imap.FetchOptions{Envelope: true,
		UID:         true,
		BodySection: []*imap.FetchItemBodySection{{}}}
	fetchCmd := c.Fetch(seqSet, fetchOptions)
	defer fetchCmd.Close()

	//  这里会卡住。因为上面defer  fetchCmd.Close() 没有正确关闭,所以单独提取成一个方法,方法执行完成也就defer执行了。这里就不会卡主了
	msg := fetchCmd.Next()
	if msg == nil {
		log.Errorf("%v FETCH command did not return any message", i)
		time.Sleep(10 * time.Second)
		return
	}

	// Find the body section in the response
	var bodySection imapclient.FetchItemDataBodySection
	ok := false
	for {
		item := msg.Next()
		if item == nil {
			break
		}
		bodySection, ok = item.(imapclient.FetchItemDataBodySection)
		if ok {
			break
		}
		time.Sleep(200 * time.Millisecond)
	}
	if !ok {
		log.Fatalf("FETCH command did not return body section")
	}

	// Read the message via the go-message library
	mr, err := mail.CreateReader(bodySection.Literal)
	if err != nil {
		log.Fatalf("failed to create mail reader: %v", err)
	}

	// Print a few header fields
	var dataString string
	h := mr.Header
	if date, err := h.Date(); err != nil {
		log.Printf("failed to parse Date header field: %v", err)
	} else {
		dataString = date.Format("2006-01-02 15:04:05")
		log.Printf("Date: %v", date)
	}
	if subject, err := h.Text("Subject"); err != nil {
		log.Printf("failed to parse Subject header field: %v", err)
	} else {
		log.Printf("Subject: %v", subject)
	}
	if from, err := h.AddressList("From"); err != nil {
		log.Printf("failed to parse To header field: %v", err)
	} else {
		address := from[0].Address
		if !strings.Contains(address, "[email protected]") {
			log.Printf("From: %v", from)
			c.Move(seqSet, "DONE")
			if strings.Contains(address, "") {

			}
			//fetchCmd.Close()
			return
		}
	}

	// Process the message's parts
	for {
		p, err := mr.NextPart()
		if err == io.EOF {
			break
		} else if err != nil {
			log.Fatalf("failed to read message part: %v", err)
		}

		switch h := p.Header.(type) {
		case *mail.InlineHeader:
			// This is the message's text (can be plain-text or HTML)
			b, _ := io.ReadAll(p.Body)
			htmlContent := string(b)
			//			log.Printf("====>Inline text: %v", string(htmlContent))
			// 将信息解析后写入数据库, 1. 按照学校,毕业时间,分别处理。 检索简历里面是否含有关键字,进一步分析和处理。 关键字匹配系统。
			//	学校建立白名单,非白名单的直接删除。
			// 自动给符合条件的发送邮件或者短信,填写问卷,收集问卷再筛选符合条件的同学。

			// 使用正则表达式解析 HTML 内容,提取所需字段
			re := regexp.MustCompile(`(?s)font-size:\s*20px;.*?>(.*?)<\/td>`)
			match := re.FindStringSubmatch(htmlContent)
			var userAllInfo []string
			var userInfo string
			if len(match) > 1 {
				userInfo = match[1]
				log.Printf("%v->%v 字段值:%v", total, i, match[1])

				userInfoArray := strings.Split(userInfo, "|")
				userAllInfo = append(userInfoArray, dataString)
				if strings.Trim(userInfoArray[1], " ") == "男" || strings.Trim(userInfoArray[3], " ") == "大专" {
					c.Move(seqSet, "Resume")
				}
			} else {
				log.Println("未找到匹配的字段")
			}

			re1 := regexp.MustCompile(`(?s)href="([^"]*)"`)
			match1 := re1.FindStringSubmatch(htmlContent)
			var url string
			if len(match1) > 1 {
				url = match1[1]
				log.Debugf("URL:%v", url)
			} else {
				log.Warn("未找到匹配的 URL")
			}

			err2 := WriterCsv.Write(append(userAllInfo, url))
			if err2 != nil {
				log.Error("WriterCsv写入文件失败")
			}
			WriterCsv.Flush() //刷新,不刷新是无法写入的

		case *mail.AttachmentHeader:
			// This is an attachment
			filename, _ := h.Filename()
			log.Printf("Attachment: %v", filename)
		}
	}

	if err := fetchCmd.Close(); err != nil {
		log.Fatalf("FETCH command failed: %v", err)
	}
}

from go-imap.

emersion avatar emersion commented on June 15, 2024

As you can see in the logs, the server replies with zero messages. This happens because you're moving messages. The sequence numbers change when messages are moved around (see the EXPUNGE notifications in the logs). For instance, when moving message 35 to another mailbox, the sequence number is decremented for all messages with a sequence number ≥ 36. You can use UIDs instead of sequence numbers if you want an identifier which doesn't change.

Closing since this isn't a go-imap bug.

from go-imap.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.