Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Certified email (PEC) #512

Closed
antoniofusco opened this issue Jul 2, 2021 · 5 comments
Closed

Certified email (PEC) #512

antoniofusco opened this issue Jul 2, 2021 · 5 comments

Comments

@antoniofusco
Copy link

Hello
Is it possible to fetch the message of the email? At moment the body is not correct with email that are certified email

Thanks

@Slamdunk
Copy link
Collaborator

Slamdunk commented Jul 2, 2021

Hi, I don't understand your question, can you be more specific on the issue?

@antoniofusco
Copy link
Author

The certified emails pec in Italy have a different structure I guess and the body of the email is inside an attachment or something.
Is there a possibility to get this content with this library?
Thanks

@Slamdunk
Copy link
Collaborator

Slamdunk commented Jul 2, 2021

Sure, I use this library with PEC providers too.

For metadatas you need to inspect attachments like daticert.xml and then parse it as a DOMDocument so you can query it for important data like /postacert/dati/msgid, /postacert/dati/data, /postacert/dati/errore-esteso and so on.

The real mail is another attachment that can be inspected as an EmbeddedMessage.

@Slamdunk Slamdunk closed this as completed Jul 2, 2021
@antoniofusco
Copy link
Author

Do you have a snippet of code, because for me it is not working
Thanks

@Slamdunk
Copy link
Collaborator

Slamdunk commented Jul 2, 2021

I can only give you a part of the code since the rest is NDA protected.
You have to figure out by yourself how to integrate this into your codebase.

$mailbox    = $connection->getMailbox('INBOX');
$messages   = $mailbox->getMessages(new Since($dataMinima));
foreach ($messages as $message) {
    foreach ($message->getAttachments() as $attachment) {
        if ('daticert.xml' !== $attachment->getFilename()) {
            continue;
        }

        $xml = $attachment->getDecodedContent();
        $dom = new DOMDocument();
        $dom->loadXML($xml);

        $domXpath            = new DOMXPath($dom);
        $msgIdNodeList       = $domXpath->query('/postacert/dati/msgid');
        if (1 !== $msgIdNodeList->length) {
            continue;
        }
        $msgId     = $msgIdNodeList->item(0)->textContent;
        $consegna  = null !== ($t = $domXpath->query('/postacert/dati/consegna')->item(0)) ? \mb_strtolower($t->textContent) : null;

        $tipo = $domXpath->query('/postacert')->item(0)->getAttribute('tipo');
        $data = Date::createFromFormat('d/m/Y H:i:s O', \sprintf(
            '%s %s %s',
            $domXpath->query('/postacert/dati/data/giorno')->item(0)->textContent,
            $domXpath->query('/postacert/dati/data/ora')->item(0)->textContent,
            $domXpath->query('/postacert/dati/data')->item(0)->getAttribute('zona')
        ));
        $data           = $data->setTimezone(new DateTimeZone(\date_default_timezone_get()));
        $identificativo = $domXpath->query('/postacert/dati/identificativo')->item(0)->textContent;
        $errore         = $domXpath->query('/postacert')->item(0)->getAttribute('errore');
        $oggetto        = null !== ($t = $domXpath->query('/postacert/intestazione/oggetto')->item(0)) ? $t->textContent : null;
        $gestore        = $domXpath->query('/postacert/dati/gestore-emittente')->item(0)->textContent;
        $ricevuta       = null !== ($t = $domXpath->query('/postacert/dati/ricevuta')->item(0)) ? $t->getAttribute('tipo') : null;
        $erroreEsteso   = null !== ($t = $domXpath->query('/postacert/dati/errore-esteso')->item(0)) ? $t->textContent : null;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants