Send Email using PHP mail() Function is one of the important feature of PHP.
mail() is used to send email from one email address to another.
Send email by PHP is very easy and without cost. PHP gives mail() function to send email.
mail() function Syntax:
1 2 3 4 5 6 7 8 | <php $subject = "Test Mail"; $message = "Hello"; $from = "pankaj @gmail.com"; mail($to,$subject,$message,$from); ?> Done! |
Output:
Done!
Benefits of email in web application
1. Customer enquiry details send admin email-id directly.
2. Send customer notification message to their email.
3. Send OTP to user email for forgot password request and send recovery link to set new password.
4. Email verification link to user email account.
And so many others benefits for mail() function.
PHP Mail function with HTML header
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?php if($_POST['mailsend']){ $subject = $_POST['subject']; Name : $_POST[username]; Phone : $_POST[userphone]; Message : $_POST[message]; Email: $_POST[useremail]; $message = “<HTML-->" // To send HTML mail, the Content-type header must be set $headers = 'MIME-Version: 1.0' . "\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\n"; $headers .= "From: $_POST[useremail]" . "\n" ; $headers .="Reply To:$_POST[useremail]"; mail($to, $subject, $message, $headers); echo "Done!<br>"; } ?> |
Contact Form to send mail
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | <form action="" method="POST"> <table> <tbody> <tr> <td>Name :</td> <td><input name="username" type="text"></td> </tr> <tr> <td>Phone:</td> <td><input name="userphone" type="number"></td> </tr> <tr> <td>Email:</td> <td><input name="useremail" type="email"></td> </tr> <tr> <td>Subject:</td> <td><input name="subject" type="text"></td> </tr> <tr> <td>Message</td> <td><textarea name="message" rows="5"></textarea></td> </tr> <tr> <td colspan="2" align="right"><input name="mailsend" type="submit" value="Send"> <input type="reset" value="Clear"></td> </tr> </tbody> </table> </form> |
1 2 3 |
About MIME
MIME (Multi-Purpose Internet Mail Extensions) is an extension of the original Internet e-mail protocol that lets people use the protocol to exchange different kinds of data files on the Internet: audio, video, images, application programs, and other kinds, as well as the ASCII text handled in the original protocol, the Simple Mail Transport Protocol
Output
1 | Done! |
Send mail with attached file using PHP Mailer
PHP mail attachment script to send email is below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | <html> <head> <title>PHPMailer - Mail() advanced test</title> </head> <body> <?php $html = '<HTML> <BODY> <H2>Hello World</H2> <FONT COLOR="RED"><H2>RED World</H2></FONT> <p>This is a test picture: <img src="cid:mypic" alt="mypic" /></p> </BODY> </HTML>'; require_once 'class.phpmailer.php'; $mail = new PHPMailer(true); $mail->Subject = 'Sending Attachments'; /* For plain text: $mail->Body = 'PHPMailer Test Subject via mail(), advanced'; */ $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; $mail->MsgHTML($html); $mail->AddAttachment('uploads/twain.dll'); $mail->AddAttachment('uploads/twain_32.dll'); $mail->AddAttachment('uploads/setuperr.log'); $mail->AddAttachment('uploads/msdfmap.zip'); $mail->AddEmbeddedImage('uploads/pic.jpg', 'mypic', 'pic.jpg'); $mail->Send(); echo "Message Sent OK</p>\n"; ?> </body> </html> |
Output:
Explain:
class.phpmailer.php is a open source php class file which use to send attached file via mail function.
User can download this file from Google it’s free. All functions are defined in the class.phpmailer.php file.
note:
Send Mail via PHP then program file must be uploading in the online server. User can use free host by registering in webhost site.
Mail function only perform online not localhost.
If user want test mail function in localhost the few configurations are want to set in php.ini file.
Find mail function in php.ini file. There is some code like this,
1 2 3 4 5 6 7 8 9 | [mail function] ; XAMPP: Comment out this if you want to work with an SMTP Server like Mercury ; SMTP = localhost ; smtp_port = 25 ; For Win32 only. ; http://php.net/sendmail-from ;sendmail_from = postmaster@localhost ;sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t" |
Replace the code with user details.
1 2 3 4 5 6 7 8 9 | [mail function] ; XAMPP: Comment out this if you want to work with an SMTP Server like Mercury SMTP = smtp.gmail.com smtp_port = 25 ; For Win32 only. ; http://php.net/sendmail-from sendmail_from = phpflow@gmail.com sendmail_path = "\"D:\xampp\sendmail\sendmail.exe\" -t" |
Then change PHP SMTP mail configuration like this in the sendmail.ini file.
1 2 3 4 5 6 | [mail function] smtp_server=smtp.gmail.com smtp_port=25 auth_username=phpflow@gmail.com auth_password=XXXXXXX force_sender=phpflow@gmail.com |
Then restart XAMPP control panel then use mail function.
Follow above basic steps to send email in php
Read More
How to fetch image from database in PHP and display in table