190 likes | 203 Views
Topics. • Sending an email • Multipart message • Storing images • Getting confirmation • Session tracking using PHP • Graphics Input Validators • Cookies. Sending an Email. – Configuring PHP for Emailing – The Mail Function – Sending Email with Headers
E N D
Topics • Sending an email • Multipart message • Storing images • Getting confirmation • Session tracking using PHP • Graphics Input Validators • Cookies
Sending an Email – Configuring PHP for Emailing – The Mail Function – Sending Email with Headers – Sending HTML mail
Configuring PHP for Emailing • 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 email 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 email 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: mail(“first@email.com, second@email.com”,”hi”, “wazzup”)
Some Examples Using mail() to send a simple email: <?php // The message $message = "Line 1\nLine 2\nLine 3"; Example #1 Sending mail. // In case any of our lines are larger than 70 characters // we should use wordwrap() $message = wordwrap($message, 70); // Send mail('caffeinated@example.com', 'My Subject', $message); ?> 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. • •
Sending With Headers Example #2 Sending mail with extra headers. The addition of basic headers, telling the MUA the From and Reply-To addresses: <?php $to = 'nobody@example.com'; $subject = 'the subject'; $message = 'hello'; $headers = 'From: webmaster@example.com' . "\r\n" . 'Reply-To: webmaster@example.com' . "\r\n" ; mail($to, $subject, $message, $headers); ?>
Collecting Data and Sending Mail we are going to create two Web pages, mailform.html and email.php The file mailform.html will collect the data you are going to send. The file email.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 email.php. This second page will take the values entered into the first page, and send them in an e-mail. • •
Mail Form.html <html> <head> <h2> Enter EMAIL DATA </h2> </head> <body> <form method="post" action="email.php"> enter to address<input type="text" name="to"> <br></br> enter ur subject<input type="text" name="subject"> <br></br> Enter ur Message <textarea name="MESSAGE" cols="100" rows="20"></textarea> <br></br> <input type="submit" value="submit"> <input type="reset" value= "Reset" > </form> </body> </html> • • • • • • • • • • • • • • • • • •
email.php <?php ini_set("sendmail_from", "pradeep@vit.ac.in"); ini_set("SMTP","mail.vit.ac.in"); echo "<h1>Welcome to emailing with PHP</h1>"; echo "<br>"; $to= $_REQUEST["to"]; $subject= $_REQUEST["subject"]; $message= $_REQUEST["MESSAGE"]; $from = "pradeep@vit.ac.in"; $headers = "From:" . $from; $send=mail($to,$subject,$message,$headers); if($send) echo "congrats! The following Mail has been Sent<br><br>"; echo "<b>To:</b> $to<br>"; echo "<b>From:</b> $from<br>"; echo "<b>Subject:</b> $subject<br>"; echo "<b>Message:</b><br>"; 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 e-mail addresses so that you can verify their receipt. • Click the Send button. A second page appears, similar to the one shown below. • Open your e-mail client and check your e- mail
How it works Remember that if your email.php page is not in the same folder as mailform.html, you have to provide the correct path: • <form name=”theform” method=”post” action=”yourdir/email.php”> • Once the user presses the Send button, email.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 e-mail 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: <br><br>”; echo “<b>To:</b> $to<br>”; echo “<b>From:</b> $from<br>”; echo “<b>Subject:</b> $subject<br><br>”; echo “<b>Message:</b><br>”; echo $message; } else { echo “There was an error...”; } ?>