hbtip and Email

General Help regarding HMG, Compilation, Linking, Samples

Moderator: Rathinagiri

melliott42
Posts: 119
Joined: Wed Feb 18, 2009 2:14 pm

hbtip and Email

Post by melliott42 »

Hello,

I am looking for the easiest way to send simple emails using HMG (no attachments). It seems it can be done via HBTIP. I am not sure how to get started with this.

1. Is HBTIP built into HMG?
2. Where can I find an example of using HBTIP to send an email?

Thanks,

Michael
User avatar
Rathinagiri
Posts: 5480
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Contact:

Re: hbtip and Email

Post by Rathinagiri »

Yes. HBtip is already built in HMG via lib file C:\hmg\HARBOUR\lib\libhbtip.a

HBTIP samples can be downloaded from Harbour sourcecodes.

Here is a test sample from the sources.

Code: Select all

/*
 * $Id: tipmmail.prg 8751 2008-06-19 00:02:50Z vszakats $
 */

/******************************************
* TIP test
* Mail - reading and writing multipart mails
*
* Creating a mail message.
* This will create a valid mail message, using
* the set of files given in the command line.
*
* Usage:
* testmmail [options] attachment1, attachment2...
*  options:
*    -h              Help
*    -f "from"       Set "mail from" field
*    -t "to"         Set "mail to" field
*    -s "subject"    Set mail subject
*    -b "body"       Set mail body (or message)
*    -m "bodyfile"   Set mail body using a file
*
*
* This test writes data to standard output, and is
* compiled only under GTCGI;
*****/

PROCEDURE MAIN( ... )
   LOCAL oMail, cData, i, oAttach
   LOCAL cFname, cFExt

   IF PCount() == 0
      Usage()
      QUIT
   ENDIF

   oMail := TipMail( "This is the body of the mail" )
   oMail:hHeaders[ "Content-Type" ] := "text/plain; charset=iso8851"
   oMail:hHeaders[ "Date" ]  := Tip_Timestamp()

   i := 1
   DO WHILE i < PCount()
      cData := hb_PValue(i)

      IF lower( cData ) == "-h"
         Usage()
         QUIT
      ENDIF

      IF lower( cData ) == "-f"
         i++
         cData := hb_PValue(i)
         IF cData != NIL
            oMail:hHeaders[ "From" ] := cData
         ENDIF
      ELSEIF lower( cData ) == "-t"
         i++
         cData := hb_PValue(i)
         IF cData != NIL
            oMail:hHeaders[ "To" ] := cData
         ENDIF
      ELSEIF lower( cData ) == "-s"
         i++
         cData := hb_PValue(i)
         IF cData != NIL
            oMail:hHeaders[ "Subject" ] := cData
         ENDIF
      ELSEIF lower( cData ) == "-b"
         i++
         cData := hb_PValue(i)
         IF cData != NIL
            oMail:SetBody( cData + e"\r\n" )
         ENDIF
      ELSEIF lower( cData ) == "-m"
         i++
         cData := hb_PValue(i)
         IF cData != NIL
            cData := Memoread( cData )
            IF Empty(cData)
               ? "FATAL: Can't read", hb_PValue(i)
               QUIT
            ENDIF
            oMail:SetBody( cData + e"\r\n")
         ENDIF
      ELSE  // it is an attachment file
         cData := Memoread( cData )
         IF Empty(cData)
            ? "FATAL: Can't read attachment", hb_PValue(i)
            QUIT
         ENDIF
         oAttach := TipMail():New()

         oAttach:SetEncoder( "base64" )
         //TODO: mime type magic auto-finder
         HB_FNameSplit( hb_PValue(i),,@cFname, @cFext )
         // Some EMAIL readers use Content-Type to check for filename
         oAttach:hHeaders[ "Content-Type" ] := ;
               "application/X-TIP-Attachment; filename=";
               + cFname + cFext
         // But usually, original filename is set here
         oAttach:hHeaders[ "Content-Disposition" ] := ;
               "attachment; filename=" + cFname + cFext
         oAttach:SetBody( cData )

         oMail:Attach( oAttach )
      ENDIF

      i++
   ENDDO

   /* Writing stream */
   FWrite( 1, oMail:ToString() )
