One way of sending mail from IGOR

A quick and direct solution of sending mail is available if you have the base64.xop and SOCKIT files installed, and the server doesn't require secure login. It is also possible to send attachments, but this requires slightly more effort as the attachment needs to be encoded in base64 (which is possible, if I get round to writing it).

Here is some sample code that demonstrates using the SMTP LOGIN protocol (another option may be PLAIN, see http://www.technoids.org/saslmech.html#appA) :

string mailServer = "mail.mymailserver.com"
string username = base64encode("myusername")
string password = base64encode("mypassword")

variable sock       //holds the SOCKIT reference
make/t buf          //holds the output from the SOCKIT connection

sock = sockitopenconnection(mailServer,25,buf,"","",1)  //open a SOCKIT connection to the mail server

sockitsendmsg(sock,"AUTH LOGIN\n")      //SMTP LOGIN, NOT VERY SECURE
sockitsendmsg(sock,username+"\n")
sockitsendmsg(sock,password+"\n")

sockitsendmsg(sock,"HELO whatever.whatever.com\n")      //say HELO to the server
sockitsendmsg(sock,"MAIL FROM: username@mymailserver.com\n")        //say who you are
sockitsendmsg(sock,"RCPT TO: poo@mymailserver.com\n")       //say who the recipient is

sockitsendmsg(sock,"DATA\n")        //start the message
sockitsendmsg(sock,"Subject:test a message\n\n")        //subject line, note double newline is required
sockitsendmsg(sock,"This is a test message\n")      //the message

sockitsendmsg(sock,".\n")       //finish the message and send

sockitcloseconnection(sock) //close the SOCKIT connection
I'm updating this example with SockIT operations. Also, some mail servers require CRLF in places, or require HELO before AUTH LOGIN. For example, this format will work for Yahoo mail. For servers that require SSL, like gmail, see http://www.igorexchange.com/node/952.

For some reason, this only works without the /q flag in sockitiopenconnection. Maybe printing output slows Igor down enough to let messages get sent before the socket is closed?

// For yahoo, use smtpServer="smtp.mail.yahoo.com"
function email(selfServer,smtpServer,username,password,from,to,subject,message)
    string selfServer,smtpServer,username,password,from,to,subject,message
     
    variable sock       //holds the SOCKIT reference
    make/o/t buf        //holds the output from the SOCKIT connection
     
    sockitopenconnection /time=1 sock,smtpServer,25,buf //open a SOCKIT connection to the mail server
     
    sockitsendmsg sock,"HELO "+selfServer+"\n"      //say HELO to the server
    sockitsendmsg sock,"AUTH LOGIN\n"       //SMTP LOGIN, NOT VERY SECURE
    sockitsendmsg sock,base64encode(username)+"\r\n"
    sockitsendmsg sock,base64encode(password)+"\r\n"
     
    sockitsendmsg sock,"MAIL FROM:"+from+"\n"       //say who you are
    sockitsendmsg sock,"RCPT TO:"+to+"\n"       //say who the recipient is
     
    sockitsendmsg sock,"DATA\n"     //start the message
    sockitsendmsg sock,"Subject:"+subject+"\r\n"        //subject line, note double newline is required
    sockitsendmsg sock,message+"\r\n"       //the message
     
    sockitsendmsg sock,"\r\n.\r\n"      //finish the message and send
   
    sockitcloseconnection(sock) //close the SOCKIT connection
    killwaves /z buf
end
Could one roll it up like:
function email(selfServer,smtpServer,username,password,from,to,subject,message)
    string selfServer,smtpServer,username,password,from,to,subject,message
       
        string cmd = ""
    variable sock       //holds the SOCKIT reference
    make/o/t buf        //holds the output from the SOCKIT connection
 
    sockitopenconnection/q sock,smtpServer,25,buf   //open a SOCKIT connection to the mail server
        cmd += "HELO "+selfServer+"\n"      //say HELO to the server
    cmd += "AUTH LOGIN\n"       //SMTP LOGIN, NOT VERY SECURE
        cmd += base64encode(username)+"\r\n"
    cmd += base64encode(password)+"\r\n"
 
        cmd += "MAIL FROM:"+from+"\n"       //say who you are
    cmd += "RCPT TO:"+to+"\n"       //say who the recipient is
 
    cmd += "DATA\n"     //start the message
    cmd += "Subject:"+subject+"\r\n"        //subject line, note double newline is required
    cmd += message+"\r\n"       //the message
 
    cmd += "\r\n.\r\n"      //finish the message and send
       
        sockitsendmsg sock, cmd
    sockitcloseconnection(sock) //close the SOCKIT connection
    killwaves /z buf
end


The only trouble is that the server may barf because the whole message arrives at once.

Forum

Support

Gallery

Igor Pro 9

Learn More

Igor XOP Toolkit

Learn More

Igor NIDAQ Tools MX

Learn More