duanqiu2064 2013-01-19 03:09
浏览 50
已采纳

我可以添加到这个PHP电子邮件表单,以使其发送附件

I would like to make it so that I can attach the image specified in filefield "image" to the email that my form sends. What can I add here to do that? I appreciate the help. It should send me a message with the specified info and the image attachment if all goes well.

<?php 
if ($_POST["email"]<>'') { 
$ToEmail = 'skidberna@yahoo.com'; 
$EmailSubject = 'Contact form from Legendmaker Customer: ' .$_POST["email"]; 
$mailheader = "From Legendmaker Customer: ".$_POST["name"]."
"; 
$mailheader .= "Reply-To: ".$_POST["name"]."
"; 
$mailheader .= "Content-type: text/html; charset=iso-8859-1
"; 
$MESSAGE_BODY = "Name: ".$_POST["name"]."     "; 
$MESSAGE_BODY .= "Email: ".$_POST["email"]."     "; 
$MESSAGE_BODY .= "Recipient's Gender: ".nl2br($_POST["gender"])."     ";
$MESSAGE_BODY .= "Recipient's Name: ".$_POST["recipientname"]."     ";
$MESSAGE_BODY .= "Recipient's Hobbies/Interests: ".nl2br($_POST["hobbies"])."   ";
$MESSAGE_BODY .= "Recipient's Age: ".$_POST["age"]."     ";
$MESSAGE_BODY .= "Other Information: ".nl2br($_POST["otherinfo"])."     "; 
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure"); 
?>
<title>The Legend Maker - submit a story (test server)</title>
<link href="/CSS/CSS.css" rel="stylesheet" type="text/css"> 
<div align="center">
<p><span class="linkText"><a href="/index.html">Home</a> 
<a href="/contact-us.php">Contact Us</a> 
<a href="/payments.html">Payments</a>    </span> </p>
<p>&nbsp;</p>
<h2 class="headingText">&nbsp;</h2>
<h2 class="headingText">&nbsp;</h2>
<h2 class="headingText">&nbsp;</h2>
<h2 class="headingText">(for testing purposes)</h2>
<h2 class="headingText">Submit a story test server</h2>
</div>
<p>Your story request was submitted
<?php 
 } 
 else 
 { 


?> 
</p>
<title>The Legend Maker - submit a story (test server)</title>
<link href="/CSS/CSS.css" rel="stylesheet" type="text/css">

<div align="center">
<p><span class="linkText">
<a href="/index.html">Home</a> 
<a href="/contact-us.php">Contact Us</a> 
<a href="/payments.html">Payments</a></span> </p>
<p>&nbsp;</p>
<h2 class="headingText">&nbsp;</h2>
<h2 class="headingText">&nbsp;</h2>
<h2 class="headingText">&nbsp;</h2>
<h2 class="headingText">(for testing purposes)</h2>
<h2 class="headingText">Submit a story test server</h2>
</div>
<form action="submit-story-with-attach.php" method="post" enctype="multipart/form-data">
<table width="476" height="468" border="0" cellpadding="0" cellspacing="2">
<tr>
<td width="23%" height="25" class="bodytext"><p align="center">Your Name:</p></td>
<td width="77%"><input name="name" type="text" id="name" size="32"></td>
</tr>
<tr>
<td height="25" class="bodytext"><p align="center">Email Address:</p></td>
<td><input name="email" type="text" id="email" value="you@example.com" size="32"></td>
</tr>
<tr>
<td height="44" class="bodytext"><p align="center">Recipient's Gender:</p></td>
<td><label>
<select name="gender" id="gender">
<option value="male" selected="selected">Male</option>
<option value="female">Female</option>
<option value="other">Other</option>
                          </select>
</label></td>
</tr>
<tr>
<td height="44" class="bodytext"><p align="center">Recipient's Name:</p>  </td>
<td align="left" valign="top"><input name="recipientname" type="text"  id="recipientname" value="ex: Jonny" size="32" /></td>
</tr>
<tr>
<td height="44" class="bodytext"><p align="center">Recipient's Interests or Hobbies: </p></td>
<td align="left" valign="top"><textarea name="hobbies" cols="50" rows="6"   id="hobbies">ex: horseback riding, biking...</textarea></td>
</tr>
<tr>
<td class="bodytext"><p align="center">Recipient's Age:</p></td>
<td align="left" valign="top"><input name="age" type="text" id="age" size="5" maxlength="3" /></td>
</tr>
<tr>
<td class="bodytext"><p align="center">Other Important Information:</p></td>
<td align="left" valign="top"><textarea name="otherinfo" cols="50" rows="6"  id="otherinfo">ex: other things the recipient may enjoy or their favorite superhero
Please specify information you are giving us also: don't do this: superman
submit it like this: favorite superhero: superman</textarea></td>
</tr>
<tr>
<td class="bodytext"><p align="center">Images You Would Like To Include In Story:</p>     </td>
<td align="left" valign="top"><label>
<br />
<input type="file" name="image" id="image" />
</label></td>
</tr>
<tr>
<td class="bodytext"><p align="center">&nbsp;</p></td>
<td align="left" valign="top"><input type="submit" name="Submit" value="Send" /></td>
</tr>
</table>
</form> 
<?php 
}; 
?>
  • 写回答

1条回答 默认 最新

  • dtrotfd1012 2013-01-19 03:11
    关注

    Is there any reason why you can't use PHPMailer? I'd highly recommend it for attachments alone.

    You can then do something like this:

    $mail             = new PHPMailer();
    
    $body             = file_get_contents('contents.html');
    $body             = preg_replace('/[\]/i','',$body);
    
    $mail->SetFrom('name@yourdomain.com', 'First Last');
    
    $mail->AddReplyTo("name@yourdomain.com","First Last");
    
    $address = "whoto@otherdomain.com";
    $mail->AddAddress($address, "John Doe");
    
    $mail->Subject    = "PHPMailer Test Subject via mail(), basic";
    
    $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
    
    $mail->MsgHTML($body);
    
    $mail->AddAttachment("images/phpmailer.gif");      // attachment
    $mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
    
    if(!$mail->Send()) {
      echo "Mailer Error: " . $mail->ErrorInfo;
    } else {
      echo "Message sent!";
    }
    

    Source

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 poi合并多个word成一个新word,原word中横版没了.
  • ¥15 【火车头采集器】搜狐娱乐这种列表页网址,怎么采集?
  • ¥15 求MCSCANX 帮助
  • ¥15 机器学习训练相关模型
  • ¥15 Todesk 远程写代码 anaconda jupyter python3
  • ¥15 我的R语言提示去除连锁不平衡时clump_data报错,图片以下所示,卡了好几天了,苦恼不知道如何解决,有人帮我看看怎么解决吗?
  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?