RETURN


PROCEDURE Usage()
   ? "Usage:"
   ? "testmmail [options] attachment1, attachment2..."
   ? "  options:"
   ? "    -h              Help"
   ? '    -f "from"       Set "mail from" field'
   ? '    -t "to"         Set "mail to" field'
   ? '    -s "subject"    Set mail subject'
   ? '    -b "body"       Set mail body (or message)'
   ? '    -m "bodyfile"   Set mail body using a file'
   ?
   ?
RETURN

Code: Select all

/*
 * $Id: tipmail.prg 8751 2008-06-19 00:02:50Z vszakats $
 */

/******************************************
* TIP test
* Mail - reading and writing multipart mails
*
* Test for reading a multipart message (that must already
* be in its canonical form, that is, line terminator is
* CRLF and it must have no headers other than SMTP/Mime).
*
* This test writes data to standard output, and is
* compiled only under GTCGI;
*****/

PROCEDURE MAIN( cFileName )
   LOCAL oMail, cData, i

   IF cFileName != NIL
      cData := MemoRead(cFileName)
      IF Ferror() > 0
         ? "Can't open", cFileName
         QUIT
      ENDIF
   ENDIF
   oMail := TipMail():New()
   IF oMail:FromString( cData ) == 0
      ? "Malformed mail. Dumping up to where parsed"
   ENDIF

   WITH OBJECT oMail
      ? "-------------============== HEADERS =================--------------"
      FOR i := 1 TO Len( :hHeaders )
         ? hb_HKeyAt( :hHeaders, i ), ":", hb_HValueAt( :hHeaders, i )
      NEXT
      ?

      ? "-------------============== RECEIVED =================--------------"
      FOR EACH cData IN :aReceived
         ? cData
      NEXT
      ?

      ? "-------------============== BODY =================--------------"
      ? :GetBody()
      ?

      DO WHILE :GetAttachment() != NIL
         ? "-------------============== ATTACHMENT =================--------------"
         ? :NextAttachment():GetBody()
         ?
      ENDDO

   END

   ? "DONE"
   ?
   /* Writing stream */
   //FWrite( 1, oMail:ToString() )
RETURN
In our forum too, Roberto had started thread about this. Let me search and show you.
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.
User avatar
Rathinagiri
Posts: 5480
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Contact:

Re: hbtip and Email

Post by Rathinagiri »

Yes, here it is. :)
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.
melliott42
Posts: 119
Joined: Wed Feb 18, 2009 2:14 pm

Re: hbtip and Email

Post by melliott42 »

rathinagiri,

Thanks for posting! I think you can see from my other posts once I get a good example I am off to the races. :-)

Though I greatly appreciate your posts I am having a difficult time deciphering HBTIP. I am very glad it it built in.

How would you call the object or function in a manner to do something like this:

local nRetVal, cTo, cFrom, cSMTPServer, cSubject, cBody
cSMTPServer := '192.168.1.101'
cTo := 'rathinagiri@mydestinaation.com'
cFrom := 'michael@mysource.com'
cSubject := 'Test Email'
cBody := 'Hello email world!'

nRetVal := sendEmail(cTo, cFrom, cSMTPServer, cSubject, cBody)
if nRetVal != 0
msginfo('Uh Oh!')
end if

Thanks again for your advice,

Michael
User avatar
Rathinagiri
Posts: 5480
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Contact:

Re: hbtip and Email

Post by Rathinagiri »

Yes. That would definitely be so simple. I think we can code a small udf to do so.
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.
User avatar
Roberto Lopez
HMG Founder
Posts: 4012
Joined: Wed Jul 30, 2008 6:43 pm

Re: hbtip and Email

Post by Roberto Lopez »

rathinagiri wrote:Yes. That would definitely be so simple. I think we can code a small udf to do so.
The following function is part of HBTIP and is ready to use:

Code: Select all


