<問題>想要利用php來發送email, 要如何做呢?!
<方法>
找了許多的作法, 發現中文亂碼的問題還蠻嚴重的,
同時幸運地看到正確的二個網頁介紹, 如下:
1.http://css6.pixnet.net/blog/post/7250018-%E5%8F%AF%E4%BB%A5%E7%99%BCmail%E7%9A%84php%E8%AA%9E%E6%B3%95%EF%BC%8C%E4%B8%AD%E6%96%87%E4%BA%82%E7%A2%BC%E4%B9%9F%E8%A7%A3%E6%B1%BA%E4%BA%86%E5%93%A6
2.http://iammic.pixnet.net/blog/post/11305582-php-mail-%E7%99%BC%E9%80%81%EF%BC%8C%E5%A4%BE%E5%B8%B6%E9%99%84%E4%BB%B6%E6%AA%94
也手癢地小改了一下程式(我沒有什麼用意,只希望原創者不要生氣)
(1).參攷上述網頁1.修改...
<方法>
找了許多的作法, 發現中文亂碼的問題還蠻嚴重的,
同時幸運地看到正確的二個網頁介紹, 如下:
1.http://css6.pixnet.net/blog/post/7250018-%E5%8F%AF%E4%BB%A5%E7%99%BCmail%E7%9A%84php%E8%AA%9E%E6%B3%95%EF%BC%8C%E4%B8%AD%E6%96%87%E4%BA%82%E7%A2%BC%E4%B9%9F%E8%A7%A3%E6%B1%BA%E4%BA%86%E5%93%A6
2.http://iammic.pixnet.net/blog/post/11305582-php-mail-%E7%99%BC%E9%80%81%EF%BC%8C%E5%A4%BE%E5%B8%B6%E9%99%84%E4%BB%B6%E6%AA%94
也手癢地小改了一下程式(我沒有什麼用意,只希望原創者不要生氣)
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<?php
header("Content-Type:text/html; charset=utf-8");
$mailToname = "收件者姓名"; //收件者姓名
$mailTo = "xxxxx@xxx.com.tw"; //收件者
$mailSubject = "郵件主旨測試信"; //主旨
$mailContent = "郵件內容測試test123" ; //內容
$mailFromname = "寄件者姓名"; //寄件者姓名
$mailFrom = "xxxxx@xxx.com.tw"; //寄件者電子郵件
$mailCc = "Cc: xxxxx@xxx.com.tw"; //副本電子郵件
$mailTo = "=?UTF-8?B?".base64_encode($mailToname)."?= <" . $mailTo . ">";
$mailSubject = "=?UTF-8?B?".base64_encode($mailSubject)."?="; //主旨編碼成UTF-8
$mailFrom = "=?UTF-8?B?" . base64_encode($mailFromname) . "?= <" . $mailFrom . ">";
$mailHeaders = "Mime-Version: 1.0\nFrom:" . $mailFrom . "\n" . $mailCc . "\nContent-Type: text/html ; charset=utf-8";
if (mail($mailTo, $mailSubject, $mailContent, $mailHeaders))
echo "Mail Sent.";
else
echo "Mail Failed.";
?>
</body>
</html>
(2).參攷上述網頁2.修改...
mail.php
<?php
header("Content-Type:text/html; charset=utf-8");
require_once('class.phpmailer.php');
//檔案上傳
$filename = $_FILES['file']['name'];
$tmpname = $_FILES['file']['tmp_name'];
$filetype = $_FILES['file']['type'];
$filesize = $_FILES['file']['size'];
$mail = new PHPMailer();
mb_internal_encoding('UTF-8');
$mail->SetFrom($_POST['email'], $_POST['name']);
//要寄送的信箱
$mail->AddAddress("xxxxx@xxx.com.tw", "yyyyy@yyy.com.tw");
$mail->Subject = mb_encode_mimeheader("測試", "UTF-8");
$mail->CharSet = "UTF-8";
$body = nl2br($_POST['contents']);
$mail->MsgHTML($body);
$mail->IsHTML(true);
//夾帶附件檔
$mail->AddAttachment($tmpname, $filename);
if($mail->Send())
echo "寄件成功.";
else
echo "寄件失敗.";
?>
(3).參攷上述網頁2.修改...
contact.html
<!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=utf-8" />
<title>contact</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="mail.php" enctype="multipart/form-data">
<table width="333" border="1">
<tr>
<td>姓名</td>
<td><input name="name" type="text" id="name" /></td>
</tr>
<tr>
<td width="92">Email</td>
<td width="225"><input name="email" type="text" id="email" /></td>
</tr>
<tr>
<td valign="top">內容</td>
<td><textarea name="contents" rows="10" id="contents"></textarea></td>
</tr>
<tr>
<td valign="top">附件</td>
<td><input type="file" name="file" /></td>
</tr>
<tr>
<td colspan="2"><div align="center">
<input type="submit" name="Submit" value="送出" />
<input type="reset" value="重設" />
</div></td>
</tr>
</table>
</form>
</body>
</html>
留言