Sunday, October 23, 2011

PHP Captcha Class Using Session Variable

Following php class will generate a captcha at runtime using random numbers assigned to session variable and display it as an image.
Captcha generated using this class


// Set the content-type
session_start();


header('Content-Type: image/png');


// Create the image
$im = imagecreatetruecolor(180, 40);


// Create some colors
$white = imagecolorallocate($im, 255, 248, 201);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 135, 85, 70);
imagefilledrectangle($im, 2, 2, 178, 38, $white);


// The text to draw
$text = $_SESSION['secure'];
// Replace path by your own font path
$font = 'nyala.ttf';


// Add some shadow to the text
imagettftext($im, 35, 0, 0, 0, $black, $font, $text);


// Add the text
imagettftext($im, 38, 0, 20, 30, $black, $font, $text);


for ($i = 1; $i <= 150; $i++) {
    $x1=rand (1,180);
$y1=rand (1,180);
$x2=rand (1,180);
$y2=rand (1,180);
imageline($im ,$x1,$y1,$x2,$y2, $black);
}
$rey2 = imagecolorallocate($im, 255, 248, 201);


for ($i = 1; $i <= 30; $i++) {
    $x1=rand (1,180);
$y1=rand (1,180);
$x2=rand (1,180);
$y2=rand (1,180);
imageline($im ,$x1,$y1,$x2,$y2, $rey2);
}
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>



Save the above file with the name generate.php or any other name you like.


How to call this Class 
In the page you want add this captcha,add following code to header



session_start();
if (!isset($_POST['secure'])) {
$_SESSION['secure'] =rand(100000, 999999);
}

and in img tag src call to
src="generate.php"




Tuesday, February 22, 2011

VB.Net Send Email With Attachment (SMTP)

'Send Mail Class


Imports Microsoft.VisualBasic
Imports System.Net.Mail
Imports System.Text


Public Class SendEmail
    Dim smtpCredentials As New Net.NetworkCredential("username", "password")
    Public Sub SendEmailMessage(ByVal strFrom As String, ByVal strTo() _
    As String, ByVal strSubject _
    As String, ByVal strMessage _
    As String, ByVal fileList() As String)
        'This procedure takes string array parameters for multiple recipients and files
        Try
            For Each item As String In strTo
                'For each to address create a mail message
                Dim MailMsg As New MailMessage(New MailAddress(strFrom.Trim()), New MailAddress(item))
                MailMsg.BodyEncoding = Encoding.Default
                MailMsg.Subject = strSubject.Trim()
                MailMsg.Body = strMessage.Trim() & vbCrLf
                MailMsg.Priority = MailPriority.High
                MailMsg.IsBodyHtml = True


                'attach each file attachment
                For Each strfile As String In fileList
                    If Not strfile = "" Then
                        Dim MsgAttach As New Attachment(strfile)
                        MailMsg.Attachments.Add(MsgAttach)
                    End If
                Next


                'Smtpclient to send the mail message
                Dim SmtpMail As New SmtpClient
                SmtpMail.UseDefaultCredentials = False
                SmtpMail.Host = "hostname"
                SmtpMail.EnableSsl = True
                SmtpMail.Credentials = smtpCredentials
                SmtpMail.Send(MailMsg)
            Next
            MessageBox.Show("Success")
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub


    Public Sub SendEmailMessage(ByVal strFrom As String, ByVal strTo _
    As String, ByVal strSubject _
    As String, ByVal strMessage _
    As String, ByVal file As String)
        'This procedure overrides the first procedure and accepts a single
        'string for the recipient and file attachement 
        Try
            Dim MailMsg As New MailMessage(New MailAddress(strFrom.Trim()), New MailAddress(strTo))
            MailMsg.BodyEncoding = Encoding.Default
            MailMsg.Subject = strSubject.Trim()
            MailMsg.Body = strMessage.Trim() & vbCrLf
            MailMsg.Priority = MailPriority.High
            MailMsg.IsBodyHtml = True


            If Not file = "" Then
                Dim MsgAttach As New Attachment(file)
                MailMsg.Attachments.Add(MsgAttach)
            End If


            'Smtpclient to send the mail message
            Dim SmtpMail As New SmtpClient
            SmtpMail.UseDefaultCredentials = False
            SmtpMail.Host = "mail.elections.gov.mv"
            SmtpMail.Credentials = smtpCredentials
            SmtpMail.Send(MailMsg)
            MessageBox.Show("List has been Successfully mailed to Elections Commission", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
End Class




'Send Mail Button Code



Private Sub btnSendEmail_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSendEmail.Click
        Dim SendEmail As New SendEmail
        Dim SendTo As String
        Dim FileAttach As String = Nothing
        Dim strSubject As String
        Dim strMessage As String




        SendTo = "to"


        FileAttach = "location of the Attachment"


        strSubject = "Subject"


        strMessage = "Message" 'The body encoding is set to HTML 


        SendEmail.SendEmailMessage("frommail", SendTo, strSubject, strMessage, FileAttach)
    End Sub


    Private Sub cmbConsit_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbConsit.SelectedIndexChanged
        values(cmbConsit.SelectedItem)
    End Sub