Topics Sending an Multipart message Storing images Getting confirmation Session tracking using PHP Graphics Input Validators Cookies
Sending an –Configuring PHP for ing –The Mail Function –Sending with Headers –Sending HTML mail
Configuring PHP for ing If u r using any *NIX environment- sendmail is automatically installed Any hosting Service – service provider will provide sendmail settings If we are not using sendmail- –We can use existing SMTP service –Install a mailserver such as Mailtraq
Parameters to set for configuration Once the mail server is installed we need to modify php.ini There are some important parameters to setup –SMTP: Set this to the ip address or DNS name of your SMTP server. (windows installation only) default value : localhost –smtp_port: Set this to the port PHP uses to connect to the SMTP server (windows installation only) default value 25 –Sendmail_from: The From address used by default by the PHP mail() command –Sendmail_path: the path to the sendmail program(*NIX servers only) most servers usr/sbin/sendmail
The Mail Function The mail() is used to Send mail using PHP Synatx bool mail ( string $to, string $subject, string $message, [ string $additional_headers,[ string $additional_parameters ]] )
parameters –to Receiver, or receivers of the mail. The formatting of this string must comply –subject Subject of the to be sent. – message Message to be sent. Each line should be separated with a LF (\n). Lines should not be larger than 70 characters The remaining two are optional
Optional Parameters additional_headers (optional) String to be inserted at the end of the header. This is typically used to add extra headers (From, Cc, and Bcc). Multiple extra headers should be separated with a LF (\n). additional_parameters (optional) The additional_parameters parameter can be used to pass additional flags as command line options to the program configured to be used when sending mail, as defined by the sendmail_path configuration setting.
Multiple Recipients Sending message to multiple recipients- the addresses must be seperated with comma. Syntax:
Some Examples The wordwrap() function wraps a string into new lines when it reaches a specific length. This function returns the string broken into lines on success, or FALSE on failure. Example #1 Sending mail. Using mail() to send a simple <?php // The message $message = "Line 1\nLine 2\nLine 3"; // In case any of our lines are larger than 70 characters // we should use wordwrap() $message = wordwrap($message, 70); // Send 'My Subject', $message); ?>
Sending With Headers Example #2 Sending mail with extra headers. The addition of basic headers, telling the MUA the From and Reply-To addresses:
Collecting Data and Sending Mail we are going to create two Web pages, mailform.html and .php The file mailform.html will collect the data you are going to send. The file .php will actually send the message, using the data you enter. Save the first page as mailform.html. Note that mailform.html doesn’t actually have any PHP code in it. It simply collects the required data in an HTML form. Save the second page as .php. This second page will take the values entered into the first page, and send them in an .
Mail Form.html Enter DATA enter to address enter ur subject Enter ur Message
.php <?php ini_set("sendmail_from", ini_set("SMTP","mail.vit.ac.in"); echo " Welcome to ing with PHP "; echo " "; $to= $_REQUEST["to"]; $subject= $_REQUEST["subject"]; $message= $_REQUEST["MESSAGE"]; $from = $headers = "From:". $from; $send=mail($to,$subject,$message,$headers); if($send) echo "congrats! The following Mail has been Sent "; echo " To: $to "; echo " From: $from "; echo " Subject: $subject "; echo " Message: "; echo $message; else echo "error in sending mail...."; ?>
Running the script Load up the first page, mailform.html, in your browser, and enter some data. Make sure you use valid addresses so that you can verify their receipt. Click the Send button. A second page appears, similar to the one shown below. Open your client and check your e- mail
How it works Remember that if your .php page is not in the same folder as mailform.html, you have to provide the correct path: Once the user presses the Send button, .php is loaded. The first step in your PHP code assigns all the fields from mailform.html to variables. In order to specify from whom the is coming, use the optional fourth parameter for the mail() function, headers. $headers = “From: “. $from. “\r\n”; The mail() function returns a value of True if it is successful and False if it fails.
The mail() function returns a value of True if it is successful and False if it fails. You will use this function to make your application a little more user-friendly: $mailsent = mail($to, $subject, $message, $headers); if ($mailsent) { echo “Congrats! The following message has been sent: ”; echo “ To: $to ”; echo “ From: $from ”; echo “ Subject: $subject ”; echo “ Message: ”; echo $message; } else { echo “There was an error...”; } ?>