TEDIA会員に登録したメールアドレスとパスワードを入力してください

メールアドレス:

     パスワード:


パスワードを忘れた方はパスワードの確認を行ってください。

TEDIA会員へのご登録がお済みで無い方はこちらで登録ができます


>> テクノロジーポータル TEDIA トップページへ戻る <<

Think IT Software Developer's Think IT Find-IT 失敗しないソフト選び Find-IT TEDIA テクノロジーポータル TEDIA インストールマニアックス2008 インストールマニアックス2008

TEDIA SponsorsOpen Source Web Development Tutorials - Dev Shed

PHPでメール送信機能を実装しよう
(2009/02/26公開)

アプリケーションコード

 では「メールのデータを収集するフォームを生成し、次に、データを処理してmail関数に手渡し、mail関数がメールメッセージを送信する」というアプリケーションを作成してみましょう。非常に単純なアプリケーション例として、以下のコードを書いてみました。


<?
//is form submitted
if(isset($_POST['key'])){
//collect the information
$from=$email;
$cc=$_POST['cc'];
$bcc=$_POST['bcc'];
if(empty($_POST['tos'])){
$error=true;
}else{
$to=$_POST['tos'];
}
if(empty($_POST['sub'])){
$error=true;
}else{
$subject=$_POST['sub'];
}
if(empty($_POST['msg'])){
$error=true;
}else{
$msg=$_POST['msg'];
}
//check if the an attachment is present
if(isset($_FILES['userfile']['name'])){
$attachment = $_FILES['userfile']['tmp_name'], $_FILES['userfile']['name'];
$headers ="Content-disposition: attachment;
$filename=.$attachment."n";
$headers=.Content-Transfer-Encoding: base64n";
}
if(!$error){
$res=mail($to,$subject,$msg,$headers); 
}
if(!$res){
echo "Mail error occurred";
}
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<span class="style2">Sending Mail With PHP</span>
<form name="form1" method="post" action="newmsg.php">
<table width="100%" border="0" class="block">
<tr>
<td width="10%" valign="top"><strong>To</strong></td>
<td width="90%"><input name="tos" type="text" id="to" size="70" value="esme@holidays.com"/></td>
</tr>
<tr>
<td valign="top"><strong>Cc</strong></td>
<td><input name="cc" type="text" id="cc" size="70" /></td>
</tr>
<tr>
<td valign="top"><strong>Bcc</strong></td>
<td><input name="bcc" type="text" id="bcc" size="70" /></td>
</tr>
<tr>
<td valign="top"><strong>Subject</strong></td>
<td><input name="sub" type="text" id="sub" size="70" />
<input type="hidden" name="hkey" /></td>
</tr>
<tr>
<td valign="top"><strong>Attachment</strong></td>
<td>
<input name="userfile" type="file" id="userfile" size="70" /></td>
</tr>
<tr>
<td valign="top"><strong>Message:</strong></td>
<td><label>
<textarea name="msg" cols="50" rows="10"></textarea>
</label></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" name="submit" value="Send Message" />
</td>
</tr>
</table>
</form>
</body>
</html>



 上記のコードでは、以下のフォームを生成します。


前のページ     1    2    3    4    5    次のページ

Copyright © 2008 Ziff Davis Enterprise, Inc.
Originally appearing in the U.S. Edition of Dev Shed. All Rights Reserved.