emailLOGIN

email

php email

PHP Send Email

PHP allows you to send emails directly from scripts.

PHP mail() function

PHP mail() function is used to send emails from scripts.

QQ截图20161009091837.png

Note: PHP requires an installed and running mail system in order for the mail functions to be available. The program used is defined through configuration settings in the php.ini file. Read more in our PHP Mail reference manual.

PHP Simple E-Mail

The simplest way to send email via PHP is to send a text email.

In the following example, we first declare the variables ($to, $subject, $message, $from, $headers), and then we use these variables in the mail() function to send an email -mail:

<?php
 $to = "someone@example.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "someonelse@example.com";
$headers = "From: $from";
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
 ?>

PHP Mail Form

With PHP, you can create a feedback form on your site. The following example sends a text message to the specified e-mail address:

<html>
<body>
 <?php
if (isset($_REQUEST['email']))
//if "email" is filled out, send email
  {
  //send email
  $email = $_REQUEST['email'] ;
  $subject = $_REQUEST['subject'] ;
  $message = $_REQUEST['message'] ;
  mail( "someone@example.com", "Subject: $subject",
  $message, "From: $email" );
  echo "Thank you for using our mail form";
  }
else
//if "email" is not filled out, display the form
  {
  echo "<form method='post' action='mailform.php'>
  Email: <input name='email' type='text' /><br />
  Subject: <input name='subject' type='text' /><br />
  Message:<br />
  <textarea name='message' rows='15' cols='40'>
  </textarea><br />
  <input type='submit' />
  </form>";
  }
?>
 </body>
</html>

Explanation of the example:

First, check whether the email input box is filled in

If it is not filled in (for example, when the page is visited for the first time), output the HTML form

If filled in (after the form is filled in), send an email from the form

When the submit button is clicked, reload the page , display the message that the email was sent successfully



Next Section
<html> <body> <?php if (isset($_REQUEST['email'])) //if "email" is filled out, send email { //send email $email = $_REQUEST['email'] ; $subject = $_REQUEST['subject'] ; $message = $_REQUEST['message'] ; mail( "someone@example.com", "Subject: $subject", $message, "From: $email" ); echo "Thank you for using our mail form"; } else //if "email" is not filled out, display the form { echo "<form method='post' action='mailform.php'> Email: <input name='email' type='text' /><br /> Subject: <input name='subject' type='text' /><br /> Message:<br /> <textarea name='message' rows='15' cols='40'> </textarea><br /> <input type='submit' /> </form>"; } ?> </body> </html>
submitReset Code
ChapterCourseware