SMTP (Simple Mail Transfer Protocol) is the most common, basic protocol for sending an email on the Internet. Having its roots in 1982, it is still at the heart of email sending today in 2014, although many email servers these days lean towards ESMTP - Enhanced SMTP. ESMTP provides, amongst other things, authentication (username & password).
Discussion here.
The ESMTP PB version of this demo also supports plain SMTP (as do all ESMTP clients), so I recommend using that instead of this, but I think it's important to have a pure-SMTP demo to learn from (ESMTP does afterall still use SMTP at its heart), which is why I leave this demo here.
Send to multiple email addresses (ie. BCC and CC emails):
To send to multiple email addresses you simply send the "RCPT TO:" field over and over, once for each email address ... for which you should receive something like "250 recipient <email address> ok" with each address.
Essentially, every email starts out as a BCC - the only difference between BCC and CC is that a CC message includes in its header who it was emailed to (so if you don't include the CC part it will remain a BCC), ie:
From: "Tech Support" <[email protected]>
To: [email protected]
Cc: [email protected], [email protected]
(note: the CC header field doesn't tell the server who to send the email to, we have to repeatedly use RCPT TO for that... the CC header field on the other hand is purely to provide recipients with the information regarding who else has been sent the email)
ESMTP
ESMTP (Enhanced SMTP), for all intents and purposes in regards to this demo is essentially just SMTP but with one additional step at the start - using your username & password to authenticate you. It's also very easy to implement, and here I provide a demo similar to the above, that has SMTP and ESMTP (both PLAIN and LOGIN auth types) support:
Discussion here.
The ESMTP PB version of this demo also supports plain SMTP (as do all ESMTP clients), so I recommend using that instead of this, but I think it's important to have a pure-SMTP demo to learn from (ESMTP does afterall still use SMTP at its heart), which is why I leave this demo here.
Code:
#COMPILE EXE $SMTP_SERV = "mail.myisp.com" %SMTP_PORT = 25 FUNCTION SendEmail (sEmailTo AS STRING, sEmailFrom AS STRING, sSubject AS STRING, sMessage AS STRING) AS LONG '// Returns 0 if successful, or errorcode LOCAL hTCP AS DWORD, sLocalHost AS STRING, sBuf AS STRING hTCP = FREEFILE TCP OPEN PORT %SMTP_PORT AT $SMTP_SERV AS hTCP IF ERR THEN FUNCTION = -1: GOTO ErrSMTP TCP RECV hTCP, 4096, sBuf IF LEFT$(sBuf, 3) <> "220" THEN FUNCTION = -2: GOTO ErrSMTP HOST NAME TO sLocalHost TCP PRINT hTCP, "HELO " & sLocalHost TCP RECV hTCP, 4096, sBuf IF LEFT$(sBuf, 3) <> "250" THEN FUNCTION = -3: GOTO ErrSMTP TCP PRINT hTCP, "MAIL FROM:" & sEmailFrom TCP RECV hTCP, 4096, sBuf IF LEFT$(sBuf, 3) <> "250" THEN FUNCTION = -4: GOTO ErrSMTP TCP PRINT hTCP, "RCPT TO: " & sEmailTo TCP RECV hTCP, 4096, sBuf IF LEFT$(sBuf, 3) <> "250" THEN FUNCTION = -5: GOTO ErrSMTP TCP PRINT hTCP, "DATA" TCP RECV hTCP, 4096, sBuf IF LEFT$(sBuf, 3) <> "354" THEN FUNCTION = -6: GOTO ErrSMTP TCP PRINT hTCP, "Date: " + DATE$ TCP PRINT hTCP, "From: " + sEmailFrom TCP PRINT hTCP, "To: " + sEmailTo 'TCP PRINT hTCP, "Cc: " '// use this field if you want to change a BCC'd email into a CC'd email TCP PRINT hTCP, "Subject: " + sSubject TCP PRINT hTCP, "X-Mailer: PB SMTP client demo" TCP PRINT hTCP, "" TCP PRINT hTCP, sMessage TCP PRINT hTCP, "" TCP PRINT hTCP, "." TCP RECV hTCP, 4096, sBuf IF LEFT$(sBuf, 3) <> "250" THEN FUNCTION = -7: GOTO ErrSMTP TCP PRINT hTCP, "QUIT" TCP RECV hTCP, 4096, sBuf IF LEFT$(sBuf, 3) <> "221" THEN FUNCTION = -8: GOTO ErrSMTP TCP CLOSE hTCP EXIT FUNCTION ErrSMTP: TCP CLOSE hTCP ? "ERROR. Last msg from server = " & sBuf END FUNCTION FUNCTION PBMAIN () AS LONG LOCAL lSent AS LONG, sEmailTo AS STRING, sEmailFrom AS STRING, sSubject AS STRING, sMessage AS STRING sEmailTo = CHR$(34) & "To Name" & CHR$(34) & " <[email protected]>" sEmailFrom = CHR$(34) & "From Name" & CHR$(34) & " <[email protected]>" sSubject = "Testing one two three" sMessage = "Message line 1" & $CRLF & "Line 2" & $CRLF & "Line 3" lSent = SendEmail(sEmailTo, sEmailFrom, sSubject, sMessage) ? "Sent = " & STR$(lSent) & " (0 = success)" #IF %DEF(%PB_CC32) STDOUT "Done, press any key to continue...";: WAITKEY$ #ENDIF END FUNCTION
To send to multiple email addresses you simply send the "RCPT TO:" field over and over, once for each email address ... for which you should receive something like "250 recipient <email address> ok" with each address.
Essentially, every email starts out as a BCC - the only difference between BCC and CC is that a CC message includes in its header who it was emailed to (so if you don't include the CC part it will remain a BCC), ie:
From: "Tech Support" <[email protected]>
To: [email protected]
Cc: [email protected], [email protected]
(note: the CC header field doesn't tell the server who to send the email to, we have to repeatedly use RCPT TO for that... the CC header field on the other hand is purely to provide recipients with the information regarding who else has been sent the email)
ESMTP
ESMTP (Enhanced SMTP), for all intents and purposes in regards to this demo is essentially just SMTP but with one additional step at the start - using your username & password to authenticate you. It's also very easy to implement, and here I provide a demo similar to the above, that has SMTP and ESMTP (both PLAIN and LOGIN auth types) support:
Comment