PureSMTP library : mail & attachments (AUTH supported)

All PureFORM, JaPBe, Libs and useful code maintained by gnozal

Moderator: gnozal

gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

PureSMTP library : mail & attachments (AUTH supported)

Post by gnozal »

PureSMTP library (AUTH supported) (PB3.94 : V5.71 OCT 31th 2007 ; PB4.0x : V6.01 JAN 26th 2008 ; PB4.20 : V6.01 MAY 27th 2008 ; PB4.30-4.4x : V7.00 JUL 18th 2009 ; PB4.5x : V7.02 MAR 14th 2011 ; PB4.6x : V8.00 JUL 12th 2012 ; PB5.0x : V8.00 NOV 7th 2012)

Purpose :

Code: Select all

Send e-mails (+/- files attached) with Purebasic as simply as possible.

Features :
- authentication supported [ POP3 before SMTP / SMTP LOGIN ]
- large attachements

Note : TLS/SSL is not supported
Functions :
PB3.94 : http://gnozal.ucoz.com/PureSMTP.htm
PB4.xx : http://gnozal.ucoz.com/PureSMTP_.htm

Example :

Code: Select all

MySMTPServer.s = "test@test"
MySMTPPort.l = 25
If PureSMTP_OpenSMTPConnection(MySMTPServer, MySMTPPort) = #PureSMTP_Ok
  Debug PureSMTP_GetLastServerMessage()
  MailTo.s = "MyName@MyServer"
  MailFrom.s = "PureSMTP@testing"
  Subject.s = "Test (with attachments)"
  MsgBody.s = "Testing PureSMTP"
  ; file attachements
  File1.s = "C:\PureBasic391\Program\PureSMTP.zip"
  If FileSize(File1) <= 0
    File1 = ""
  EndIf
  File2.s = "C:\PureBasic391\Program\PureUPX.exe"
  If FileSize(File2) <= 0
    File2 = ""
  EndIf
  Separator.s = ";"
  If File1 = "" Or File2 = ""
    Separator = ""
  EndIf
  Attachments.s = File1 + Separator + File2
  ; sending mail
  Debug Attachments
  Status.l = PureSMTP_SendMail(MailTo, MailFrom, Subject, MsgBody, Attachments)
  If Status = #PureSMTP_Ok
    Debug "Message 1 : sent"
    Debug "Status = " + Str(Status)
    Debug PureSMTP_GetLastServerMessage()
    Subject = "Test 2 (no attachment)"
    Attachments = ""
    Status.l = PureSMTP_SendMail(MailTo, MailFrom, Subject, MsgBody, Attachments)
    If Status = #PureSMTP_Ok
      Debug "Message 2 : sent"
      Debug "Status = " + Str(Status)
      Debug PureSMTP_GetLastServerMessage()
    Else
      Debug "Message 2 : something went wrong !"
      Debug "Status = " + Str(Status)
      Debug PureSMTP_GetLastServerMessage()
    EndIf
  Else
    Debug "Message 1 : something went wrong !"
    Debug "Status = " + Str(Status)
    Debug PureSMTP_GetLastServerMessage()
  EndIf
  PureSMTP_CloseSMTPConnection()
Else
  Debug "OpenSMTPConnection failed"
  Debug PureSMTP_GetLastServerMessage()
EndIf
Download :
Only available for Purebasic Windows x86
PB3.94 : http://gnozal.ucoz.com/PureSMTP.zip
PB4.00-4.10 : http://gnozal.ucoz.com/PureSMTP_.zip
PB4.20 : http://gnozal.ucoz.com/PureSMTP___.zip
PB4.3x : http://gnozal.ucoz.com/PureSMTP_430.zip
PB4.4x : http://gnozal.ucoz.com/PureSMTP_440.zip
PB4.5x : http://gnozal.ucoz.com/PureSMTP_450.zip
PB4.6x : http://gnozal.ucoz.com/PureSMTP_460.zip
PB5.0x : http://gnozal.ucoz.com/PureSMTP_500.zip

