-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimap_to_obsidian.py
More file actions
835 lines (696 loc) · 30.1 KB
/
imap_to_obsidian.py
File metadata and controls
835 lines (696 loc) · 30.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
#!/usr/bin/env python3
"""
IMAP to Obsidian Email Importer
This script connects to an IMAP server, searches for emails matching
specific keywords, and imports them as markdown files into an Obsidian vault.
Usage:
uv run imap_to_obsidian.py # Run import
uv run imap_to_obsidian.py --test # Test connection and auth
"""
import imaplib
import email
from email.header import decode_header
from datetime import datetime
import os
import re
import sys
import argparse
from pathlib import Path
import html2text
# =============================================================================
# CONFIGURATION - Update these paths
# =============================================================================
VAULT_ROOT = Path(os.getenv("OBSIDIAN_VAULT", "/home/nico/ObsidiMax"))
CONFIG_FILE_DEFAULT = VAULT_ROOT / "Email_import_config.md"
def sanitize_for_table(text):
"""Sanitize text for markdown table cells."""
if not text:
return ""
# Escape pipes (they break tables)
text = text.replace("|", "|")
# Remove newlines and carriage returns
text = text.replace("\n", " ").replace("\r", " ")
# Collapse multiple spaces
text = re.sub(r"\s+", " ", text)
# Limit length to prevent huge table cells
if len(text) > 100:
text = text[:97] + "..."
return text.strip()
def load_config(config_path=None):
"""Load configuration from the specified config file."""
if config_path is None:
config_path = CONFIG_FILE_DEFAULT
else:
config_path = Path(config_path)
config = {
"email_user": "",
"email_password": "",
"imap_server": "",
"imap_port": 993,
"target_folders": [],
"keywords": [],
"blacklist": [],
"import_folder": "",
"daily_note_folder": "",
"processed_folder": "",
"dry_run": False,
}
if not config_path.exists():
raise FileNotFoundError(f"Config file not found: {config_path}")
with open(config_path, "r") as f:
content = f.read()
# Parse YAML frontmatter (between --- markers)
yaml_match = re.search(r"^---\n(.*?)\n---", content, re.DOTALL)
if not yaml_match:
raise ValueError("No YAML frontmatter found in config file")
yaml_content = yaml_match.group(1)
# Parse key-value pairs
for line in yaml_content.split("\n"):
if ":" in line and not line.strip().startswith("#"):
key, value = line.split(":", 1)
key = key.strip()
value = value.strip().strip('"').strip("'")
# Handle lists
if key in config:
if isinstance(config[key], list):
# Continue accumulating list items
pass
config[key] = []
# Parse list items
current_list = None
for line in yaml_content.split("\n"):
line = line.strip()
if line.endswith(":"):
current_list = line.rstrip(":")
elif line.startswith("- ") and current_list:
value = line[2:].strip().strip('"').strip("'")
if current_list in config:
config[current_list].append(value)
# Parse scalar values
scalar_keys = ["email_user", "email_password", "imap_server", "imap_port", "import_folder",
"daily_note_folder", "processed_folder"]
for key in scalar_keys:
match = re.search(rf"{key}:\s*[\"']?([^\"'\n]+)[\"']?", yaml_content)
if match:
value = match.group(1).strip().strip('"').strip("'")
if key == "imap_port":
config[key] = int(value)
else:
config[key] = value
# Parse dry_run boolean
dry_run_match = re.search(r"dry_run:\s*(true|false)", yaml_content, re.IGNORECASE)
if dry_run_match:
config["dry_run"] = dry_run_match.group(1).lower() == "true"
return config
def decode_email_header(header):
"""Decode email header, handling various encodings."""
if not header:
return ""
decoded_parts = []
for part, encoding in decode_header(header):
if isinstance(part, bytes):
try:
decoded_parts.append(part.decode(encoding or "utf-8", errors="ignore"))
except (LookupError, TypeError):
decoded_parts.append(part.decode("utf-8", errors="ignore"))
else:
decoded_parts.append(str(part))
return "".join(decoded_parts)
def sanitize_filename(name):
"""Sanitize string for use as filename."""
# Remove or replace invalid characters
invalid_chars = '<>:"/\\|?*'
for char in invalid_chars:
name = name.replace(char, "-")
# Remove leading/trailing spaces and dots
name = name.strip(". ")
# Limit length
if len(name) > 200:
name = name[:200]
return name or "untitled"
def matches_blacklist(subject, body, blacklist):
"""Check if email matches any blacklist keywords."""
if not blacklist:
return False
subject_lower = subject.lower()
body_lower = body.lower()
for keyword in blacklist:
keyword_lower = keyword.lower()
if keyword_lower in subject_lower or keyword_lower in body_lower:
return True
return False
def create_markdown(email_msg, config):
"""Create markdown content from email message. Returns (markdown, filename_sanitized, date_obj, attachments)."""
# Extract email data
subject = decode_email_header(email_msg.get("Subject", "(No Subject)"))
from_addr = decode_email_header(email_msg.get("From", "Unknown"))
date_str = email_msg.get("Date", "")
to_addr = decode_email_header(email_msg.get("To", ""))
cc_addr = decode_email_header(email_msg.get("Cc", ""))
attachments = [] # List of (filename, content) tuples
# Parse date
try:
email_date = email.utils.parsedate_tz(date_str)
if email_date:
timestamp = email.utils.mktime_tz(email_date)
date_obj = datetime.fromtimestamp(timestamp)
else:
date_obj = datetime.now()
except:
date_obj = datetime.now()
date_formatted = date_obj.strftime("%Y-%m-%d %H:%M")
# Extract body - prefer HTML and convert to Markdown
body = ""
html_converter = html2text.HTML2Text()
html_converter.ignore_links = False
html_converter.ignore_images = False
html_converter.body_width = 0 # Don't wrap lines
html_converter.unicode_snob = True
html_converter.skip_internal_links = False
if email_msg.is_multipart():
# Walk through all parts, preferring HTML
plain_text = ""
html_content = ""
for part in email_msg.walk():
content_type = part.get_content_type()
content_disposition = str(part.get("Content-Disposition", ""))
# Handle attachments
if "attachment" in content_disposition or content_type not in ["text/plain", "text/html"]:
filename = part.get_filename()
if filename:
filename = decode_email_header(filename)
try:
payload = part.get_payload(decode=True)
if payload:
attachments.append((filename, payload))
except:
pass
# Continue to process other parts, but don't try to decode as text
if content_type not in ["text/plain", "text/html"]:
continue
try:
charset = part.get_content_charset() or "utf-8"
payload = part.get_payload(decode=True)
if payload:
decoded = payload.decode(charset, errors="ignore")
if content_type == "text/plain":
plain_text = decoded
elif content_type == "text/html":
html_content = decoded
except:
pass
# Prefer HTML if available, convert to Markdown
if html_content:
body = html_converter.handle(html_content)
elif plain_text:
body = plain_text
else:
# Non-multipart email
try:
charset = email_msg.get_content_charset() or "utf-8"
payload = email_msg.get_payload(decode=True)
if payload:
decoded = payload.decode(charset, errors="ignore")
if email_msg.get_content_type() == "text/html":
body = html_converter.handle(decoded)
else:
body = decoded
else:
body = str(email_msg.get_payload())
except:
body = str(email_msg.get_payload())
# Create markdown
attachment_section = ""
if attachments:
attachment_section = "\n**Attachments:**\n\n"
for i, (att_filename, _) in enumerate(attachments, 1):
# Attachments will be saved in a subfolder
attachment_section += f"{i}. [{att_filename}](attachments/{att_filename})\n"
markdown = f"""---
type: email_import
imported_at: {datetime.now().strftime("%Y-%m-%d %H:%M:%S")}
---
# {subject}
**From:** {from_addr}
**To:** {to_addr}
{"**Cc:** " + cc_addr if cc_addr else ""}
**Date:** {date_formatted}
{attachment_section}
---
{body}
"""
return markdown, sanitize_filename(subject), date_obj, attachments
def write_daily_log(config, stats):
"""Write summary to daily note and details to monthly log."""
daily_folder = VAULT_ROOT / config['daily_note_folder']
daily_folder.mkdir(parents=True, exist_ok=True)
# Use today's date for the daily note
today = stats['end_time'].strftime("%Y-%m-%d")
daily_note_path = daily_folder / f"{today}.md"
# Monthly log file
month_year = stats['end_time'].strftime("%Y-%m")
monthly_log_path = daily_folder / f"imap-import-{month_year}.md"
timestamp = stats['end_time'].strftime("%H:%M")
num_imported = len(stats['imported'])
num_blacklisted = len(stats.get('blacklisted', []))
# === MONTHLY LOG (detailed) ===
# Create or append to monthly log
monthly_entry = f"""### {today} {timestamp}
| | | |
|---|---|---|
| **Emails found** | {stats.get('total_found', 0)} | |
| **Imported** | {num_imported} | |
| **Blacklisted** | {num_blacklisted} | |
"""
if num_imported > 0:
monthly_entry += "**Imported:**\n\n| # | Date | Subject | From | File |\n|---|---|---|---|---|\n"
for i, email in enumerate(stats['imported'], 1):
relative_path = f"../{config['import_folder']}/{email['file']}"
monthly_entry += f"| {i} | {email['date']} | [{sanitize_for_table(email['subject'])}]({relative_path}) | {sanitize_for_table(email['from'])} | [{sanitize_for_table(email['file'])}]({relative_path}) |\n"
if num_blacklisted > 0:
monthly_entry += "\n**Blacklisted:**\n\n| # | Date | Subject | From |\n|---|---|---|---|\n"
for i, email in enumerate(stats.get('blacklisted', []), 1):
monthly_entry += f"| {i} | {email['date']} | {sanitize_for_table(email['subject'])} | {sanitize_for_table(email['from'])} |\n"
# Write to monthly log
if monthly_log_path.exists():
with open(monthly_log_path, 'r', encoding='utf-8') as f:
monthly_content = f.read()
# Append new entry
monthly_content += monthly_entry
else:
# Create new monthly log with header
monthly_content = f"""# {month_year} - Email Import Log
[[Email_import_config]] - Configuration
---
"""
monthly_content += monthly_entry
with open(monthly_log_path, 'w', encoding='utf-8') as f:
f.write(monthly_content)
# === DAILY NOTE (minimal) ===
# Check if daily note exists
existing_content = ""
if daily_note_path.exists():
with open(daily_note_path, 'r', encoding='utf-8') as f:
existing_content = f.read()
# Build minimal daily entry
summary = f"📧 {num_imported} imported"
if num_blacklisted > 0:
summary += f", {num_blacklisted} blocked"
daily_entry = f"- [{timestamp}] {summary} - [[imap-import-{month_year}#---{today.replace('-', '')}]]\n"
# Append to daily note
if existing_content:
# Find the position to insert (before any existing entries for today, or at end)
if "## 📧" in existing_content:
# Add to existing section
new_content = existing_content.rstrip() + "\n" + daily_entry
else:
# Create new section at the end
new_content = existing_content.rstrip() + f"\n\n## 📧 Email Imports\n\n" + daily_entry
else:
# Create new daily note
new_content = f"""# {today}
## 📧 Email Imports
""" + daily_entry
# Write the daily note
with open(daily_note_path, 'w', encoding='utf-8') as f:
f.write(new_content)
print(f"\n✓ Logs updated:")
print(f" Daily: {daily_note_path}")
print(f" Monthly: {monthly_log_path}")
def import_emails(config):
"""Main function to import emails from IMAP to Obsidian."""
# Check dry-run mode
dry_run = config.get("dry_run", False)
if dry_run:
print("\n" + "=" * 60)
print("🔍 DRY RUN MODE - No files will be created, no emails will be moved")
print("=" * 60 + "\n")
# Track import statistics
import_stats = {
'total_found': 0,
'imported': [],
'blacklisted': [],
'skipped': [],
'failed': [],
'start_time': datetime.now(),
}
# Get blacklist from config
blacklist = config.get('blacklist', [])
if blacklist:
print(f"🚫 Blacklist active: {', '.join(blacklist)}")
# Get password from config
email_password = config.get("email_password", "")
if not email_password:
raise ValueError("email_password not set in config file")
# Connect to IMAP server
print(f"Connecting to {config['imap_server']}:{config['imap_port']}...")
mail = imaplib.IMAP4_SSL(config['imap_server'], config['imap_port'])
try:
mail.login(config['email_user'], email_password)
print("✓ Logged in successfully")
except imaplib.IMAP4.error as e:
error_msg = str(e)
print(f"✗ Login failed: {error_msg}")
# Provide helpful diagnostics
print("\n" + "=" * 60)
print("AUTHENTICATION TROUBLESHOOTING")
print("=" * 60)
if "AUTHENTICATIONFAILED" in error_msg or "Authentication failed" in error_msg:
print("\n❌ Authentication failed - Possible causes:")
print(f" 1. Wrong password for {config['email_user']}")
print(f" 2. Wrong username format (try just the username part, not full email)")
print(f" 3. Account requires app-specific password")
print(f" 4. IMAP is not enabled for this account")
print(f" 5. Server requires different authentication method")
elif "NO" in error_msg or "cannot" in error_msg.lower():
print("\n❌ Server rejected the request:")
print(f" 1. Check if the username is correct")
print(f" 2. Some servers use just the username (e.g., 'nico') not the full email")
print(f"\n📋 Your current settings:")
print(f" Server: {config['imap_server']}:{config['imap_port']}")
print(f" Username: {config['email_user']}")
print(f" Password length: {len(email_password)} characters")
print(f"\n💡 Tips:")
print(f" • For goneo/goneo: Check your control panel for correct username")
print(f" • For Gmail: Use an App Password, not your regular password")
print(f" • For some providers: Username might be just 'nico' not 'nico@...'")
print("=" * 60)
return
# Setup output folder
output_path = VAULT_ROOT / config['import_folder']
output_path.mkdir(parents=True, exist_ok=True)
print(f"✓ Output folder: {output_path}")
# Process each target folder
for folder_name in config['target_folders']:
print(f"\n--- Processing folder: {folder_name} ---")
try:
mail.select(folder_name)
except imaplib.IMAP4.error as e:
print(f"✗ Cannot select folder '{folder_name}': {e}")
continue
# Build search criteria - search for each keyword separately and combine results
all_email_ids = set()
print(f"Searching for: {', '.join(config['keywords'])}")
for keyword in config['keywords']:
# Search in SUBJECT
try:
status, messages = mail.search(None, f'SUBJECT "{keyword}"')
if status == "OK":
all_email_ids.update(messages[0].split())
except imaplib.IMAP4.error as e:
print(f" ✗ Subject search for '{keyword}' failed: {e}")
# Search in BODY
try:
status, messages = mail.search(None, f'BODY "{keyword}"')
if status == "OK":
all_email_ids.update(messages[0].split())
except imaplib.IMAP4.error as e:
print(f" ✗ Body search for '{keyword}' failed: {e}")
email_ids = sorted(all_email_ids, key=lambda x: int(x))
print(f"Found {len(email_ids)} matching emails")
import_stats['total_found'] += len(email_ids)
# Track which emails are actually imported (not blacklisted)
imported_email_ids = []
if not email_ids:
continue
# Process each email
for msg_id in email_ids:
try:
_, msg_data = mail.fetch(msg_id, "(RFC822)")
except imaplib.IMAP4.error as e:
print(f"✗ Failed to fetch email {msg_id}: {e}")
continue
for response_part in msg_data:
if isinstance(response_part, tuple):
msg = email.message_from_bytes(response_part[1])
# Get sender and subject for display
from_addr = decode_email_header(msg.get("From", "Unknown"))
subject = decode_email_header(msg.get("Subject", "(No Subject)"))
date_str = msg.get("Date", "")
# Parse date first (needed for logging)
try:
email_date = email.utils.parsedate_tz(date_str)
if email_date:
timestamp = email.utils.mktime_tz(email_date)
date_obj = datetime.fromtimestamp(timestamp)
else:
date_obj = datetime.now()
except:
date_obj = datetime.now()
date_formatted = date_obj.strftime("%Y-%m-%d")
# Extract body for blacklist checking
body_text = ""
try:
if msg.is_multipart():
for part in msg.walk():
if part.get_content_type() == "text/plain" and "attachment" not in str(part.get("Content-Disposition", "")):
charset = part.get_content_charset() or "utf-8"
payload = part.get_payload(decode=True)
if payload:
body_text = payload.decode(charset, errors="ignore")
break
else:
if msg.get_content_type() == "text/plain":
charset = msg.get_content_charset() or "utf-8"
payload = msg.get_payload(decode=True)
if payload:
body_text = payload.decode(charset, errors="ignore")
except:
pass
# Check blacklist
if matches_blacklist(subject, body_text, blacklist):
print(f"\n 🚫 Blacklisted: {subject}")
print(f" From: {from_addr}")
import_stats['blacklisted'].append({
'from': from_addr,
'subject': subject,
'date': date_formatted,
})
continue
markdown, filename_sanitized, date_obj, attachments = create_markdown(msg, config)
# Generate filename
timestamp = date_obj.strftime("%Y%m%d-%H%M%S")
filename = f"{timestamp}_{filename_sanitized}.md"
filepath = output_path / filename
# Create attachments subfolder if needed
attachments_dir = output_path / "attachments"
if attachments and not dry_run:
attachments_dir.mkdir(parents=True, exist_ok=True)
# Display and write file
if dry_run:
print(f"\n 📧 {date_formatted}")
print(f" From: {from_addr}")
print(f" Subject: {subject}")
if attachments:
print(f" 📎 {len(attachments)} attachment(s): {', '.join(a[0] for a in attachments)}")
print(f" → {filepath}")
import_stats['imported'].append({
'from': from_addr,
'subject': subject,
'date': date_formatted,
'file': filename,
})
imported_email_ids.append(msg_id)
else:
with open(filepath, "w", encoding="utf-8") as f:
f.write(markdown)
# Save attachments
for att_filename, att_content in attachments:
att_path = attachments_dir / att_filename
with open(att_path, "wb") as att_file:
att_file.write(att_content)
print(f" 📎 Saved: {att_filename}")
print(f" ✓ Imported: {filename}")
import_stats['imported'].append({
'from': from_addr,
'subject': subject,
'date': date_formatted,
'file': filename,
})
imported_email_ids.append(msg_id)
# Move processed emails to processed folder
if config['processed_folder']:
if dry_run:
print(f" [DRY RUN] Would move {len(imported_email_ids)} emails to {config['processed_folder']}")
else:
try:
# Create processed folder if it doesn't exist
try:
mail.create(config['processed_folder'])
except:
pass # Folder may already exist
# Move only imported emails (not blacklisted ones)
for msg_id in imported_email_ids:
mail.copy(msg_id, config['processed_folder'])
# Mark for deletion in current folder
for msg_id in imported_email_ids:
mail.store(msg_id, '+FLAGS', '\\Deleted')
mail.expunge()
print(f"✓ Moved {len(imported_email_ids)} emails to {config['processed_folder']}")
except imaplib.IMAP4.error as e:
print(f"✗ Failed to move emails: {e}")
print(" (Emails were imported but not moved)")
# Write daily log note
import_stats['end_time'] = datetime.now()
if not dry_run and config.get('daily_note_folder'):
write_daily_log(config, import_stats)
# Cleanup
try:
mail.close()
mail.logout()
print("\n✓ Disconnected from server")
except:
pass
def test_connection(config):
"""Test IMAP connection with detailed diagnostics."""
import getpass
import socket
print("\n" + "=" * 60)
print("🔍 CONNECTION DIAGNOSTICS")
print("=" * 60)
server = config['imap_server']
port = config['imap_port']
# Test 1: DNS resolution
print(f"\n1. Testing DNS resolution for {server}...")
try:
ip = socket.gethostbyname(server)
print(f" ✓ DNS resolved: {server} → {ip}")
except socket.gaierror as e:
print(f" ✗ DNS resolution failed: {e}")
print(f" → Check if the server name is correct")
return False
# Test 2: TCP connection
print(f"\n2. Testing TCP connection to {server}:{port}...")
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(5)
result = sock.connect_ex((server, port))
if result == 0:
print(f" ✓ TCP connection successful")
sock.close()
else:
print(f" ✗ TCP connection failed (error code: {result})")
print(f" → Check if port {port} is correct (IMAPS should be 993)")
return False
except socket.timeout:
print(f" ✗ Connection timed out")
print(f" → Firewall may be blocking the connection")
return False
except Exception as e:
print(f" ✗ Connection error: {e}")
return False
# Test 3: IMAP server greeting
print(f"\n3. Testing IMAP server response...")
try:
mail = imaplib.IMAP4_SSL(server, port)
greeting = mail.welcome.decode() if mail.welcome else "No greeting"
print(f" ✓ Server greeting: {greeting[:60]}...")
mail.logout()
except Exception as e:
print(f" ✗ IMAP handshake failed: {e}")
return False
# Test 4: Authentication
print(f"\n4. Testing authentication...")
print(f" Username: {config['email_user']}")
# Try different username formats
username_variants = [config['email_user']]
# If email format, add the part before @
if '@' in config['email_user']:
base_username = config['email_user'].split('@')[0]
if base_username != config['email_user']:
username_variants.append(base_username)
# Also suggest common numeric IDs if we saw them in notes
# (This is based on the earlier IMAP notes showing 495384)
email_password = config.get('email_password', '')
for i, username_attempt in enumerate(username_variants, 1):
print(f"\n Attempt {i}: Trying username '{username_attempt}'")
try:
mail = imaplib.IMAP4_SSL(server, port)
mail.login(username_attempt, email_password)
print(f" ✓✓✓ SUCCESS! Authentication worked with username: '{username_attempt}'")
mail.logout()
print(f"\n💡 Update your config to use:")
print(f" email_user: {username_attempt}")
return True
except imaplib.IMAP4.error as e:
error_msg = str(e)
if "AUTHENTICATIONFAILED" in error_msg:
print(f" ✗ Authentication failed for '{username_attempt}'")
else:
print(f" ✗ Error: {error_msg}")
except Exception as e:
print(f" ✗ Unexpected error: {e}")
print(f"\n❌ All authentication attempts failed")
print(f"\n📋 Troubleshooting checklist:")
print(f" □ Password is correct (check for typos, special characters)")
print(f" □ IMAP is enabled in your email account settings")
print(f" □ If using 2FA, generate an app-specific password")
print(f" □ Check your email provider's IMAP settings")
print(f" □ Try accessing via webmail to verify account works")
return False
def main():
"""Main entry point."""
# Parse command line arguments
parser = argparse.ArgumentParser(
description="IMAP to Obsidian Email Importer",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
uv run imap_to_obsidian.py # Run with default config
uv run imap_to_obsidian.py -c work_config.md # Use specific config
uv run imap_to_obsidian.py --config ~/vault/config.md # Config with path
uv run imap_to_obsidian.py --test # Test connection
uv run imap_to_obsidian.py -c work_config.md --test # Test specific profile
"""
)
parser.add_argument(
"--config", "-c",
type=str,
default=None,
help="Path to config file (default: Email_import_config.md in vault)"
)
parser.add_argument(
"--test", "-t",
action="store_true",
help="Test IMAP connection and authentication"
)
parser.add_argument(
"--verbose", "-v",
action="store_true",
help="Verbose output"
)
args = parser.parse_args()
print("=" * 60)
print("IMAP to Obsidian Email Importer")
print("=" * 60)
try:
config = load_config(args.config)
config_path_display = args.config if args.config else CONFIG_FILE_DEFAULT
print(f"\nConfiguration loaded from: {config_path_display}")
print(f" Email: {config['email_user']}")
print(f" Server: {config['imap_server']}:{config['imap_port']}")
print(f" Folders: {', '.join(config['target_folders'])}")
print(f" Keywords: {', '.join(config['keywords'])}")
print(f" Blacklist: {', '.join(config.get('blacklist', []))}")
print(f" Import to: {config['import_folder']}")
print(f" Processed folder: {config['processed_folder']}")
print(f" Dry run: {'Yes' if config.get('dry_run', False) else 'No'}")
if args.test:
# Run diagnostic tests
success = test_connection(config)
sys.exit(0 if success else 1)
else:
# Run normal import
import_emails(config)
except FileNotFoundError as e:
print(f"✗ Error: {e}")
print(f" Please ensure the config file exists at {CONFIG_FILE}")
sys.exit(1)
except ValueError as e:
print(f"✗ Configuration error: {e}")
sys.exit(1)
except Exception as e:
print(f"✗ Unexpected error: {e}")
raise
if __name__ == "__main__":
main()