FUNCTION HB_SendMail( cServer, nPort, cFrom, aTo, aCC, aBCC, cBody, cSubject, aFiles, cUser, cPass, cPopServer, nPriority, lRead, lTrace, lPopAuth, lNoAuth, nTimeOut, cReplyTo )
   /*
   cServer    -> Required. IP or domain name of the mail server
   nPort      -> Optional. Port used my email server
   cFrom      -> Required. Email address of the sender
   aTo        -> Required. Character string or array of email addresses to send the email to
   aCC        -> Optional. Character string or array of email adresses for CC (Carbon Copy)
   aBCC       -> Optional. Character string or array of email adresses for BCC (Blind Carbon Copy)
   cBody      -> Optional. The body message of the email as text, or the filename of the HTML message to send.
   cSubject   -> Optional. Subject of the sending email
   aFiles     -> Optional. Array of attachments to the email to send
   cUser      -> Required. User name for the POP3 server
   cPass      -> Required. Password for cUser
   cPopServer -> Required. Pop3 server name or address
   nPriority  -> Optional. Email priority: 1=High, 3=Normal (Standard), 5=Low
   lRead      -> Optional. If set to .T., a confirmation request is send. Standard setting is .F.
   lTrace     -> Optional. If set to .T., a log file is created (sendmail<nNr>.log). Standard setting is .F.
   lNoAuth    -> Optional. Disable Autentication methods
   nTimeOut   -> Optional. Number os ms to wait default 20000 (20s)
   cReplyTo   -> Optional.
   */
Regards/Saludos,

Roberto


(Veritas Filia Temporis)
melliott42
Posts: 119
Joined: Wed Feb 18, 2009 2:14 pm

Re: hbtip and Email

Post by melliott42 »

Hello,

I do not have a POP server (just an SMTP server). Can you please recommend the values to use for the POP related fields?

Thanks,

Michael
User avatar
Rathinagiri
Posts: 5480
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Contact:

Re: hbtip and Email

Post by Rathinagiri »

POP is to retrieving the emails from a mailbox. Isn't it? I would checkup.
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.
melliott42
Posts: 119
Joined: Wed Feb 18, 2009 2:14 pm

Re: hbtip and Email

Post by melliott42 »

I just need to send via SMTP.
User avatar
Rathinagiri
Posts: 5480
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Contact:

Re: hbtip and Email

Post by Rathinagiri »

As Roberto pointed out, we can use the following function.

Code: Select all

FUNCTION hb_SendMail( cServer, nPort, cFrom, xTo, xCC, xBCC, cBody, cSubject, aFiles, cUser, cPass, cPopServer, nPriority, lRead, bTrace, lPopAuth, lNoAuth, nTimeOut, cReplyTo, lTLS, cSMTPPass, cCharset, cEncoding )
   /*
   cServer    -> Required. IP or domain name of the mail server
   nPort      -> Optional. Port used my email server
   cFrom      -> Required. Email address of the sender
   xTo        -> Required. Character string or array of email addresses to send the email to
   xCC        -> Optional. Character string or array of email adresses for CC (Carbon Copy)
   xBCC       -> Optional. Character string or array of email adresses for BCC (Blind Carbon Copy)
   cBody      -> Optional. The body message of the email as text, or the filename of the HTML message to send.
   cSubject   -> Optional. Subject of the sending email
   aFiles     -> Optional. Array of attachments to the email to send
   cUser      -> Required. User name for the POP3 server
   cPass      -> Required. Password for cUser
   cPopServer -> Required. POP3 server name or address
   nPriority  -> Optional. Email priority: 1=High, 3=Normal (Standard), 5=Low
   lRead      -> Optional. If set to .T., a confirmation request is send. Standard setting is .F.
   bTrace     -> Optional. If set to .T., a log file is created (smtp-<nNr>.log). Standard setting is NIL.
                           If a block is passed, it will be called for each log event with the message a string, no param on session close.
   lPopAuth   -> Optional. Do POP3 authentication before sending mail.
   lNoAuth    -> Optional. Disable Autentication methods
   nTimeOut   -> Optional. Number os ms to wait default 20000 (20s)
   cReplyTo   -> Optional.
   */
From the hbtip library source, we can see that if lPopAuth is false, then we need not give cPopServername. Kindly check with a sample.
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.
Post Reply