Real Software Forums
http://forums.realsoftware.com/

SMTP From CGI.
http://forums.realsoftware.com/viewtopic.php?f=2&t=45374
Page 1 of 1

Author:  kript0x [ Mon Sep 17, 2012 2:19 pm ]
Post subject:  SMTP From CGI.

Hey guys trying to send an email from a CGI application.
This is my code:
Dim mail as EmailMessage
dim smtp as smtp
smtp = new smtp
smtp.address = "mail.bttb.net.bd"
smtp.port = 25

mail = New EmailMessage
mail.fromAddress="myrealemail"
mail.subject="hello world"
mail.bodyPlainText = "PLAIN"
mail.bodyHTML = "<strong>HTML</strong>"
mail.headers.appendHeader "X-Mailer","RB Mail"


mail.addRecipient Trim("myemail")


//send the mail
smtp.messages.append mail //add email to list of messages
smtp.SendMail //send message

while smtp.BytesLeftToSend > 0
Context.print("Left to send: " + str(smtp.BytesLeftToSend) )
smtp.Poll
wend
Context.print("Error code:" + str(smtp.LastErrorCode) )


I added a class to my project called smtp with as super SMTPSocket1, however this didn't help.

Author:  J.Sh3ppard [ Mon Sep 17, 2012 5:21 pm ]
Post subject:  Re: SMTP From CGI.

UserName and Password ?

Make sure you are using the correct port for the emailing service.

You may also need to verify that your ISP isn't blocking the emailing port.

I'm sure there are many posts about emailing which you probably should read.

Author:  kript0x [ Tue Sep 18, 2012 3:25 am ]
Post subject:  Re: SMTP From CGI.

J.Sh3ppard wrote:
UserName and Password ?

Make sure you are using the correct port for the emailing service.

You may also need to verify that your ISP isn't blocking the emailing port.

I'm sure there are many posts about emailing which you probably should read.

Username and password aren't needed for public SMTP's (the one in the example is random however I tried to use my ISP's one that worked before).

I've read lots of posts however it's all about how you need that loop at the end.

Error code is 0 btw.

Author:  J.Sh3ppard [ Tue Sep 18, 2012 12:24 pm ]
Post subject:  Re: SMTP From CGI.

Try adding a time delay in that loop so it's not being called too fast repeatedly.

Make sure your delay is long enough for the email to be sent off.

Author:  kript0x [ Tue Sep 18, 2012 2:11 pm ]
Post subject:  Re: SMTP From CGI.

J.Sh3ppard wrote:
Try adding a time delay in that loop so it's not being called too fast repeatedly.

Make sure your delay is long enough for the email to be sent off.


How would I do a proper delay if smtp.BytesLeftToSend returns 0.

Author:  J.Sh3ppard [ Tue Sep 18, 2012 7:43 pm ]
Post subject:  Re: SMTP From CGI.

kript0x wrote:
J.Sh3ppard wrote:
Try adding a time delay in that loop so it's not being called too fast repeatedly.

Make sure your delay is long enough for the email to be sent off.


How would I do a proper delay if smtp.BytesLeftToSend returns 0.



Hi.


I am also seeing 0 always returned but I am not sure if it's a bug.

My email is very short and I don't know if BytesLeftToSend shows you the first bytes sent or if it executes after the first send... "Bytes Left To Send" not "All Bytes totally sent".

Forget using a timed delay, using socket.poll will work and is the correct way to do this.

Don't use BytesLeftToSend to control your while loop.

Use a boolean flag to control your while loop.
Create a boolean flag as a property of app, set it true on run.


While app.Stay_Alive=true
If smtpsock <> nil then
smtpsock.poll
else
print "Did not create socket"
end
Wend


Set the flag to false after the email has been sent using the socket's mail sent event.
That will end your app after the email was successfully sent.

Don't build your email and socket in the while loop.
Don't repeatedly send your email in the while loop either.




I just did this test and sent emails successfully using :
Mac OS X 10.6.8 RS 2012_R1 console with smtpsecure socket with gmail.

Author:  kript0x [ Wed Sep 19, 2012 5:38 am ]
Post subject:  Re: SMTP From CGI.

J.Sh3ppard wrote:
kript0x wrote:
J.Sh3ppard wrote:
Try adding a time delay in that loop so it's not being called too fast repeatedly.

Make sure your delay is long enough for the email to be sent off.


How would I do a proper delay if smtp.BytesLeftToSend returns 0.



Hi.


I am also seeing 0 always returned but I am not sure if it's a bug.

My email is very short and I don't know if BytesLeftToSend shows you the first bytes sent or if it executes after the first send... "Bytes Left To Send" not "All Bytes totally sent".

Forget using a timed delay, using socket.poll will work and is the correct way to do this.

Don't use BytesLeftToSend to control your while loop.

Use a boolean flag to control your while loop.
Create a boolean flag as a property of app, set it true on run.


While app.Stay_Alive=true
If smtpsock <> nil then
smtpsock.poll
else
print "Did not create socket"
end
Wend


Set the flag to false after the email has been sent using the socket's mail sent event.
That will end your app after the email was successfully sent.

Don't build your email and socket in the while loop.
Don't repeatedly send your email in the while loop either.




I just did this test and sent emails successfully using :
Mac OS X 10.6.8 RS 2012_R1 console with smtpsecure socket with gmail.


Hah that worked! How did I not think of that myself...

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/