MCPcopy Create free account
hub / github.com/Keeper-Security/Commander / send

Method send

keepercommander/email_service.py:341–408  ·  view source on GitHub ↗

Send email via SMTP. Args: to: Recipient email address subject: Email subject body: Email body html: True if body is HTML Returns: True if sent successfully Raises: smtplib.SMTPException: If S

(self, to: str, subject: str, body: str, html: bool = False)

Source from the content-addressed store, hash-verified

339 self._connection = None
340
341 def send(self, to: str, subject: str, body: str, html: bool = False) -> bool:
342 """
343 Send email via SMTP.
344
345 Args:
346 to: Recipient email address
347 subject: Email subject
348 body: Email body
349 html: True if body is HTML
350
351 Returns:
352 True if sent successfully
353
354 Raises:
355 smtplib.SMTPException: If SMTP operation fails
356 """
357 try:
358 # Create message
359 msg = MIMEMultipart('alternative')
360 msg['Subject'] = subject
361 msg['From'] = f"{self.config.from_name} <{self.config.from_address}>"
362 msg['To'] = to
363
364 # Attach body
365 if html:
366 part = MIMEText(body, 'html')
367 else:
368 part = MIMEText(body, 'plain')
369 msg.attach(part)
370
371 # Connect and send
372 if self.config.smtp_use_ssl:
373 # Use SMTP_SSL for port 465
374 context = ssl.create_default_context()
375 with smtplib.SMTP_SSL(
376 self.config.smtp_host,
377 self.config.smtp_port,
378 context=context
379 ) as server:
380 server.login(self.config.smtp_username, self.config.smtp_password)
381 server.send_message(msg)
382 else:
383 # Use SMTP with STARTTLS for port 587
384 with smtplib.SMTP(
385 self.config.smtp_host,
386 self.config.smtp_port,
387 timeout=30
388 ) as server:
389 server.ehlo()
390 if self.config.smtp_use_tls:
391 context = ssl.create_default_context()
392 server.starttls(context=context)
393 server.ehlo()
394 server.login(self.config.smtp_username, self.config.smtp_password)
395 server.send_message(msg)
396
397 logging.info(f"[EMAIL] SMTP email sent to {to} via {self.config.smtp_host}")
398 return True

Callers 4

test_smtp_send_htmlMethod · 0.95

Calls 4

send_messageMethod · 0.80
loginMethod · 0.45
infoMethod · 0.45
errorMethod · 0.45

Tested by 4

test_smtp_send_htmlMethod · 0.76