Other libs and tools at http://gnozal.ucoz.com/
Last edited by gnozal on Tue Aug 18, 2009 11:52 am, edited 36 times in total.
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post by Kale »

Works well! :) One little thing. I was a little confused for a while by trying to compare #PureSMTP_Ok with the return value of PureSMTP_OpenSMTPConnection() which always didn't match. I then tested PureSMTP_OpenSMTPConnection() return value on a good connection and found it to be 100 and not 1. Not a big deal but maybe useful to mention it in the doc. Good job though, it even recognises ascii files and encodes them 7bit instead of just base64'ing the lot! 8O :D Nice!
--Kale

Image
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Post by gnozal »

Thanks.
You are right : #PureSMTP_Ok is only valid for PureSMTP_SendMail().
PureSMTP_OpenSMTPConnection() returns 0 if failed ; if <> 0 it's Ok.
Maybe I change this if there is a next version.
User avatar
jqn
User
User
Posts: 97
Joined: Fri Oct 31, 2003 3:04 pm

Post by jqn »

Hi,
NICE library! , thanks. :D

But... some e.mail functions must be added (like send HTML or LOGIN in erver).

Do you think to extend them?

Joaquin
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Post by gnozal »

jqn wrote:Hi,
NICE library! , thanks. :D

But... some e.mail functions must be added (like send HTML or LOGIN in erver).

Do you think to extend them?

Joaquin
HTML : perhaps I would if I knew how it works :oops: but I must say I never looked. My goal was not to build an e-mail client, but to provide a simple way to send some messages / files with Purebasic.

LOGIN : I don't understand what you mean
freedimension
Enthusiast
Enthusiast
Posts: 613
Joined: Tue May 06, 2003 2:50 pm
Location: Germany
Contact:

Post by freedimension »

With some SMTP-Servers you have to Login to the POP-Account first (to verify your identity) in order to send mails.
plouf
Enthusiast
Enthusiast
Posts: 250
Joined: Fri Apr 25, 2003 6:35 pm
Location: Athens,Greece

Post by plouf »

gnozal wrote:
jqn wrote:Hi,

LOGIN : I don't understand what you mean
maybe he means the username/password authentication some smtp servers requiere ?


btw nice job it saves some of my time :wink:
Christos
User avatar
jqn
User
User
Posts: 97
Joined: Fri Oct 31, 2003 3:04 pm

Post by jqn »

Hi again,

I'm using BLAT.DLL for send e.mail from within a program, but it is'nt "Pure" construct, and some options do not work fine ("-charset" by example).


All options, from BLAT help:

Code: Select all

