SMTP with authentication:

Code:

alias sendmail {
  if (%sndmail.busy) {
    echo -a Already sending. Try again later.
    return -1
  }
  set %sndmail.busy 1

  ;------ SERVER SETTINGS ------
  set %sndmail.smtpserver smtp.server.com
  set %sndmail.smtpport 25

  set %sndmail.doauth $true
  set %sndmail.authuser smtp_auth_username
  set %sndmail.authpass smtp_auth_password

  set %sndmail.debug $true

  ;------ SET THESE VALUES THEN CALL /sendmail -----
  set %sndmail.fromaddress from@address.com
  set %sndmail.toaddress to@address.com
  set %sndmail.subject This is the subject
  set %sndmail.body This is the body

  ;------
  ;
  set %sndmail.logfile $+(maillog.,$asctime(dd.mm.yyyy),.txt)
  set %sndmail.state 1

  if ($sock(sendmail)) sockclose sendmail
  write %sndmail.logfile # Connect Attempt to %sndmail.smtpserver $+ : %sndmail.smtpport
  sockopen sendmail %sndmail.smtpserver %sndmail.smtpport
}
;
on *:sockread:sendmail:{ 
  var %temptext
  sockread %temptext
  tokenize 32 %temptext

  ;echo 2 -a %sndmail.state > $1-
  .timersmtp 1 5 mailsockclose $sockname

  write %sndmail.logfile $asctime(HH:nn:ss) -> $1- 
  if ((%sndmail.state == 1) && ($1 == 220)) {
    mailsockwrite EHLO localhost
    maildebug 220 -> EHLO (Introduce ourselves)
    %sndmail.state = 2
  }
  elseif ((%sndmail.state == 2) && ($left($1,4) == 250-)) {
    ; Do nothing
  }
  elseif ((%sndmail.state == 2) && ($1 == 250)) {
    if (%sndmail.doauth) {
      mailsockwrite AUTH LOGIN
      maildebug 250 -> AUTH LOGIN (Begin authentication)
      %sndmail.state = 3
    }
    else {
      mailsockwrite MAIL From: $+ %sndmail.fromaddress
      maildebug 250 -> MAIL (Auth OK; Specify FROM address)
      %sndmail.state = 6
    }
  }
  elseif ((%sndmail.state == 3) && ($1 == 334)) {
    mailsockwrite $encode(%sndmail.authuser,m) 
    maildebug 334 -> $encode(%sndmail.authuser,m) (Send Base64 encoded username)
    %sndmail.state = 4
  }
  elseif ((%sndmail.state == 4) && ($1 == 334)) {
    mailsockwrite $encode(%sndmail.authpass,m)
    maildebug 334 -> $encode(%sndmail.authpass,m) (Send Base64 encoded password)
    %sndmail.state = 5
  }
  elseif ((%sndmail.state == 5) && ($1 == 535)) {
    mailsockwrite QUIT
    maildebug 535 -> QUIT (Auth failed; Disconnect)
    %sndmail.state = -1
  }
  elseif ((%sndmail.state == 5) && ($1 == 235)) {
    mailsockwrite MAIL From: $+ %sndmail.fromaddress
    maildebug 250 -> MAIL (Auth OK; Specify FROM address)
    %sndmail.state = 6
  }

  elseif ((%sndmail.state == 6) && ($1 == 250)) { 
    mailsockwrite RCPT To: $+ %sndmail.toaddress 
    maildebug 250 -> RCPT (Specify TO address)
    %sndmail.state = 7
  }

  elseif ((%sndmail.state == 7) && ($1 == 250)) { 
    mailsockwrite DATA 
    maildebug 250 -> DATA (Initiate BODY specification)
    %sndmail.state = 8
  }

  elseif ((%sndmail.state == 8) && ($1 == 354)) {
    mailsockwrite Subject: %sndmail.subject 
    mailsockwrite $crlf 
    mailsockwrite %sndmail.body 
    mailsockwrite . 
    maildebug 354 -> SEND (Specify BODY data; Send email)
    %sndmail.state = 9
  }
  elseif ((%sndmail.state == 9) && ($1 == 250)) {
    mailsockwrite QUIT
    maildebug 250 -> QUIT (Email accepted; Disconnect)
    %sndmail.state = -1
  }

  elseif ((%sndmail.state < 0) && ($1 == 221)) {
    sockclose $sockname
    maildebug 221 (Quit successful)
    unset %sndmail.*
  }
  else {
    mailsockwrite QUIT
    maildebug -> QUIT (Unhandled result; Disconnect)
    maildebug ** 4ERROR: $1-
    set %sndmail.state -1
  }
} 
;
alias mailsockwrite {
  sockwrite -n $sockname $1-
  write %sndmail.logfile $asctime(HH:nn:ss) <- $1-
}
alias maildebug { if (%sndmail.debug) echo 3 -at $1- }
alias mailsockclose { if ($sock($1)) mailsockwrite $1 QUIT }



Operates the same as my previous code.

If the %sndmail.doauth variable is $true, authentication is done using the specified %sndmail.authuser and %sndmail.authpass . If it is $false, authentication is not done.

-genius_at_work