Configuring a notification email for data export

This topic provides step-by-step instructions on how to prepare data for export and configure a script that sends a notification email with the exported file attached.

Step 1: Configure export settings

1. In Database Explorer, right-click the database whose data you want to export and select Tasks > Export Data.

2. On the Export format page of the Data Export Wizard, select CSV as the data export format, then click Next.

3. On the Source page, configure the export parameters:

3.1. Verify the connection and database names.

3.2. Select the tables and views to export data from.

3.3. Click Next.

Export settings

4. On the Output Settings page, configure the export options:

4.1. Choose whether to export the data as a single file or as separate files per object.

4.2. Specify the destination directory. If exporting to a single file, also enter the file name.

4.3. Optional: Append a timestamp to the file name.

4.4. Optional: Configure automatic deletion of old backup files.

4.5. Optional: Configure compression settings.

4.6. Preview the files to export.

4.7. Click Export.

Step 2: Generate a template file

After you configure the export settings, save them as a template.

1. In the Data Export Wizard, click Save > Save Template.

Save template

2. Specify the destination folder for the template file.

Step 3: Save command-line settings

1. In the export wizard, click Save > Save Command Line.

Save command-line

2. On the Command line execution file settings page, click Save, then enter a name for the batch file (.bat) where the settings will be stored. These settings will be required for use in the following step.

Copy command-line

Step 4: Set up email notifications

To send an email notification when the export finishes, create a Windows PowerShell script (.ps1).

1. Open a text editor.

2. Enter the following code:

# ── SMTP / RECIPIENT SETTINGS ──────────────────────────────────────────────────
$smtpServer   = "smtp.yourdomain.com"
$smtpPort     = smtpPortNumber
$smtpUser     = "[email protected]"
$smtpPassword = "smtpPassword"      

$from         = "[email protected]"
$to           = "[email protected]"

# ── EXIT‑CODE LOOK‑UP TABLE ────────────────────────────────────────────────────
$exitCodes = @{
    0   = "Success."                                  ; 2   = "Ctrl+Break."                               
    3   = "Failed."                                   ; 10  = "Command‑line usage error."             
    11  = "Illegal argument duplication."             ; 20  = "Trial expired."                         
    30  = "Project file corrupted."                   ; 40  = "Server connection failed."                          
    103 = "Script executed with errors."              ; 105 = "Resource unavailable."                     
    107 = "Failed to create report."                  ; 108 = "No objects for operation."
}

# ── COMPLETE COMMAND (UNCHANGED) ───────────────────────────────────────────────
$commandLine = '"C:\Program Files\Devart\dbForge Studio for MySQL\dbforgemysql.com" /dataexport /templatefile:"path_to_file.det" /connection:"User Id=User;Host=HOST;Database=database;Character Set=utf8" /table "database_table" /outputfile:"C:\file_path.csv" /singlefile /errormode:abort'

# ── RUN EXPORT ────────────────────────────────────────────────────────────────
$null = cmd /c $commandLine 2>&1
$code = $LASTEXITCODE

# ── PREPARE MAIL ──────────────────────────────────────────────────────────────
if ($exitCodes.ContainsKey($code)) {
    $description = $exitCodes[$code]
} else {
    $description = "Unknown exit code $code."
}

$subject = "dbForge MySQL export – exit code $code"
$body = @"
Time   : $(Get-Date -Format "u")

Command:
$commandLine

Exit code: $code
Meaning  : $description
"@

$csvPath = 'pathToFile.csv'
$attachments = @()
if ($code -eq 0 -and (Test-Path $csvPath)) {
    $attachments += $csvPath
}

# ── SEND MAIL ─────────────────────────────────────────────────────────────────
$securePw = ConvertTo-SecureString $smtpPassword -AsPlainText -Force
$cred     = New-Object System.Management.Automation.PSCredential($smtpUser, $securePw)

if ($attachments.Count -gt 0) {
    Send-MailMessage -SmtpServer $smtpServer -Port $smtpPort -UseSsl `
                     -Credential $cred -From $from -To $to `
                     -Subject $subject -Body $body -Attachments $attachments
} else {
    Send-MailMessage -SmtpServer $smtpServer -Port $smtpPort -UseSsl `
                     -Credential $cred -From $from -To $to `
                     -Subject $subject -Body $body
}

# ── CONSOLE OUTPUT ────────────────────────────────────────────────────────────
Write-Host "Exit code $code – $description"

3. Replace the following parameter values with your own.

  • $smtpServer = "smtp.yourdomain.com" – The SMTP server address.

  • $smtpPort = smtpPortNumber – The SMTP port number.

  • $smtpUser = "[email protected]" – The email address used to authenticate with the SMTP server.

  • $smtpPassword = "smtpPassword" – The password used to authenticate with the SMTP server.

  • $from = "[email protected]" – The sender email address.

  • $to = "[email protected]" – The recipient email address.

  • $csvPath = 'pathToFile.csv' – The path to the CSV file.

  • $commandLine – Your actual command-line configuration from Step 3.

4. Save the file with the PS1 extension.

Step 5: Execute the script

1. Open Windows PowerShell ISE as an administrator.

2. In the terminal, enter the path to the generated PS1 file and press Enter.

Execute the script

Once executed, the script displays an exit code. If the exit code is 0, the email notification is sent to the recipient.

Notification email