Posts Tagged ‘Mail’

mail() in PHP

Thursday, April 30th, 2009
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)

Apache Commons-Email

Thursday, March 5th, 2009
Apache Commons has many built-in utils. One of the most used util is Commons-Email. Couple of week back had a situation need to send a mail to the users from my application. So i ended here.
The implementation is very simple. Commons-Email is built on top of the Java Mail API.
Some of the mail classes that are provided are as follows:
  • SimpleEmail-This class is used to send basic text based emails.
  • MultiPartEmail-This class is used to send multipart messages.
  • HtmlEmail-This class is used to send HTML formatted emails.
  • EmailAttachment-This is a simple container class to allow for easy handling of attachments.
The following piece of code will send an mail by using Commons-Email
				package com.techmaddy;

				import org.apache.commons.mail.HtmlEmail;
				public class MailMan {
					public static void main(String[] args) {
						try {
							HtmlEmail email = new HtmlEmail();
							email.setHostName("mysmtp.server.com");
							email.addTo("test@test.com", "KMK");
							email.setFrom("admin@ttext", "Admin");
							email.setSubject("WARNING::Be Aware of this");
							email.setHtmlMsg("<b>Read this from techmaddy.com</b>");
							email.send();
						}
						catch(Exception e) {
							e.printStackTrace();
						}
					}
				}
			
If you SMTP Server needs authentication
email.setAuthentication("username", "password");
For more ref Commons-Email
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)
Get Adobe Flash player