【问题标题】:How to send email with dropdown as selected using PHPmailer?如何使用 PHPmailer 发送带有下拉列表的电子邮件?
【发布时间】:2020-09-30 07:10:11
【问题描述】:

我试图弄清楚如何检索选择的下拉列表并使用 PHPmailer 将其发送到邮件,但我无法实现它,它只需要一些调整!另请注意,我将 Bulma 用作我项目的一部分。

 <form action="bug.php" method="POST" class="" enctype="multipart/form-data">
      <div class="section">
        <div class="container">
          <div class="columns">
            <div class="column"></div>
            <div class="column is-5">
              <div class="field">
                <label class="label">Name</label>
                <div class="control">
                  <input
                    class="input"
                    type="text"
                    id="name"
                    name="vname"
                    placeholder="e.g Alex Smith"
                  />
                </div>
              </div>
    
              <div class="field">
                <label class="label">Email</label>
                <div class="control">
                  <input
                    class="input"
                    type="email"
                    name="vemail"
                    placeholder="e.g. alexsmith@gmail.com"
                  />
                </div>
              </div>
    
              <div class="control">
                <div class="select">
                  <select id="selection" onchange="showradio(this)">
                    <option value="0">Select dropdown</option>
                    <option value="1">Best</option>
                    <option value="2">Bestest</option>
                    <option value="3">Exceptional</option>
                  </select>
                </div>
              </div>
    
              
    
    <div class="buttons" >
      <button name="btnsubmit" id="btnsend" class="button is-primary is-light">Submit</button>
    </div>
    
    
    
            </div>
            <div class="column"></div>
          </div>
        </div>
      </div>
      </form>

现在是我配置的 PHPMailer。

<?php


$name = $_POST['vname'];
$email = $_POST['vemail'];
$msg = $_POST['vmsg'];





// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

// Load Composer's autoloader
require 'PHPmailer/vendor/autoload.php';

// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);

try {
    //Server settings
    $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      // Enable verbose debug output
    $mail->isSMTP();                                            // Send using SMTP
    $mail->Host       = 'smtp.gmail.com';                    // Set the SMTP server to send through
    $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
    $mail->Username   = 'from24@gmail.com';                     // SMTP username
    $mail->Password   = 'pass';                               // SMTP password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
    $mail->Port       = 587;                                    // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above

    //Recipients
    $mail->setFrom('messagefrom24@gmail.com', 'Tools');
    $mail->addAddress('messageto@gmail.com', 'To');     // Add a recipient
    $mail->AddAddress("$_POST['emailreceiver']");


    // // Attachments
    // $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
    // $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

    // Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'New Bug Submission !';
    $mail->Body   = $msg;

    // $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}

?> 

代码仅发送消息、主题和名称,但我需要添加在下拉列表中选择的文本。

【问题讨论】:

  • 在您的选择标签中添加 name="dropdown" 然后通过 $_POST['dropdown'] 获取,而不是 value="0,1,2 将其更改为要发送的字符串

标签: javascript php html phpmailer


【解决方案1】:

正如建议的那样,您缺少选择元素的“名称”属性,因此您需要输入名称 =“my_dropdown”。此外,您需要将选项的值从 0、1、2 更改为实际文本。 如果您不想更改选项值,则可以创建一个简单的辅助函数,如下所示:

function get_dropwown_text() {
 return ["Select dropdown", "Best", "Bestest", "Exceptional"];
}

在您的视图(HTML)中,您可以使用帮助函数准备下拉列表:

<select id="selection" onchange="showradio(this)">
<?php
foreach(get_dropwown_text() as $k => $v) {
 echo sprintf('<option value="%d">%s</option>', $k, $v);
}             
?>
</select>                 

现在,在您的 php 脚本中,您可以通过以下方式获取下拉文本:

$dropdown_value = $_POST['my_dropdown']; //will give 1 or 2 or 3
$dropdown_text = get_dropwown_text()[$dropdown_value]; //will give you actual text

//Now you can use the $dropdown_text to send in mail body via PHPmailer
$msg = $dropdown_text;

【讨论】:

    【解决方案2】:

    您的代码存在重大漏洞。

    这些行的作用是形成一个垃圾邮件网关

    $msg = $_POST['vmsg'];
    $mail->AddAddress("$_POST['emailreceiver']");
    $mail->Body   = $msg;
    

    任何人都可以编写一个脚本,使用您的服务器他们选择的任何内容发送给他们喜欢的任何人这样做。这是一件非常危险的事情,而且此类漏洞经常被发现并被大规模利用。

    要解决此问题,您需要更改电子邮件地址的使用或消息内容。前者比较简单,做到这一点的方法是使用提交的地址发送到,而是使用它的代理。

    在您的表单中,您可能有这样的选项:

    <select name="emailreceiver">
        <option value="sales" selected>Sales</option>
        <option value="support">Technical support</option>
        <option value="accounts">Accounts</option>
    </select>
    

    然后在您的脚本中,将这些值映射到固定地址:

    $addresses = [
        'sales' => 'sales@example.com',
        'support' => 'support@example.com',
        'accounts' => 'accounts@example.com',
    ];
    //Validate address selection before trying to use it
    if (array_key_exists('emailreceiver', $_POST) && array_key_exists($_POST['emailreceiver'], $addresses)) {
        $mail->addAddress($addresses[$_POST['emailreceiver']]);
    } else {
        //Fall back to fixed address if selection invalid
        $mail->addAddress('support@example.com');
    }
    //Validate any email address that was submitted, and use it as a reply-to
    if (!$mail->addReplyTo($_POST['vemail'], $_POST['vname'])) {
        die('Invalid email address provided');
    }
    

    这样就没有外部控制消息发送给谁。查看the contact form example provided with PHPMailer,了解如何安全地构建联系表单。

    完全分开,这是可疑的,表明您可能不了解作曲家的工作原理:

    require 'PHPmailer/vendor/autoload.php';
    

    composer.json 文件应该位于整个项目的顶层,而不是特定于 PHPMailer 的文件。搜索有关如何使用 composer 的教程。

    【讨论】:

      猜你喜欢
      • 2012-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-10
      • 2013-07-29
      • 2016-10-10
      • 1970-01-01
      相关资源
      最近更新 更多