# Sending an incoming fax to email

1. Add a new dialplan application (see [**Dialplan Applications**](https://docs.mikopbx.com/mikopbx/english/manual/modules/dialplan-applications)).

<figure><img src="https://835495363-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsZ8acWnNlSalIHQjMFu1%2Fuploads%2FNrOZjBErG4lnGPyR87Uu%2FNEWDialplan.png?alt=media&#x26;token=9dff900f-c127-4426-96c5-69abe9d4e74e" alt=""><figcaption><p>Creating a new dialplan application</p></figcaption></figure>

2. Assign an internal number, for example, **2200110**.

<figure><img src="https://835495363-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsZ8acWnNlSalIHQjMFu1%2Fuploads%2F8FqzbADOLLpwCnoPN7JN%2FSettingsForDialplan.png?alt=media&#x26;token=ba1cc170-a282-43fe-a32f-6952f8b360d3" alt=""><figcaption><p>Parameters for dialplan </p></figcaption></figure>

3. Insert the following code into the "**Programme Code**" tab:

```php
<?php
require_once 'Globals.php';

use \MikoPBX\Core\Asterisk\AGI;
use MikoPBX\Core\System\Notifications;
use MikoPBX\Core\System\{MikoPBXConfig};

$agi     = new AGI();
$faxFile = "/tmp/" .$agi->get_variable("CDR(linkedid)", true).'.tiff';
$caller  = $agi->get_variable("CALLERID(num)", true);
$agi->exec("ReceiveFax", "{$faxFile},d"); 
$result  = $agi->get_variable("FAXOPT(status)", true);

if($result === 'SUCCESS' && file_exists($faxFile)){
    $notify = new Notifications();
    $notify->sendMail('your-email@gmail.com', 'Fax msg from '.$caller, 'Incoming fax <br><br>', $faxFile);
    unlink($faxFile);
}
sleep(1);
```

{% hint style="success" %}
In the code, replace '<your-email@gmail.com>' with your own email address.
{% endhint %}

<figure><img src="https://835495363-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsZ8acWnNlSalIHQjMFu1%2Fuploads%2F3jlleZ42HrNEnc0TdFm8%2FprogrammeCodeForDialplan.png?alt=media&#x26;token=721d9ca2-916a-4cd8-aceb-a1e44f896c66" alt=""><figcaption><p>Code for dialplan</p></figcaption></figure>

4. Go to the "**System file customization**" section.

<figure><img src="https://835495363-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsZ8acWnNlSalIHQjMFu1%2Fuploads%2Fn06cIfMIqk6wJhcxD3gW%2FSystemFileCustomization.png?alt=media&#x26;token=4edfbfbf-6903-46c5-b1b5-d2a62131f59e" alt=""><figcaption><p>System file customization section.</p></figcaption></figure>

5. Open the "**extensions.conf**" file for editing.

<figure><img src="https://835495363-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsZ8acWnNlSalIHQjMFu1%2Fuploads%2FNuELWVzuFh5rThr08jvu%2FEditExtensions.conf.png?alt=media&#x26;token=567dc075-00ed-45a7-bc10-915ccc1dc97f" alt=""><figcaption><p>extensions.conf file</p></figcaption></figure>

6. Add the folowwing code to the end of file:

```php
[SIP-1687941868-incoming](+)
exten => fax,1,Goto(internal,2200100,1)
```

<figure><img src="https://835495363-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsZ8acWnNlSalIHQjMFu1%2Fuploads%2FoNN8xxanXcjwyrtojwtz%2FcodeforExtenionsConf.png?alt=media&#x26;token=c9c52180-ae54-4782-a809-01f295fad3e7" alt=""><figcaption></figcaption></figure>

Replace "2200100" with the number of **your** application.

In the code, replace "**SIP-1687941868**" with your provider's ID. You can find it in the provider's card in the browser's address bar.

<figure><img src="https://835495363-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsZ8acWnNlSalIHQjMFu1%2Fuploads%2F3XStuOpw48SpfaPoja1Q%2FProviderID.png?alt=media&#x26;token=18907fb7-e20c-426f-ac7e-def2b35ae001" alt=""><figcaption><p>Provider ID</p></figcaption></figure>

7. Open the "**modules.conf**" file for editing.

<figure><img src="https://835495363-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsZ8acWnNlSalIHQjMFu1%2Fuploads%2FhbQsWn4atQiVihAhxcrs%2FmodulesConf.png?alt=media&#x26;token=47dfcc4e-a8bc-4c67-b3b4-64c927494daf" alt=""><figcaption><p>Modules.conf file</p></figcaption></figure>

8. Add the following code to the end of the file:

```php
load => res_fax.so
load => res_fax_spandsp.so
```

<figure><img src="https://835495363-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsZ8acWnNlSalIHQjMFu1%2Fuploads%2F5Awqjpe3Ng8xNAcKYziX%2FcodeForModulesConf.png?alt=media&#x26;token=92b0745e-b76e-49ac-937b-5bd3e43ee625" alt=""><figcaption><p>Code for Modules.conf</p></figcaption></figure>

9. In the provider's card, in the **advanced settings**, in **additional parameters**, specify:

```php
[endpoint]
fax_detect=yes
fax_detect_timeout=30
```

<figure><img src="https://835495363-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsZ8acWnNlSalIHQjMFu1%2Fuploads%2FWcgSAALWTFpUzAVGy5Nt%2FextraParametersForProvider.png?alt=media&#x26;token=7b149be9-95a0-448b-8b44-6f5e3af41b33" alt=""><figcaption><p>Additional parameters in Provider section</p></figcaption></figure>
