Wednesday, May 2, 2012

Sending E-mail with PHP (basic mail() & Gmail with phpmailer )


Basic PHP mail()

Send a simple email:

<?php
// the message
$msg = "First line of text\nSecond line of text";
// use wordwrap() if lines are longer than 70 characters
$msg = wordwrap($msg,70);
// send email
mail("someone@example.com","My subject",$msg);
?>
Definition and Usage
The mail() function allows you to send emails directly from a script.
Syntax
mail(to,subject,message,headers,parameters);

Basic PHP mail() Function code to send emails from a form

Below is the code for the basic email function. We can take the script and actually use a form on our website to set the variables in the script above to send an email.

<?php
//if "email" variable is filled out, send email
  if (isset($_REQUEST['email']))  {
  
  //Email information
  $admin_email = "someone@example.com";
  $email = $_REQUEST['email'];
  $subject = $_REQUEST['subject'];
  $comment = $_REQUEST['comment'];
  
  //send email
  mail($email, "$subject", $comment, "From:" . $email);
  
  //Email response
  echo "Thank you for contacting us!";
  }
  
  //if "email" variable is not filled out, display the form
  else  {
?>

 <form method="post">
  Email: <input name="email" type="text" /><br />
  Subject: <input name="subject" type="text" /><br />
  Message:<br />
  <textarea name="comment" rows="15" cols="40"></textarea><br />
  <input type="submit" value="Submit" />
  </form>
  
<?php
  }
?>

So let’s now review what the form is actually doing. 
 1. The first part checks to make sure the email input field is filled out. If it is not, then it will display the HTML form on the page. If the email is in fact, set (after the visitor fills out the form), it is ready to send.  
2. When the submit button is pressed, after the form is filled out, the page reloads and reads that the email input is set, so it sends the email.
3. if you get following error,  Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25,
then make sure your php.ini configured correctly you have to configure these values on php.ini file
SMTP = smtp.yourdomain.com  
smtp_port = 25 
username = info@yourdomain.com
password = yourmailpassord
sendmail_from = info@yourdomain.com

