mail() in PHP

PHP allows you to send a mail from script. PHP has wide range of options in mail() function to use it.
To send a mail(text) from script
			<?php
				$to = "user@domain.com";
				$subject = "My First Mail Script Demo from PHP";
				$message = "Hello! This is a simple email message i tried from PHP.";
				$from = "user2@domain.com";
				$headers = "From: $from";
				mail($to,$subject,$message,$headers);
				echo "Mail Sent.";
			?>
		
The above code just sends a plain text to the mail specified as sender. In-order to send the mail with text/html, you can more parameters to the header, so that it”ll send’’s an HTML mail. The following two lines tells our PHP script to send an HTML mail message instead of plain text mail. And let us add some more parameters to header like CC and Bcc, which add the CC and Bcc to our mail.
		<?php
			// To send the HTML mail we need to set the Content-type header.
			$headers = "MIME-Version: 1.0rn";
			$headers .= "Content-type: text/html; charset=iso-8859-1rn";
			$headers  .= "From: $fromrn";//options to send to CC and Bcc
			$headers .= "Cc: cc@domain.com";
			$headers .= "Bcc: Bcc@domain.com";
		?>
By using the header we can send the attachment also.
In-addition to this, in PHP we have IMAP library to check the mails from your POP/IMAP server(s). The dll file php_imap.dll has the support for the IMAP function in PHP. This dll will be installed in ext folder under the PHP is in configured. This library can also be used to open streams to POP3 and NNTP servers, but some functions in this library and features are only available on IMAP servers. The following code will check the mail in your POP server’’s Inbox folder and display the
		<?php
			//Open the POP3 Server's using the username and passowrd
			$mbox = imap_open("{pop.server.com:110/pop3}INBOX", "username", "password");
			$MC=imap_check($mbox); //Check the Mailbox using mbox handle
			$MN=$MC->Nmsgs;
			//Fetch mbox''s message''s overview into an Array
			$overview=imap_fetch_overview($mbox,"1:$MN",0);
			$size=sizeof($overview);
			for($i=$size-1;$i>=0;$i--){
				$val=$overview[$i];
				$msg=$val->msgno; //Message #
				$from=$val->from; //From address
				$date=$val->date; //Date Message sent
				$subj=$val->subject; //Subject line of the mail
				echo "<tr><td align=''center''>$msg</td><td>$from</td><td>$date</td><td>$subj</td></tr>";
			}
			//Close the handle
			imap_close($mbox);
		?>	
Apart from the aboce taken parameters from the we can add many parameters into the overview handler. To check the above script in action click here
Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Tags: , , , ,

Leave a Reply

Name and Email Address are required fields. Your email will not be published or shared with third parties.

Get Adobe Flash player