### SMTP Configuration Example Source: https://github.com/nfephp-org/sped-mail/blob/master/README.md Configure SMTP settings using a stdClass object. Ensure all necessary parameters like host, port, authentication, and sender details are correctly set. ```php $config = new stdClass(); $config->smtpdebug = 0; //0-no 1-client 2-server 3-connection 4-lowlevel $config->host = 'smtp.example.com.br'; $config->port = 587; //25 ou 465 ou 587 $config->smtpauth = true; $config->user = 'fulano@example.com.br'; $config->password = 'senha'; $config->secure = 'tls'; $config->authtype = ''; //'', CRAM-MD5, PLAIN, LOGIN, XOAUTH2 $config->from = 'fulano@example.com.br'; $config->fantasy = 'Fulanoda Silva'; $config->replyTo = 'ciclano@mail.com'; $config->replyName = 'Ciclano Moreira'; $config->smtpoptions = null; /*[ 'ssl' => [ 'verify_peer' => true, 'verify_depth' => 3, 'allow_self_signed' => true, 'peer_name' => 'smtp.example.com', 'cafile' => '/etc/ssl/ca_cert.pem', ] ];*/ $config->timeout = 130; ``` -------------------------------- ### Install sped-mail with Composer Source: https://github.com/nfephp-org/sped-mail/blob/master/README.md Add the sped-mail package to your project using Composer. You can do this via the command line or by updating your composer.json file. ```bash composer require nfephp-org/sped-mail ``` ```json { "require": { "nfephp-org/sped-mail": "^1.0" } } ``` -------------------------------- ### Using NFePHP Mail Class Instance Source: https://github.com/nfephp-org/sped-mail/blob/master/docs/Mail.md Instantiate the Mail class with SMTP configuration and then load documents and optionally a custom HTML template before sending emails to specified addresses. ```php use NFePHP\Mail\Mail; $config = new stdClass(); $config->host = 'smtp.test.com.br'; $config->user = 'usuario@test.com.br'; $config->password = 'senha'; $config->secure = 'tls'; $config->port = 587; $config->from = 'usuario@test.com.br'; $config->fantasy = 'Test Ltda'; $config->replyTo = 'vendas@test.com.br'; $config->replyName = 'Vendas'; try { //a configuração é uma stdClass com os campos acima indicados //esse parametro é OBRIGATÓRIO $mail = new Mail($config); //use isso para inserir seu próprio template HTML com os campos corretos //para serem substituidos em execução com os dados dos xml $htmlTemplate = ''; $mail->loadTemplate($htmlTemplate); //aqui são passados os documentos, tanto pode ser um path como o conteudo //desses documentos $xml = 'nfe.xml'; $pdf = '';//não é obrigatório passar o PDF, tendo em vista que é uma BOBAGEM $mail->loadDocuments($xml, $pdf); //se não for passado esse array serão enviados apenas os emails //que estão contidos no XML, isto se existirem $addresses = ['seu@email.com.br']; //se esse array for passado serão enviados emails para os endereços indicados apenas //e os endereços contidos no xml serão ignorados //envia emails $mail->send($addresses); } catch (\InvalidArgumentException $e) { echo "Falha: " . $e->getMessage(); } catch (\RuntimeException $e) { echo "Falha: " . $e->getMessage(); } catch (\Exception $e) { echo "Falha: " . $e->getMessage(); } ``` -------------------------------- ### PDF Content or Path (Optional) Source: https://github.com/nfephp-org/sped-mail/blob/master/README.md Optionally, provide the PDF content as a string or a file path. ```php $pdf = '../nfe.pdf'; ``` ```php $pdf = file_get_contents('../nfe.pdf'); ``` -------------------------------- ### Send Email using Static Method Source: https://github.com/nfephp-org/sped-mail/blob/master/README.md Use the static `sendMail` method for sending emails. This method requires configuration, XML content, and optionally PDF, recipient addresses, templates, PFX certificates, and passwords. ```php $resp = Mail::sendMail($config, $xml, $pdf, $addresses, $template, $pfx, $password, $mailer); ``` -------------------------------- ### XML Content or Path Source: https://github.com/nfephp-org/sped-mail/blob/master/README.md Provide the XML content as a string or a file path to the `sendMail` method. ```php $xml = '../nfe.xml'; ``` ```php $xml = file_get_contents('../nfe.xml'); ``` -------------------------------- ### NFePHP\Mail\Mail::__construct Source: https://github.com/nfephp-org/sped-mail/blob/master/docs/Mail.md The constructor method initializes the Mail class. It accepts a stdClass object containing SMTP configuration details required for sending emails. ```APIDOC ## __construct $config ### Description Initializes the Mail class with SMTP configuration. ### Parameters #### Path Parameters - **config** (stdClass) - Required - An object containing the following properties: - **host** (string) - The SMTP host address. - **user** (string) - The SMTP username. - **password** (string) - The SMTP password. - **secure** (string) - The security level (e.g., 'tls'). - **port** (integer) - The SMTP port number. - **from** (string) - The sender's email address. - **fantasy** (string) - The simplified company name. - **replyTo** (string) - The email address for replies. - **replyName** (string) - The name associated with the reply-to address. ``` -------------------------------- ### PFX Certificate Content (Optional) Source: https://github.com/nfephp-org/sped-mail/blob/master/README.md Provide the PFX certificate content, which can be read from a file or directly from a database. ```php $pfx = file_get_contents('path ao certificado pfx'); ``` -------------------------------- ### Using NFePHP Mail Static Method Source: https://github.com/nfephp-org/sped-mail/blob/master/docs/Mail.md Send emails directly using the static `sendMail` method, providing configuration, XML document, optional PDF, and recipient addresses. This method also supports an optional HTML template. ```php use NFePHP\Mail\Mail; $config = new stdClass(); $config->host = 'smtp.test.com.br'; $config->user = 'usuario@test.com.br'; $config->password = 'senha'; $config->secure = 'tls'; $config->port = 587; $config->from = 'usuario@test.com.br'; $config->fantasy = 'Test Ltda'; $config->replyTo = 'vendas@test.com.br'; $config->replyName = 'Vendas'; try { //paramtros: //config - (obrigatório) vide acima //xml - (obrigatório) documento a ser enviado NFe, NFCe, CTe, ou CCe, pode ser um path ou o arquivo em string //pdf - (opcional) documento pdf a ser enviado DANFE, DANFCE, DACTE, ou DACCE, pode ser um path ou o arquivo em string //enderecos - (opcional) array com os endereços de email adicionais para envio //template = (opcional) template HTML a ser usado $resp = Mail::sendMail($config, 'nfe.xml', '', ['recebedor@outro.com.br'], ''); } catch (\InvalidArgumentException $e) { echo "Falha: " . $e->getMessage(); } catch (\RuntimeException $e) { echo "Falha: " . $e->getMessage(); } catch (\Exception $e) { echo "Falha: " . $e->getMessage(); } ``` -------------------------------- ### PFX Certificate Password (Optional) Source: https://github.com/nfephp-org/sped-mail/blob/master/README.md Provide the password for the PFX certificate if it is protected. ```php $password = 'senha'; ``` -------------------------------- ### Existing PHPMailer Instance (Optional) Source: https://github.com/nfephp-org/sped-mail/blob/master/README.md Pass an already instantiated PHPMailer object to the `sendMail` method. ```php $mailer = new PHPMailer(); ``` -------------------------------- ### static sendMail($config, $xml, $pdf, $addresses, $htmltemplate) Source: https://github.com/nfephp-org/sped-mail/blob/master/docs/Mail.md A static method to send emails. It requires configuration, XML data, and optionally a PDF file, additional recipient addresses, and a custom HTML template. This is the primary method for sending emails with attachments. ```APIDOC ## function static sendMail($config, $xml, $pdf, $addresses, $htmltemplate) ### Description A static method to send emails. It requires configuration, XML data, and optionally a PDF file, additional recipient addresses, and a custom HTML template. This is the primary method for sending emails with attachments. ### Parameters #### Path Parameters - **config** (stdClass) - Required - Configuration object for the email service. - **xml** (string|resource) - Required - Path to the XML document or the XML content as a string. - **pdf** (string|resource) - Optional - Path to the PDF document or the PDF content as a string. - **addresses** (array) - Optional - An array of additional email addresses. - **htmltemplate** (string) - Optional - A string containing an alternative HTML template. ### Notes - Static images or logos are not supported directly; they must be included within the provided HTML template. ``` -------------------------------- ### loadTemplate($html) Source: https://github.com/nfephp-org/sped-mail/blob/master/docs/Mail.md Allows users to create their own custom HTML templates for email messages. The method accepts an HTML string as input, which can include predefined variables that will be replaced with actual data during email generation. ```APIDOC ## function loadTemplate($html) ### Description This method allows users to create their own custom HTML templates for email messages. The method accepts an HTML string as input, which can include predefined variables that will be replaced with actual data during email generation. ### Parameters #### Path Parameters - **html** (string) - Required - The HTML content for the email template. ### Variables The following variables can be used within the template and will be replaced dynamically: - `{destinatario}`: Recipient's full name (e.g., xNome). - `{data}`: Emission date of the document (e.g., dhEmi). - `{numero}`: Document number (e.g., nNF). - `{emitente}`: Issuer's full name (e.g., xNome). - `{valor}`: Total document value (e.g., vNF). ``` -------------------------------- ### NFePHP\Mail\Mail::sendMail (Static Method) Source: https://github.com/nfephp-org/sped-mail/blob/master/docs/Mail.md A static method to send emails directly. It takes configuration, XML, optional PDF, optional recipient addresses, and an optional HTML template as arguments. ```APIDOC ## sendMail $config, $xml, $pdf, $addresses, $template ### Description Statically sends an email with attached documents. ### Parameters #### Path Parameters - **config** (stdClass) - Required - SMTP configuration object (see __construct documentation). - **xml** (string) - Required - Path to the XML document or the XML content as a string. - **pdf** (string) - Optional - Path to the PDF document or the PDF content as a string. - **addresses** (array) - Optional - An array of email addresses to send the emails to. - **template** (string) - Optional - An HTML template to be used for the email body. ``` -------------------------------- ### NFePHP\Mail\Mail::loadDocuments Source: https://github.com/nfephp-org/sped-mail/blob/master/docs/Mail.md This method loads the documents (XML and optional PDF) that will be attached to the emails. Documents can be provided as file paths or their string content. ```APIDOC ## loadDocuments $xml, $pdf ### Description Loads the XML and optional PDF documents to be attached to emails. ### Parameters #### Path Parameters - **xml** (string) - Required - The path to the XML document or the XML content as a string. - **pdf** (string) - Optional - The path to the PDF document or the PDF content as a string. ``` -------------------------------- ### Extracting Emails from XML using obsCont Source: https://github.com/nfephp-org/sped-mail/blob/master/docs/Mail.md Recipient email addresses can be included in the XML using the `` tag with `xCampo="email"`. Multiple email addresses can be added this way. ```xml fulano@yahoo.com.br cilcano@yahoo.com.br beltrano@yahoo.com.br ``` -------------------------------- ### Customizable Email Template Structure Source: https://github.com/nfephp-org/sped-mail/blob/master/docs/Mail.md This HTML template can be used to create custom email messages. Variables like {destinatario}, {data}, {numero}, {emitente}, and {valor} will be dynamically replaced with data from the NF-e XML. ```html

