Пример #1
0
def make(doctype=None,
         name=None,
         content=None,
         subject=None,
         sent_or_received="Sent",
         sender=None,
         sender_full_name=None,
         recipients=None,
         communication_medium="Email",
         send_email=False,
         print_html=None,
         print_format=None,
         attachments='[]',
         send_me_a_copy=False,
         cc=None,
         bcc=None,
         flags=None,
         read_receipt=None,
         print_letterhead=True):
    """Make a new communication.

	:param doctype: Reference DocType.
	:param name: Reference Document name.
	:param content: Communication body.
	:param subject: Communication subject.
	:param sent_or_received: Sent or Received (default **Sent**).
	:param sender: Communcation sender (default current user).
	:param recipients: Communication recipients as list.
	:param communication_medium: Medium of communication (default **Email**).
	:param send_email: Send via email (default **False**).
	:param print_html: HTML Print format to be sent as attachment.
	:param print_format: Print Format name of parent document to be sent as attachment.
	:param attachments: List of attachments as list of files or JSON string.
	:param send_me_a_copy: Send a copy to the sender (default **False**).
	"""

    is_error_report = (doctype == "User" and name == frappe.session.user
                       and subject == "Error Report")
    send_me_a_copy = cint(send_me_a_copy)

    if doctype and name and not is_error_report and not frappe.has_permission(
            doctype, "email",
            name) and not (flags or {}).get('ignore_doctype_permissions'):
        raise frappe.PermissionError(
            "You are not allowed to send emails related to: {doctype} {name}".
            format(doctype=doctype, name=name))

    if not sender:
        sender = get_formatted_email(frappe.session.user)

    if isinstance(recipients, list):
        recipients = ', '.join(recipients)

    comm = frappe.get_doc({
        "doctype": "Communication",
        "subject": subject,
        "content": content,
        "sender": sender,
        "sender_full_name": sender_full_name,
        "recipients": recipients,
        "cc": cc or None,
        "bcc": bcc or None,
        "communication_medium": communication_medium,
        "sent_or_received": sent_or_received,
        "reference_doctype": doctype,
        "reference_name": name,
        "message_id": get_message_id().strip(" <>"),
        "read_receipt": read_receipt,
        "has_attachment": 1 if attachments else 0
    })
    comm.insert(ignore_permissions=True)

    if not doctype:
        # if no reference given, then send it against the communication
        comm.db_set(
            dict(reference_doctype='Communication', reference_name=comm.name))

    if isinstance(attachments, string_types):
        attachments = json.loads(attachments)

    # if not committed, delayed task doesn't find the communication
    if attachments:
        add_attachments(comm.name, attachments)

    frappe.db.commit()

    if cint(send_email):
        frappe.flags.print_letterhead = cint(print_letterhead)
        comm.send(print_html,
                  print_format,
                  attachments,
                  send_me_a_copy=send_me_a_copy)

    return {
        "name":
        comm.name,
        "emails_not_sent_to":
        ", ".join(comm.emails_not_sent_to)
        if hasattr(comm, "emails_not_sent_to") else None
    }
Пример #2
0
 def new_email_queue(self, **kwargs):
     defaults = {'message_id': get_message_id().strip(" <>")}
     d = {**defaults, **kwargs}
     return self.new_doc('Email Queue', **d)