Sending E-mail with PHP (Gmail SMTP and phpmailer)

 tutorial for sending PHP Mail using Gmail as your SMTP engine (with help of phpmailer functions.

before you start you must have valid gmail account and web server capable of PHP
  • PHP (you know, PHP: Hypertext Preprocessor). Its a lovely language for developing web pages. If you already knew that, then good. If not, you might want to read a good PHP Tutorial.
  • A local apache+PHP webserver running to test with. I am using  AppServ -http://www.appservnetwork.com/  while on Windows 7.
  • Can edit your PHP.ini configuration file. (If you don’t know what I’m talking about, get really familiar with the above two.
-----------------------------------------------------------------------------------------------------------

  1. If you haven’t done so already, get a PHP server running.
  2. Download PHP-Mailer .
  3. Go to your Apache  web server directory C:\AppServ\www\PHP_MAIL folder (in AppSever), and extract the PHP Mailer into it. I’ll call this folder Root from now on.(it contain - 3 PHP files- class.phpmailer.php , class.pop3.php , class.smtp.php , include   class.phpmailer.php in your test_gmail.php file ,source code is given below)
  4. Now go to Root\PHP-Mailer\examples\, and open up test_smtp_gmail_basic.php.(i have created my own test_gmail.php in side example, with the following content )
  5. Now do some minor editing. (Just change the Emails to your send and receiver, and type your password. )
---------------------------------------------------------------------------------------------
<?php

include("../class.phpmailer.php");

$mail             = new PHPMailer();
$body             = $mail->getFile('contents.html');
$body             = eregi_replace("[\]",'',$body);

$mail->IsSMTP();
$mail->SMTPAuth   = true;  // enable SMTP authentication
$mail->SMTPKeepAlive = true;
$mail->SMTPSecure = "ssl";  // sets the prefix to the servier
$mail->Host       = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port       = 465;// set the SMTP port for the GMAIL server
$mail->Username   = "umeshnarayanan1983@gmail.com";  // GMAIL username
$mail->Password   = "********";// your GMAIL password
$mail->AddReplyTo("umeshnarayanan1983@gmail.com","Umesh Narayanan");
$mail->From       = "umeshnarayanan1983@gmail.com";
$mail->FromName   = "Umesh Narayanan";


$mail->Subject    = "PHPMailer Test Subject via gmail";


$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->WordWrap   = 50; // set word wrap

$mail->MsgHTML($body);

$mail->AddAddress("umeshnarayanan1@gmail.com", "Umeshnarayanan");

$mail->AddAttachment("images/activate.jpg");     // attachment

$mail->IsHTML(true); // send as HTML

if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {
  echo "Email has been sent Sucessfuly!";
}

?>


6.Now, pressing save, you have almost done . Ok, the actual step is you need to edit your PHP.ini file settings. You can find that C:\Windows or  C:\AppServ\php5

or you can Go Start > All Programs > AppServ> Configuration server >PHP Edit the php.ini Configuration  as show below




open the PHP.ini file, then search for “openssl”. Go ahead and remove ‘;’ comment symbol from the start of the line.

extension=php_openssl.dll



  1. Restart your servers (optional -  restart your system )
  2. navigate to http://localhost/PHP_MAIL/examples/test_gmail.php in your browser. ( navigate to your file through browser )
  3. And then check your Receivers Inbox. 

Full source code can be downloaded from the following

Google doc link - https://docs.google.com/open?id=0B30amJFTQ4KbRlExVHNlQWM1QkU
(download and extract the whole folder in your root folder , and change email and password and you have done It!)


if you have any query pls contact me - umeshnarayanan1@gmail.com


7 comments:

  1. Great post indeed and thanks for all the information, it was very helpful i really like that you are providing information on PHP and MYSQL with basic JAVASCRIPT,being enrolled in

    http://www.wiziq.com/course/5871-php-mysql-with-basic-javascript-integrated-course
    i was looking for such information online to assist me on php and mysql and your information helped me a lot. Thanks.

    ReplyDelete
    Replies

    1. Hello all
      am looking few years that some guys comes into the market
      they called themselves hacker, carder or spammer they rip the
      peoples with different ways and it’s a badly impact to real hacker
      now situation is that peoples doesn’t believe that real hackers and carder scammer exists.
      Anyone want to make deal with me any type am available but first
      I‘ll show the proof that am real then make a deal like

      Available Services

      ..Wire Bank Transfer all over the world

      ..Western Union Transfer all over the world

      ..Credit Cards (USA, UK, AUS, CAN, NZ)

      ..School Grade upgrade / remove Records

      ..Spamming Tool

      ..keyloggers / rats

      ..Social Media recovery

      .. Teaching Hacking / spamming / carding (1/2 hours course)

      discount for re-seller

      Contact: 24/7

      fixitrogers@gmail.com

      Delete
  2. Hi Clients!

    We have the fresh and valid USA ssn leads and dead fullz
    99% connectivity with quality
    *If you have any trust issue before any deal you may get few to test
    *Every leads are well checked and available 24 hours
    *Fully cooperate with clients

    *Format of Fullz/leads/profiles
    °First & last Name
    °SSN
    °DOB
    °(DRIVING LICENSE NUMBER)
    °ADDRESS
    (ZIP CODE,STATE,CITY)
    °PHONE NUMBER
    °EMAIL ADDRESS
    °REFERENCE DETAILS
    °BANK ACCOUNT DETAILS

    ****Contact Me****
    *ICQ :748957107

    *Gmail :taimoorh944@gmail.com

    Cost for lead cost $2 for each
    Price can be negotiable if order in bulk

    *Contact soon!
    *I hope a long term deal
    *Thank You

    ReplyDelete

  3. i was lost with no hope for my wife was cheating and had always got away with it because i did not know how or always too scared to pin anything on her. with the help a friend who recommended me to who help hack her phone, email, chat, sms and expose her for a cheater she is. I just want to say a big thank you to ( int.hackers002@gmail.com) . am sure someone out there is looking for how to solve his relationship problems, you can also contact him for all sorts of hacking job..he is fast and reliable. you could also WHATSAPP HIM +1(765) 705-0044

    ReplyDelete
  4. We provide loans for developing businesses and competitive edge, Personal / Business Loans (Secure and Unsecured) Consolidation Loan and many more. Quality services offer. Contact us today via email: triumphantfinanceltd@gmail.com

    ReplyDelete
  5. We have the fresh and valid USA ssn leads
    99% connectivity with quality
    ====================
    *If you have any trust issue you can buy few to test
    *Every leads are well checked and available 24 hours
    *Fully cooperate with clients
    ====================
    >> SSN+DOB
    >> SSN+DOB+DL
    >> Premium high score fullz (also included relative info)
    ====================
    TUTORIALS AVAILABLE FOR
    SPAMMING
    CARDING
    CASHOUTS
    MOBILE DEPOSITS
    >APPLE PAY & ANDROID TAP CASH
    >BANK TRANSFER
    >HOW TO CASHOUT DUMPS+PINS
    >MOBILE DEPOSIT
    ====================
    >SAFE SOCKS5 (USA)
    >SMTP Linux Root
    -->DUMPS+PINS
    (How to use & create dumps with pins track 1 & 2)
    =====================
    Also SELLING
    >SERVER I.P's & proxies in bulk
    >USA EMAILS Combo
    >Fresh Leads for tax returns & w-2 form filling
    >CC's with CVV's (vbv & non-vbv)
    >USA Photo ID'S (Front & back)
    >Payment mode BTC, ETH, LTC, & USDT

    Telegram : @Cyberz_Phoenix
    ICQ : @1001829652a
    WICKR : @cyberzphoenix

    ReplyDelete