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>";
    }
}
?>

No comments:

Post a Comment