Пример #3
0
def make(doctype=None, name=None, content=None, subject=None, sent_or_received = "Sent",
	sender=None, sender_full_name=None, recipients=None, communication_medium="Email", send_email=False,
	print_html=None, print_format=None, attachments='[]', send_me_a_copy=False, cc=None, flags=None,read_receipt=None):
	"""Make a new communication.

	:param doctype: Reference DocType.
	:param name: Reference Document name.
	:param content: Communication body.
	:param subject: Communication subject.
	:param sent_or_received: Sent or Received (default **Sent**).
	:param sender: Communcation sender (default current user).
	:param recipients: Communication recipients as list.
	:param communication_medium: Medium of communication (default **Email**).
	:param send_mail: Send via email (default **False**).
	:param print_html: HTML Print format to be sent as attachment.
	:param print_format: Print Format name of parent document to be sent as attachment.
	:param attachments: List of attachments as list of files or JSON string.
	:param send_me_a_copy: Send a copy to the sender (default **False**).
	"""

	is_error_report = (doctype=="User" and name==frappe.session.user and subject=="Error Report")
	send_me_a_copy = cint(send_me_a_copy)

	if doctype and name and not is_error_report and not frappe.has_permission(doctype, "email", name) and not (flags or {}).get('ignore_doctype_permissions'):
		raise frappe.PermissionError("You are not allowed to send emails related to: {doctype} {name}".format(
			doctype=doctype, name=name))

	if not sender:
		sender = get_formatted_email(frappe.session.user)

	comm = frappe.get_doc({
		"doctype":"Communication",
		"subject": subject,
		"content": content,
		"sender": sender,
		"sender_full_name":sender_full_name,
		"recipients": recipients,
		"cc": cc or None,
		"communication_medium": communication_medium,
		"sent_or_received": sent_or_received,
		"reference_doctype": doctype,
		"reference_name": name,
		"message_id":get_message_id().strip(" <>"),
		"read_receipt":read_receipt,
		"has_attachment": 1 if attachments else 0
	})
	comm.insert(ignore_permissions=True)

	if not doctype:
		# if no reference given, then send it against the communication
		comm.db_set(dict(reference_doctype='Communication', reference_name=comm.name))

	if isinstance(attachments, string_types):
		attachments = json.loads(attachments)

	# if not committed, delayed task doesn't find the communication
	if attachments:
		add_attachments(comm.name, attachments)

	frappe.db.commit()

	if cint(send_email):
		comm.send(print_html, print_format, attachments, send_me_a_copy=send_me_a_copy)

	return {
		"name": comm.name,
		"emails_not_sent_to": ", ".join(comm.emails_not_sent_to) if hasattr(comm, "emails_not_sent_to") else None
	}
Пример #4
0
def make(doctype=None,
         name=None,
         content=None,
         subject=None,
         sent_or_received="Sent",
         sender=None,
         sender_full_name=None,
         recipients=None,
         communication_medium="Email",
         send_email=False,
         print_html=None,
         print_format=None,
         attachments='[]',
         send_me_a_copy=False,
         cc=None,
         bcc=None,
         flags=None,
         read_receipt=None,
         print_letterhead=True,
         email_template=None,
         communication_type=None,
         ignore_permissions=False):
    """Make a new communication.

	:param doctype: Reference DocType.
	:param name: Reference Document name.
	:param content: Communication body.
	:param subject: Communication subject.
	:param sent_or_received: Sent or Received (default **Sent**).
	:param sender: Communcation sender (default current user).
	:param recipients: Communication recipients as list.
	:param communication_medium: Medium of communication (default **Email**).
	:param send_email: Send via email (default **False**).
	:param print_html: HTML Print format to be sent as attachment.
	:param print_format: Print Format name of parent document to be sent as attachment.
	:param attachments: List of attachments as list of files or JSON string.
	:param send_me_a_copy: Send a copy to the sender (default **False**).
	:param email_template: Template which is used to compose mail .
	"""
    is_error_report = (doctype == "User" and name == frappe.session.user
                       and subject == "Error Report")
    send_me_a_copy = cint(send_me_a_copy)

    if not ignore_permissions:
        if doctype and name and not is_error_report and not frappe.has_permission(
                doctype, "email",
                name) and not (flags or {}).get('ignore_doctype_permissions'):
            raise frappe.PermissionError(
                "You are not allowed to send emails related to: {doctype} {name}"
                .format(doctype=doctype, name=name))

    if not sender:
        sender = get_formatted_email(frappe.session.user)

    recipients = list_to_str(recipients) if isinstance(recipients,
                                                       list) else recipients
    cc = list_to_str(cc) if isinstance(cc, list) else cc
    bcc = list_to_str(bcc) if isinstance(bcc, list) else bcc

    comm = frappe.get_doc({
        "doctype": "Communication",
        "subject": subject,
        "content": content,
        "sender": sender,
        "sender_full_name": sender_full_name,
        "recipients": recipients,
        "cc": cc or None,
        "bcc": bcc or None,
        "communication_medium": communication_medium,
        "sent_or_received": sent_or_received,
        "reference_doctype": doctype,
        "reference_name": name,
        "email_template": email_template,
        "message_id": get_message_id().strip(" <>"),
        "read_receipt": read_receipt,
        "has_attachment": 1 if attachments else 0,
        "communication_type": communication_type
    }).insert(ignore_permissions=True)

    comm.save(ignore_permissions=True)

    if isinstance(attachments, str):
        attachments = json.loads(attachments)

    # if not committed, delayed task doesn't find the communication
    if attachments:
        add_attachments(comm.name, attachments)

    if cint(send_email):
        if not comm.get_outgoing_email_account():
            frappe.throw(msg=OUTGOING_EMAIL_ACCOUNT_MISSING,
                         exc=frappe.OutgoingEmailError)

        comm.send_email(print_html=print_html,
                        print_format=print_format,
                        send_me_a_copy=send_me_a_copy,
                        print_letterhead=print_letterhead)

    emails_not_sent_to = comm.exclude_emails_list(
        include_sender=send_me_a_copy)
    return {
        "name": comm.name,
        "emails_not_sent_to": ", ".join(emails_not_sent_to or [])
    }
Пример #5
0
def _make(
    doctype=None,
    name=None,
    content=None,
    subject=None,
    sent_or_received="Sent",
    sender=None,
    sender_full_name=None,
    recipients=None,
    communication_medium="Email",
    send_email=False,
    print_html=None,
    print_format=None,
    attachments="[]",
    send_me_a_copy=False,
    cc=None,
    bcc=None,
    read_receipt=None,
    print_letterhead=True,
    email_template=None,
    communication_type=None,
    add_signature=True,
):
    """Internal method to make a new communication that ignores Permission checks."""

    sender = sender or get_formatted_email(frappe.session.user)
    recipients = list_to_str(recipients) if isinstance(recipients,
                                                       list) else recipients
    cc = list_to_str(cc) if isinstance(cc, list) else cc
    bcc = list_to_str(bcc) if isinstance(bcc, list) else bcc

    comm = frappe.get_doc({
        "doctype":
        "Communication",
        "subject":
        subject,
        "content":
        content,
        "sender":
        sender,
        "sender_full_name":
        sender_full_name,
        "recipients":
        recipients,
        "cc":
        cc or None,
        "bcc":
        bcc or None,
        "communication_medium":
        communication_medium,
        "sent_or_received":
        sent_or_received,
        "reference_doctype":
        doctype,
        "reference_name":
        name,
        "email_template":
        email_template,
        "message_id":
        get_string_between("<", get_message_id(), ">"),
        "read_receipt":
        read_receipt,
        "has_attachment":
        1 if attachments else 0,
        "communication_type":
        communication_type,
    })
    comm.flags.skip_add_signature = not add_signature
    comm.insert(ignore_permissions=True)

    if isinstance(attachments, string_types):
        attachments = json.loads(attachments)

    # if not committed, delayed task doesn't find the communication
    if attachments:
        add_attachments(comm.name, attachments)

    if cint(send_email):
        # Raise error if outgoing email account is missing
        _ = frappe.email.smtp.get_outgoing_email_account(
            append_to=comm.doctype, sender=comm.sender)
        frappe.flags.print_letterhead = cint(print_letterhead)
        comm.send(print_html,
                  print_format,
                  attachments,
                  send_me_a_copy=send_me_a_copy)

    return {
        "name":
        comm.name,
        "emails_not_sent_to":
        ", ".join(comm.emails_not_sent_to)
        if hasattr(comm, "emails_not_sent_to") else None,
    }