;===========================================================================
;-USAGE
;===========================================================================
;
; syntax:
;   Blat <filename> -To <recipient> [optional switches (see below)]
;   Blat -install <server addr> <sender's addr> [<try>[<port>[<profile>]]] [-q]
;   Blat -profile [-delete | "<default>"] [profile1] [profileN] [-q]
;   Blat -h [-q]
;
; -install <server addr> <sender's addr> [<try n times> [<port> [<profile>]]]
;      : set's SMTP server, sender, number of tries and port for profile
;        (<try n times> And <port> may be replaced by '-').
;
; <filename>     : file with the message body ('-' For console input, End with ^Z)
; -To <recipient> : recipient list (also -t) (comma separated)
; -tf <recipient> : recipient list filename
; -subject <subj>: subject Line (also -s)
; -f <sender>    : overrides the Default sender address (must be known To server)
; -i <addr>      : a 'From:' address, not necessarily known To the SMTP server.
; -cc <recipient>: carbon copy recipient list (also -c) (comma separated)
; -cf <file>     : cc recipient list filename
; -bcc <recipient>: blind carbon copy recipient list (also -bcc) (comma separated)
; -bf <file>     : bcc recipient list filename
; -organization <organization>: Organization field (also -o And -org)
; -body <text>   : Message body
; -x <X-Header: detail>: Custom 'X-' header.  eg: -x "X-INFO: Blat is Great!"
; -r             : Request Return receipt.
; -d             : Request disposition notification.
; -h             : displays this help.
; -q             : supresses *all* output.
; -Debug         : Echoes server communications To screen (disables '-q').
; -noh           : prevent X-Mailer header from showing homepage of blat
; -noh2          : prevent X-Mailer header entirely
; -p <profile>   : send with SMTP server, user And port defined in <profile>.
; -priority <pr> : set message priority 0 For low, 1 For high.
; -server <addr> : Specify SMTP server To be used. (optionally, addr:port)
; -port <port>   : port To be used on the server, defaults To SMTP (25)
; -hostname <hst>: Select the hostname used To send the message
; -mime          : MIME Quoted-Printable Content-Transfer-Encoding.
; -enriched      : Send an enriched text message (Content-Type=text/enriched)
; -html          : Send an HTML message (Content-Type=text/html)
; -uuencode      : Send (binary) file UUEncoded
; -base64        : Send (binary) file using base64 (binary Mime)
; -try <n times> : how many time blat should try To send. from '1' To 'INFINITE'
; -attach <file> : attach binary file To message (may be repeated)
; -attacht <file>: attach text file To message (may be repeated)
; -ti <n>        : Set timeout To 'n' seconds.
; -u <username>  : Username For AUTH LOGIN (use with -pw)
; -pw <password> : Password For AUTH LOGIN (use with -u)
; -log <filename>: Log everything but usage To <filname>
; -plain         : Use AUTH PLAIN To login To SMTP server (use with -u -pw)
; -charset <cs>  : User defined charset.  The Default is ISO-8859-1
;
; Note that If the '-i' option is used, <sender> is included in 'Reply-to:'
; And 'Sender:' fields in the header of the message.
;
; Optionally, the following options can be used instead of the -f And -i
; options:
;
; -mailfrom <addr>   The RFC 821 MAIL From: statement
; -from <addr>       The RFC 822 From: statement
; -replyto <addr>    The RFC 822 Reply-To: statement
; -returnpath <addr> The RFC 822 Return-Path: statement
; -sender <addr>     The RFC 822 Sender: statement
;
; For backward consistency, the -f And -i options have precedence over these
; RFC 822 defined options. If both -f And -i options are omitted then the
; RFC 821 MAIL FROM statement will be defaulted To use the installation-defined
; Default sender address
;
; EXAMPLES:
;
; Blat -install smtphost.bar.com foo@bar.com          // Sets host And userid
; Blat -install smtphost.bar.com foo                  // Sets host And userid
; Blat -install smtphost.bar.com                      // Sets host only
;
; Blat myfile.txt -subject "A file for pedro" -To foo@bar.com
; // Sends a file with subject line "A file for pedro"
;
; Blat myfile.txt -subject "A file for pedro" -To foo@bar.com -q
; // Sends a file with subject line "A file for pedro" And does not print
; // informative messages on the console
;
; Blat myfile.txt -subject "A file for mark" -To fee@fi.com -f foo@bar.com
; // -f option overrides the Default sender
;
; Blat myfile.txt -subject "A file for pedro" -To foo@bar.com -i "devil@fire.hell"
; // -i replaces "From:" line address (but leaves Reply-To: And Sender: lines)
;
; Blat myfile.txt -subject "animals" -To fee@fi.com -cc "moo@grass.edu,horse@meadow.hill"
; // -c mails carbon copies To users moo@grass.edu And horse@meadow.hill
;
; Blat.exe BLAT.ZIP -subject "file to gil" -To foo@bar.com -base64
; // Sends the binary file BLAT.ZIP To Gilles in MIME Base 64 format
;
; Blat.exe BLAT.ZIP -subject "file to gil" -To foo@bar.com -uuencode
; // Sends the binary file BLAT.ZIP To Gilles in the old UUEncode format
;
; Blat myfile.txt -subject "oumpla" -To foo@bar.com -attach c:\myfolder\*.txt
; // Sends a file with subject line "oumpla", attach all files with extension "TXT"
; // in folder "myfolder" on drive "C:". Note: you must specify a filename/pattern,
; // "-attach .\*.*" will send all files in current folder.
; // "-attach ." will not send anything And WILL NOT GIVE YOU ANY WARNING.
;
; Blat myfile.txt -To fee@fi.com -server smtp.domain.com -port 6000
; // sends the message through SMTP server smtp.domain.com at port 6000
;
; Blat myfile.txt -To fee@fi.com -hostname friend
; // tells the SMTP that this computer is called "friend"
;
; The DLL works in exactly the same way as Blat.exe !
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Post by gnozal »

freedimension wrote:With some SMTP-Servers you have to Login to the POP-Account first (to verify your identity) in order to send mails.
I didn't know this (didn't found it in the SMTP reference http://www.salemioche.com/smtp/821tm.php) :oops:
I will see if I find some docs.
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Post by gnozal »

Library update (see first post of thread)

Changes :
- added PureSMTP_GetLastServerMessage() function : get last SMTP server message (usefull after #PureSMTP_BadResponse)
- all functions return the same codes (#PureSMTP_Ok etc...)
- bug fix

About username/password authentication :
I don't have any example. What should I do : send USER, wait for '+OK', send PASS, wait for '+OK', and then continue the SMTP stuff (HELO etc...) ?
User avatar
GeoTrail
Addict
Addict
Posts: 2794
Joined: Fri Feb 13, 2004 12:45 am
Location: Bergen, Norway
Contact:

Post by GeoTrail »

! PureSMTP.zip: The archive is either in unknown format or damaged
:(
I Stepped On A Cornflake!!! Now I'm A Cereal Killer!
User avatar
GeoTrail
Addict
Addict
Posts: 2794
Joined: Fri Feb 13, 2004 12:45 am
Location: Bergen, Norway
Contact:

Post by GeoTrail »

Nerver mind, it was my download manager that messed it up :oops:
I Stepped On A Cornflake!!! Now I'm A Cereal Killer!
plouf
Enthusiast
Enthusiast
Posts: 250
Joined: Fri Apr 25, 2003 6:35 pm
Location: Athens,Greece

Post by plouf »

gnozal wrote:Library update (see first post of thread)

About username/password authentication :
I don't have any example. What should I do : send USER, wait for '+OK', send PASS, wait for '+OK', and then continue the SMTP stuff (HELO etc...) ?
http://www.fehcom.de/rfc/rfc2554.txt

(from a google search) however i have not met a SMTP server requiring it though most smtp server restrict access from your ip/sender email or
require you to login in theri pop3 acount before ...

even thought both MozillaMail /Outlook supports it
Christos
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Post by gnozal »

Library updated 18 oct 2004 (see first post of thread)

Changes :
- added function PureSMTP_OpenPOP3ThenSMTPConnection(POP3Server.s, POP3Port.l, POP3UserName.s, POP3UserPassword.s, SMTPServer.s, SMTPPort.l)
Open connection to POP3 server (authentification) and then to SMTP server
I could not test it, so tell me if it works !
User avatar
GeoTrail
Addict
Addict
Posts: 2794
Joined: Fri Feb 13, 2004 12:45 am
Location: Bergen, Norway
Contact:

Post by GeoTrail »

How do I add more lines to the body?
I tried this but none of them worked

Code: Select all

MsgBody.s          = "Sending an email with PB.\nIs this a second line?"

Code: Select all

MsgBody.s          = "Sending an email with PB."+Chr(10)+"Is this a second line?"
I Stepped On A Cornflake!!! Now I'm A Cereal Killer!
Post Reply