Email with attachments

128 views
Skip to first unread message

Ronaldo Geniais

unread,
Jun 16, 2025, 5:06:53 PM6/16/25
to Harbour Minigui
I am testing the examples of sending emails with attachments that are in the samples folder. In MultipleMail, when attaching more than one file, it sends an email for each attachment. This is inconvenient. Is there a solution?

The other examples have similar behavior or do not send more than one attachment, nor do they break the text into lines as typed in the edit box.

Eu estou testando os exemplos de  envio de email com anexos que
tem na pasta samples.
No MultipleMail, ao anexar mais de um arquivo,
ele envia um email para cada anexo, isso é um inconveniente
nao tem solucao ?
Os outros exemplos tem comportamento semelhante ou nao enviam mais de um anexo, tambem nao quebram o texto em linhas conforme
digitado no edit box

Ronaldo Geniais

unread,
Jun 18, 2025, 11:08:06 AM6/18/25
to Harbour Minigui
no response/feedback
Can someone help me with this issue
or is there no solution?

Grzegorz Wojnarowski

unread,
Jun 20, 2025, 4:06:25 PM6/20/25
to Harbour Minigui
Hi Ronaldo
I use PowerShell to send emails

First I create some files in my Harbour program:
 1 - script file (EMAILS_SKRYPT.PS1)
 2 - body file (EMAILS_BODY.HTML)
 3 - run file (EMAILS_RUN.BAT)
At the end I execute  RUN( 'EMAILS_RUN.BAT' ) 

At the beginning you can edit and run it from Windows, then you start programming to create and run files from your program.

Sample files:

------ EMAILS_RUN.BAT ------

@echo off
powershell.exe -executionpolicy remotesigned -File emails_skrypt.ps1

------ EMAILS_BODY.HTML ------ 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
<center>
  <table>
     <tr><td colspan="2" class="tdtyt">Invoice info</td></tr>
     <tr><td colspan="2">Hi, there is your invoice:</td></tr>
     <tr><td>Number</td><td>Invoice1<br>Invoice2</td></tr>
     <tr><td colspan="2" class="tdend">This is a test message</td></tr>
  </table>
</center>
</body>
</html>


------ EMAILS_SKRYPT.PS1 ------

# function to store info in a log file
Function Write-Log {
    [CmdletBinding()]
    Param (
       [string]$Label,
       [string]$Message
    )

    $Stamp = (Get-Date).toString("yyyy/MM/dd HH:mm:ss")
    $Line = "$Stamp $Label $Message"

    Add-Content $logmail -Value $Line
}

# new message
$logmail  = 'emails_snd.log'
$nError   = 0

$server   = 'mail.foo.bar'
$port     =  587
$user     = 'smtp_user'
$pass     = 'smtp_pass'

$from     = 'fr...@foo.bat'
$repl     = 're...@foo.bar'

$subiect  = 'There is a subject'
$priority = 'Normal'
try
{
   $body     = get-content -Raw 'emails_body.html'
}
catch
{
   $nError = 1
   Write-Log '[ERROR_MSG]' $_.Exception.Message
}

# TLS 1.2
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$smtp             = New-Object Net.Mail.SmtpClient($server, $port)
$smtp.Credentials = New-Object System.Net.NetworkCredential($user, $pass)
$smtp.EnableSsl   = $True

$msg = New-Object Net.Mail.MailMessage
try
{
   $msg.From    = $from
}
catch
{
   $nError = 1
   Write-Log '[ERROR_MSG]' $_.Exception.Message
}
try
{
   $msg.Subject = $subiect
}
catch
{
   $nError = 1
   Write-Log '[ERROR_MSG]' $_.Exception.Message
}
try
{
   $msg.Body    = $body
}
catch
{
   $nError = 1
   Write-Log '[ERROR_MSG]' $_.Exception.Message
}
$msg.Priority   = $priority
$msg.IsBodyHtml = $True
try
{
   $msg.ReplyTo = $repl
}
catch
{
   $nError = 1
   Write-Log '[INFO]' 'RE: "r...@foo.bar"'
   Write-Log '[ERROR_ADR]' $_.Exception.Message
}

# receivers
try
{
   $msg.To.Add('cli...@gmail.com')
}
catch
{
   $nError = 1
   Write-Log '[INFO]' 'TO: "cli...@gmail.com"'
   Write-Log '[ERROR_ADR]' $_.Exception.Message
}

try
{
   $msg.Bcc.Add('b...@foo.bar')
}
catch
{
   $nError = 1
   Write-Log '[INFO]' 'BCC: "xx...@foo.bar"'
   Write-Log '[ERROR_ADR]' $_.Exception.Message
}

# attachments
try
{
   $msg.Attachments.Add('Invoice1.pdf')
}
catch
{
   $nError = 1
   Write-Log '[INFO]' 'ADD: "Invoice1.pdf"'
   Write-Log '[ERROR_ZAL]' $_.Exception.Message
}

try
{
   $msg.Attachments.Add('Invoice2.pdf')
}
catch
{
   $nError = 1
   Write-Log '[INFO]' 'ADD: "Invoice2.pdf"'
   Write-Log '[ERROR_ZAL]' $_.Exception.Message
}

# send
if($nError -eq 0)
{
try
{
   $smtp.Send($msg)
}
catch
{
   $nError = 1
   Write-Log '[ERROR_SND]' $_.Exception.Message
}
}
if($nError -eq 0)
{
   Write-Log '[OK]' 'Sent'
}


I hope it will help.

Regards
Grzegorz Wojnarowski

Ronaldo Geniais

unread,
Jun 22, 2025, 11:42:16 AM6/22/25
to Harbour Minigui
Hello
I appreciate your collaboration in sharing your solution with me
I will evaluate and test it.
I managed to make an adjustment in sendmail2 to send multiple attachments in a single email, but I am still having difficulty
understanding how to break the line in the body text of the email.
Thank you very much for your attention
Reply all
Reply to author
Forward
0 new messages