Home  >  Article  >  Backend Development  >  The definition and usage of mail function in php

The definition and usage of mail function in php

墨辰丷
墨辰丷Original
2018-06-12 11:55:311946browse

This article mainly introduces the method of sending emails with CC and BCC in PHP. It involves the use skills of mail function in PHP. It is of great practical value. Friends who need it can refer to the example of this article.

Describes how to send emails with CC and BCC in PHP. The specific analysis is as follows:

The mail function of php is used in the program, which is defined as follows:
bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $ additional_parameters ]] )
Return True if the email is sent successfully, otherwise return False

<html>
<head>
<title>Send email with CC and BCC</title>
</head>
<body>
<form action="sendemail.php" method=post name=form1>
<table>
  <tbody>
  <tr>
   <td>
    <p align=right><b>To</b></p></td>
   <td>
    <p>Name <input name=mailtoname size=35><br />E-mail
        <input name=mailtomail size=35></p></td></tr>
  <tr>
   <td>
    <p align=right><b>CC</b></p></td>
   <td><input name=mailcc size=35> </td></tr>
  <tr>
   <td>
    <p align=right><b>BCC</b></p></td>
   <td><input name=mailbcc size=35> </td></tr>
  <tr>
   <td>
    <p align=right><b>Priority</b></p></td>
   <td><select name=mailpriority>
      <option value=1>Highest</option>
      <option value=2>High</option>
      <option selected value=3>Normal</option>
      <option value=4>Low</option>
      <option value=5>Lowest</option>
     </select>
   </td></tr>
  <tr>
   <td><p align=right><b>Subject</b></p></td>
   <td><input name=mailsubject size=35></td></tr>
  <tr>
   <td>
    <p align=right><b>Message</b> </p></td>
   <td><textarea cols=50 name=mailbody rows=7></textarea></td></tr>
  <tr>
   <td colSpan=2>
    <p align=center>
 <input name=Submit type=submit value=Submit></p>
  </td>
  </tr>
  </tbody>
</table>
</form>
</body>
</html>

Backend php code, saved as sendmail.php

<html>
 <head>
 <title>Send Mail Script</title>
 </head>
 <body>
 <?php
  $message= " " ;
  if (empty ( $mailtoname) || empty ( $mailtomail) ) {
    die ( "Recipient is blank! ") ;
  }else{
    $to = $mailtoname . " <" . $mailtomail . ">" ;
  }
  if ( empty ( $mailsubject) ) {
   $mailsubject=" ";
  }
  if (($mailpriority>0) && ($mailpriority<6)) {
    $mailheader = "X-Priority: ". $mailpriority ."\n";
  }
  $mailheader.= "From: " . "Sales Team <sales@yourdomain.com>\n";
  $mailheader.= "X-Sender: " . "support@yourdomain.com\n";
  $mailheader.= "Return-Path: " . "support@yourdomain.com\n";
  if (!empty($mailcc)) {
   $mailheader.= "Cc: " . $mailcc ."\n";
  }
  if (!empty($mailbcc)) {
   $mailheader.= "Bcc: " . $mailbcc ."\n";
  }
  if (empty($mailbody)) {
   $mailbody=" ";
  }
  $result = mail ($to, $mailsubject, $mailbody, $mailheader);
  echo "<center><b>Mail sent to ". "$to". "<br />";
  echo $mailsubject. "<br />";
  echo $mailbody. "<br />";
  echo $mailheader. "<br />";
  if ($result) {
    echo "<p><b>Email sent successfully!</b></p>";
  }else{
    echo "<p><b>Email could not be sent. </b></p>";
  }
?>
<p align="center">
<table><tr><td width="66"><p align="right"><b>To</b></p></td>
       <td width="308"><b>
   <?php echo $mailtoname . " [". $mailtomail . " ]";?>
   </b></td></tr>
     <tr><td width="66"><p align="right"><b>CC</b></p></td>
       <td width="308"><b><?php echo $mailcc;?></b></td></tr>
     <tr><td width="66"><p align="right"><b>BCC</b></p></td>
       <td width="308"><b><?php echo $mailbcc; ?></b></td></tr>
     <tr><td width="66"><p align="right"><b>Priority</b></p></td>
       <td width="308"><b><?php echo $mailpriority;?></b></td></tr>
     <tr><td width="66"><p align="right"><b>Subject </b></p></td>
       <td width="308"><b><?php echo $mailsubject;?></b></td></tr>
     <tr><td width="66"><p align="right"><b>Message</b></p></td>
       <td width="308"><b><?php echo $mailbody;?></b></td></tr>
</table>
</p>
</body>
</html>

Summary: Above That’s the entire content of this article, I hope it will be helpful to everyone’s study.

Related recommendations:

php implements cookie-based recording of usernames and passwords

The database in PHP implements more secure permanent login and remembering My function

How to use the generated public key and private key for encryption and decryption in PHP

The above is the detailed content of The definition and usage of mail function in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn