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:
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
" // 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!
"; } ?>
Contact Form to send mail
// Additional headers $headers.= 'Cc: [email protected]'; $headers.= 'Bcc: [email protected]';
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
Done!
Send mail with attached file using PHP Mailer
PHP mail attachment script to send email is below
PHPMailer - Mail() advanced test Hello World
RED World
This is a test picture:
'; require_once 'class.phpmailer.php'; $mail = new PHPMailer(true); $mail->AddReplyTo('[email protected]', 'Unknown'); $mail->AddAddress('[email protected]', 'XYZ'); $mail->SetFrom('[email protected]', 'Unknown'); $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\n"; ?>