【问题标题】:Send Email To Form User With Document Links使用文档链接向表单用户发送电子邮件
【发布时间】:2012-09-04 21:55:07
【问题描述】:

我有一个非常通用的表格。我希望用户输入他们的电子邮件地址,选择他们希望收到的任何文档,并希望生成一封电子邮件,其中包含他们选择的文档的下载链接。我过去也这样做过,但我的大脑一片空白。感谢您提供的任何帮助!谢谢。

这是表格:

<!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>My Form</title>
</head>

<body>
<form method="post" action="autoreply.php">
  <label class="description" for="email">Your Email Address</label>
  <input id="email" name="email" class="element text medium" type="text" maxlength="255" value=""/>
  <br />
  Document 1:
  <input type="checkbox" name="document[]" value="document1" />
  <br />
  Document 2:
  <input type="checkbox" name="document[]" value="document2" />
  <br />
  Document 3:
  <input type="checkbox" name="document[]" value="document3" />
  <br />
  <br />
  <input type="hidden" name="send" value="1" />
  <input type="submit" value="Submit" />
</form>
</body>
</html>

这是我的 PHP:

<?php 
if(isset($_POST) && ($_POST['send'] == 1)){ 

    $documents = array( 
                    'document1' => 'http://www.example.com/document1.doc', 
                    'document2' => 'http://www.example.com/document2.doc', 
                    'document3' => 'http://www.example.com/document3.doc' 
                ); 

    $to      = '$email'; 
    $subject = 'the subject'; 
    $message = "hello\n\n"; 

    if(isset($_POST['document']) && count($_POST['document']) > 0){ 
        foreach($_POST['document'] as $doc){ 
            if(isset($documents[$doc])){ 
                $message .= "Here is ".$documents[$doc]."\n"; 
            } 
        } 
    } 


    $headers = 'From: webmaster@example.com' . "\r\n" . 
        'Reply-To: webmaster@example.com' . "\r\n" . 
        'X-Mailer: PHP/' . phpversion(); 

    mail($to, $subject, $message, $headers); 
} 
?> 

【问题讨论】:

    标签: php forms


    【解决方案1】:

    您应该使用附件来包含在文档中,或者如果文档是纯文本,请将它们包含在您的消息中。

    当您想要包含附件时,最方便的是使用现成的邮件程序类,例如swift mailer

    如果你想在你的消息中包含文档,你应该使用这样的东西:

    if(isset($_POST['document']) && count($_POST['document']) > 0){ 
            foreach($_POST['document'] as $doc){ 
                if(isset($documents[$doc])){ 
                    $message .= "Here is ".$documents[$doc]."\n\n";
                    $message .= file_get_contents( filename_of($documents[$doc]) ); 
                } 
            } 
    }  
    

    当然,你必须编写一个从文档名中获取文件名的函数。

    【讨论】:

      猜你喜欢
      • 2011-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-15
      • 2015-06-15
      相关资源
      最近更新 更多