Popular Posts

Feb 20, 2014

Send mail using SMTP in PHP


Using PHP mailer you can send email usning SMTP.

<?php

function send_mail($mailAddress, $desc) {
    require_once('../phpmailer/class.phpmailer.php');
    include("../phpmailer/class.smtp.php");
    // optional, gets called from within class.phpmailer.php if not already loaded

    $mail = new PHPMailer();

    //$body             = file_get_contents('contents.html');
    //$body             = eregi_replace("[\]",'',$body);

    $mail->IsSMTP(); // telling the class to use SMTP
    //$mail->Host       = "10.48.1.45"; // Virtual SMTP server IP
    $mail->SMTPDebug = 2;                     // enables SMTP debug information (for testing)
    // 1 = errors and messages
    // 2 = messages only
    $mail->SMTPAuth = true;                        // enable SMTP authentication
    $mail->SMTPSecure = "tls";                    // sets the prefix to the servier
    $mail->Host = "ssl://smtp.gmail.com";      // sets GMAIL as the SMTP server
    $mail->Port = 587;                                 // set the SMTP port for the GMAIL server
    $mail->Username = "mail@gmail.com";  // GMAIL username Any ID of lafarge
    $mail->Password = "password";            // GMAIL password of the ID

    $mail->SetFrom('mail@domail.com', 'From Me'); // From whom the mail is sent
    $mail->AddReplyTo("mail@domail.com", "Hello Sera");

    $mail->Subject = "SMTP MAIL";
    $mail->Body = "You have successfully entered to sera's world " . "<a href='http://jubayerdevs.blogspot.com'>" . "Click here" . "</a>";
    $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

    $address = "mail@domail.com";
    if ($address == '') {
        echo "<script>alert('No mail address found for this user. Set a mail address please');</script>";
        die();
    }

    $mail->AddAddress($address, "Lafarge Procurement Software");

    //$mail->AddAttachment("examples/images/phpmailer.png");      // attachment
    //$mail->AddAttachment("examples/images/phpmailer_mini.gif"); // attachment

    if (!$mail->Send()) {
        echo "Mailer Error: " . $mail->ErrorInfo;
    } else {
        echo "<script>alert('Successfully Submitted');</script>";
    }
}
?>

Feb 19, 2014

Excel to Gridview





In .aspx file:  

<form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
    <br />
    <table>
    <tr><td><asp:FileUpload ID="testFileUpload" runat="server" /></td><td><asp:Button ID="btnUpload" runat="server" Height="21px" Text="Upload" Width="92px" onclick="btnUpload_Click" /></td></tr>
    </table>
    <br /> 
    <asp:GridView ID="dataGridView" runat="server">
    </asp:GridView>
    </div>
    </form>



In .cs file:


    string fileName = string.Empty;
    string fileExtension = string.Empty;
    string fileLocation = string.Empty;
    string getExcelSheetName = string.Empty;
    string connectionString = string.Empty;

    DataTable dtExcelSheetName = null;
    DataTable dtExcelRecords = null;

    OleDbConnection con = null;
    OleDbCommand cmd = null;
    OleDbDataAdapter dAdapter = null;


    protected void Page_Load(object sender, EventArgs e)
    {
        cmd = new OleDbCommand();
        dtExcelRecords = new DataTable();
    }
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        try
        {
            if (testFileUpload.HasFile)
            {
                fileName = Path.GetFileName(testFileUpload.PostedFile.FileName);
                fileExtension = Path.GetExtension(testFileUpload.PostedFile.FileName);
                fileLocation = Server.MapPath("~/App_Data/" + fileName);

                GetData(fileLocation);
                Label1.Text = "File is ok to upload.";
            }
        }
        catch (Exception ex)
        {
            Label1.Text = "File is not expected formated.";
        }
    }


    public void GetData(string loc)
    {
        testFileUpload.SaveAs(fileLocation);
        //Check whether file extension is xls or xslx

        if (fileExtension == ".xls")
        {
            connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + loc + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
        }
        else if (fileExtension == ".xlsx")
        {
            connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + loc + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
        }

        dtExcelRecords.Clear();
        dtExcelRecords.Reset();

        //Create OleDB Connection and OleDb Command

        con = new OleDbConnection(connectionString);

        cmd.CommandType = System.Data.CommandType.Text;
        cmd.Connection = con;
        dAdapter = new OleDbDataAdapter(cmd);

        con.Open();
        dtExcelSheetName = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
        getExcelSheetName = dtExcelSheetName.Rows[0]["Table_Name"].ToString();
        cmd.CommandText = "SELECT * FROM [" + getExcelSheetName + "]";
        dAdapter.SelectCommand = cmd;
        dAdapter.Fill(dtExcelRecords);
        con.Close();

        dataGridView.DataSource = dtExcelRecords;
        dataGridView.DataBind();

    }