PHP/Utility Function/mail

Материал из Web эксперт
Версия от 07:08, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Add both name and address values into the email address

 
<?
    $mailtoname = "My Best Friend";
    $mailtoaddress = "a_friend@example.ru";
    $mailtocomplete = "$mailtoname <$mailtoaddress>";
    mail($mailtocomplete, "My Subject", "Hello, world!");
?>



Encrypting and Sending Secure Emails Using S/MIME

 
<?php
$message_headers = array("To: p@example.ru",
                         "From: b@example.ru",
                         "Subject: Summary");
if (openssl_pkcs7_encrypt("espionage_summary.txt", "espionage_summary.pkcs7",
 file_get_contents("phboss.crt"), $message_headers)) {
  mail("phboss@thingy.exmaple.ru",
       "Espionage Summary",
       file_get_contents("espionage_summary.pkcs7"),
       $message_headers);
} else {
  die(openssl_error_string());
}
?>
<?php
$cert = file_get_contents("bob.crt");
$pkey = file_get_contents("bob.pem");
if (!openssl_pkcs7_decrypt("new_instructions.pkcs7",
                           "new_instructions.txt",
                           $cert, $pkey)) {
  die(openssl_error_string());
}
? >



mail() function mails information to a given recipient.

 
Its syntax is: boolean mail(string recipient, string subject, string message [, string addl_headers])
<?
$email = "y@yourserver.ru";
$subject = "This is the subject";
$message = "This is the message";
$headers = "From: somebody@somesite.ru ";
mail($email, $subject, $message, $headers);
?>



mail-javascript.php

 
<?php
   if (isset($_POST["submit"]))
   {
      $headers = "FROM:editor@example.ru\n";
      $body = $_POST["name"].",id=".$_POST["id"];
      mail($_POST["recipient"],"News",$body,$headers);
      echo "The article has been mailed to ".$_POST["recipient"];
   }
?>
<form action="mail.html" method="post">
   <input type="hidden" name="id" value="<?php echo $_GET["id"];?>" />
   Email:<input type="text" name="recipient" size="20" maxlength="40" value="" />
   Your name:
   <input type="text" name="name" value="" />
   <input type="submit" name="submit" value="Send Article" />
</form>



send-email-multiple-recipients-2.php

 
<?php
   $headers = "From:s@example.ru\r\n";
   $headers .= "Bcc:t@example.ru\n";
   mail("intern@example.ru", "Company","Don"t be late!", $headers);
?>



send-email-with-headers.php

 
<?php
    $headers = "From:sender@example.ru\r\n";
    $headers .= "Reply-To:sender@example.ru\r\n";
    $headers .= "Content-Type: text/plain;\r\n charset=iso-8859-1\r\n";
    mail("test@example.ru", "This is the subject", 
         "This is the mail body", $headers);
?>



Sending a message with mail()

 
<?
$mail_body=<<<_TXT_
Your order is:
_TXT_;
mail("h@example.ru","Your Order",$mail_body);
?>



Sending Internet Mail

 
<?php
$to = "s@somedomain.ru";
$from = "j@anotherdomain.ru";
$cc = $from;
$subject = "Sending emails from PHP";
$body = <<< BODY
Hi Sam,
This email is generated from a PHP script.
- Joe
BODY;
mail($to, $subject, $body, "From: $from\r\nCc: $cc");
?>



Sending Mail

 
<?
    $mailaddress = "a_friend@example.ru";
    $mailsubject = "My Subject";
    $mailbody = "Hello, world!";
    mail($mailaddress, $mailsubject, $mailbody);
?>



Sending user-requested information

 
//File: index.php
<html>
<head>
<title></title>
</head>
<body>
<?
$form = "<form action=\"index.php\" method=\"post\">
<input type=\"hidden\" name=\"seenform\" value=\"y\">
Your Email:<br>
<input type=\"text\" name=\"email\" value=\"\"><br>
<input type=\"checkbox\" name=\"information[team]\" value=\"y\">Development
Team<br>
<input type=\"checkbox\" name=\"information[events]\" value=\"y\">Upcoming
Events<br>
<input type=\"submit\" value=\"send\">
</form>";
if ($seenform != "y") :
     print "$form";
else :
     $headers = "From: d@yoursite.ru";
     while ( list($key, $val) = each ($information) ) :
          if ($val == "y") :
               $filename = "$key.txt";
               $subject = "Requested $key information";
               $fd = fopen ($filename, "r");
               $contents = fread ($fd, filesize ($filename));
               mail($email, $subject, $contents, $headers) or die("Can"t send email!");
               fclose($fd);
          endif;
     endwhile;
     print sizeof($information)." newsletters have been sent to $email!";
endif;
?>
</body>
</html>



send-mail-multiple-recipients.php

 
<?php
   $headers = "From:sender@example.ru\r\n";
   $recipients = "test@example.ru,info@example.ru";
   mail($recipients, "This is the subject","This is the mail body", $headers);
?>



send-plaintext-email.php

 
<?php
   mail("test@example.ru", "This is a subject", "This is the mail body");
?>



Sends a HTML mail from a given email address:

 
<?
    $message = "<b>This is a <i>test</i></b>";
    $headers = "From: foo@bar.ru\r\nContent-type: text/html\r\n";
    mail("you@yourdomain.ru", "Testing", $message, $headers);
?>



Using mail() to redirect user information

 
//File: index.php
<html>
<head>
<title></title>
</head>
<body>
<?
$form = "
<form action=\"index.php\" method=\"post\">
<input type=\"hidden\" name=\"seenform\" value=\"y\">
<b>Send us your comments!</b><br>
Your Name:<br>
<input type=\"text\" name=\"name\" value=\"\"><br>
Your Email:<br>
<input type=\"text\" name=\"email\" value=\"\"><br>
Your Comments:<br>
<textarea name=\"comments\"></textarea><br>
<input type=\"submit\" value=\"submit!\">
</form>
";
if ($seenform != "y") :
     print "$form";
else :
     $recipient = "y@youremail.ru";
     $subject = "User Comments ($name)";
     $headers = "From: $email";
     mail($recipient, $subject, $comments, $headers) or die("Could not send email!");
     print "Thank you $name for taking a moment to send us your comments!";
endif;
?>
</body>
</html>