Prezados {destinatario},

"

Você está recebendo a Nota Fiscal Eletrônica emitida em {data} com o número {numero}, de {emitente}, no valor de R$ {valor} Junto com a mercadoria, você receberá também um DANFE (Documento Auxiliar da Nota Fiscal Eletrônica), que acompanha o trânsito das mercadorias.

Podemos conceituar a Nota Fiscal Eletrônica como um documento de existência apenas digital, emitido e armazenado eletronicamente, com o intuito de documentar, para fins fiscais, uma operação de circulação de mercadorias, ocorrida entre as partes. Sua validade jurídica garantida pela assinatura digital do remetente (garantia de autoria e de integridade) e recepção, pelo Fisco, do documento eletrônico, antes da ocorrência do Fato Gerador.

Os registros fiscais e contábeis devem ser feitos, a partir do próprio arquivo da NF-e, anexo neste e-mail, ou utilizando o DANFE, que representa graficamente a Nota Fiscal Eletrônica. A validade e autenticidade deste documento eletrônico pode ser verificada no site nacional do projeto (www.nfe.fazenda.gov.br), através da chave de acesso contida no DANFE.

Para poder utilizar os dados descritos do DANFE na escrituração da NF-e, tanto o contribuinte destinatário, como o contribuinte emitente, terão de verificar a validade da NF-e. Esta validade está vinculada à efetiva existência da NF-e nos arquivos da SEFAZ, e comprovada através da emissão da Autorização de Uso.

O DANFE não é uma nota fiscal, nem substitui uma nota fiscal, servindo apenas como instrumento auxiliar para consulta da NF-e no Ambiente Nacional.

Para mais detalhes, consulte: www.nfe.fazenda.gov.br


Atenciosamente,

{emitente}

``` -------------------------------- ### Custom Email Template (Optional) Source: https://github.com/nfephp-org/sped-mail/blob/master/README.md Replace the default email template with a custom HTML template. Refer to the Base.php class for standard template structure. ```php $template = '

Meu HTML {emitente} .... '; ``` -------------------------------- ### send($addresses) Source: https://github.com/nfephp-org/sped-mail/blob/master/docs/Mail.md Sends an email to specified recipients. It automatically extracts email addresses from the XML data and merges them with any additional addresses provided in the $addresses array. Duplicate and invalid email addresses are removed before sending. ```APIDOC ## function send($addresses) ### Description Sends an email to specified recipients. It automatically extracts email addresses from the XML data and merges them with any additional addresses provided in the $addresses array. Duplicate and invalid email addresses are removed before sending. ### Parameters #### Path Parameters - **addresses** (array) - Optional - An array of email addresses to send the email to. If not provided, emails will be sent only to addresses found in the XML. ### Notes - Email addresses can also be included in the XML using the `` tag. - Before sending, duplicate and invalid email addresses are removed. ``` -------------------------------- ### Recipient Addresses Array (Optional) Source: https://github.com/nfephp-org/sped-mail/blob/master/README.md Specify an array of recipient email addresses. Invalid addresses will be discarded. If not provided, the system attempts to find addresses within the XML. ```php $addresses = ['fulano@client.com.br']; ``` -------------------------------- ### Email Recipient in XML Observation Field Source: https://github.com/nfephp-org/sped-mail/blob/master/README.md Specify recipient email addresses within the XML document using the `` tag with `xCampo="email"`. ```xml fulano@yahoo.com.br ``` -------------------------------- ### NFePHP\Mail\Mail::send Source: https://github.com/nfephp-org/sped-mail/blob/master/docs/Mail.md Sends emails with attached documents. If an array of addresses is provided, emails are sent only to those addresses, ignoring addresses in the XML. Otherwise, emails are sent to addresses found within the XML. ```APIDOC ## send $addresses ### Description Sends emails with attached documents to specified recipients. ### Parameters #### Path Parameters - **addresses** (array) - Optional - An array of email addresses to send the emails to. If provided, addresses within the XML are ignored. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.