PHP/Utility Function/mail
Содержание
- 1 Add both name and address values into the email address
- 2 Encrypting and Sending Secure Emails Using S/MIME
- 3 mail() function mails information to a given recipient.
- 4 mail-javascript.php
- 5 send-email-multiple-recipients-2.php
- 6 send-email-with-headers.php
- 7 Sending a message with mail()
- 8 Sending Internet Mail
- 9 Sending Mail
- 10 Sending user-requested information
- 11 send-mail-multiple-recipients.php
- 12 send-plaintext-email.php
- 13 Sends a HTML mail from a given email address:
- 14 Using mail() to redirect user information
Add both name and address values into the email address
<source lang="html4strict">
<?
$mailtoname = "My Best Friend"; $mailtoaddress = "a_friend@example.ru"; $mailtocomplete = "$mailtoname <$mailtoaddress>"; mail($mailtocomplete, "My Subject", "Hello, world!");
?>
</source>
Encrypting and Sending Secure Emails Using S/MIME
<source lang="html4strict">
<?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());
} ? >
</source>
mail() function mails information to a given recipient.
<source lang="html4strict">
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); ?>
</source>
mail-javascript.php
<source lang="html4strict">
<?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>
</source>
send-email-multiple-recipients-2.php
<source lang="html4strict">
<?php
$headers = "From:s@example.ru\r\n"; $headers .= "Bcc:t@example.ru\n"; mail("intern@example.ru", "Company","Don"t be late!", $headers);
?>
</source>
send-email-with-headers.php
<source lang="html4strict">
<?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);
?>
</source>
Sending a message with mail()
<source lang="html4strict">
<? $mail_body=<<<_TXT_ Your order is: _TXT_; mail("h@example.ru","Your Order",$mail_body); ?>
</source>
Sending Internet Mail
<source lang="html4strict">
<?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"); ?>
</source>
Sending Mail
<source lang="html4strict">
<?
$mailaddress = "a_friend@example.ru"; $mailsubject = "My Subject"; $mailbody = "Hello, world!"; mail($mailaddress, $mailsubject, $mailbody);
?>
</source>
Sending user-requested information
<source lang="html4strict">
//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:
<input type=\"text\" name=\"email\" value=\"\">
<input type=\"checkbox\" name=\"information[team]\" value=\"y\">Development
Team
<input type=\"checkbox\" name=\"information[events]\" value=\"y\">Upcoming
Events
<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>
</source>
send-mail-multiple-recipients.php
<source lang="html4strict">
<?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);
?>
</source>
send-plaintext-email.php
<source lang="html4strict">
<?php
mail("test@example.ru", "This is a subject", "This is the mail body");
?>
</source>
Sends a HTML mail from a given email address:
<source lang="html4strict">
<?
$message = "This is a test"; $headers = "From: foo@bar.ru\r\nContent-type: text/html\r\n"; mail("you@yourdomain.ru", "Testing", $message, $headers);
?>
</source>
Using mail() to redirect user information
<source lang="html4strict">
//File: index.php
<html>
<head>
<title></title>
</head>
<body>
<?
$form = "
<form action=\"index.php\" method=\"post\">
<input type=\"hidden\" name=\"seenform\" value=\"y\">
Send us your comments!
Your Name:
<input type=\"text\" name=\"name\" value=\"\">
Your Email:
<input type=\"text\" name=\"email\" value=\"\">
Your Comments:
<textarea name=\"comments\"></textarea>
<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>
</source>