没有表单的PHP邮件列表

时间:2014-07-15 00:44:50

标签: php forms email drupal

无论如何在PHP中没有表单的情况下获取用户的电子邮件?我希望能够向某人发送邮件列表的电子邮件,而不是填写他们输入电子邮件的表单,我在电子邮件中链接的php页面。他们点击链接,然后将其发送到网站上的感谢页面。然后它将他们的IP,日期/时间和电子邮件存储在我设置的数据库中。我已经用表格填写了这个,但我想知道是否有任何可能的方法可以在没有表格的情况下完成。这是我的表单代码,我使用的是Drupal表单API。我已经谷歌搜索了几个小时,但什么都没发现。我看到了一些关于URL参数的内容,但它并没有真正与我的问题有关。任何帮助表示赞赏。

<?php

//This custom module will be used on the website to gain consent from our clients because of the recent CASL anti spam laws that were passed in Cnada.

//Menu hook starts here, implements menu and sets the title, url of the page.
function form_casl_menu() {
  $items = array();
  $items['casl-consent/form'] = array( //this creates a URL that will call this form at "url"
    'title' => 'CASL Subscription', //page title
    'description' => 'A form that allows us to send emails to clients with their consent.',
    'page callback' => 'drupal_get_form', //this is the function that will be called when the page is accessed.  for a form, use drupal_get_form
    'page arguments' => array('form_casl_form'), //put the name of the form here
    'access callback' => TRUE
  );
  return $items;

}

//permission hook
function form_casl_permission() {
  return array(
    'administer my module' => array(
      'title' => t('Administer my module'),
      'description' => t('Perform administration tasks for my module.'),
    ),
  );
}



//form hook, form elements start here

function form_casl_form($form, &$form_state) {
//sometext here
 $form['some_text'] = array(
    '#markup' => '<p><b>Simply enter your email address to subscribe</b>
    </p>'
);


   $form['email'] = array(
    '#type' => 'textfield', //their email
    '#title' => 'Email:',
    '#size' => 30,
    '#maxlength' => 150,

    '#required' => TRUE,
  );







  //submit button
  $form['submit_button'] = array(
    '#type' => 'submit',
    '#value' => t('Submit Data'),

  );
  return $form;
}
    //validate hook
    function form_casl_form_validate($form, &$form_state) { //invalid email error
    if (!valid_email_address($form_state['values']['email'])) {
    form_set_error('mail', t('You must enter a valid e-mail address.'));
    }


}




//submit hook
function form_casl_form_submit($form, $form_state) {
  $sDate = date("Y-m-d H:i:s"); //returns the date and time
  global $name;
  $subbed = 'Yes';

  //----------------------------------------------------------------\\
   $ip55 = &drupal_static(__FUNCTION__);

  //ip get function, returns a clients IP address and checks if they're behind a proxy.
  if (!isset($ip55)) {
    $ip55 = $_SERVER['REMOTE_ADDR'];

    if (variable_get('reverse_proxy', 0)) {
      $reverse_proxy_header = variable_get('reverse_proxy_header', 'HTTP_X_FORWARDED_FOR');
      if (!empty($_SERVER[$reverse_proxy_header])) {
        // If an array of known reverse proxy IPs is provided, then trust
        // the XFF header if request really comes from one of them.
        $reverse_proxy_addresses = variable_get('reverse_proxy_addresses', array());

        // Turn XFF header into an array.
        $forwarded = explode(',', $_SERVER[$reverse_proxy_header]);

        // Trim the forwarded IPs; they may have been delimited by commas and spaces.
        $forwarded = array_map('trim', $forwarded);

        // Tack direct client IP onto end of forwarded array.
        $forwarded[] = $ip55;

        // Eliminate all trusted IPs.
        $untrusted = array_diff($forwarded, $reverse_proxy_addresses);

        // The right-most IP is the most specific we can trust.
        $ip55 = array_pop($untrusted);
      }
    }
  }


  //-----------------------------------------------------------------\\


  //inserting data into database
  db_insert('CASL')
    ->fields(array(


      'email' => $form_state['values']['email'],//email
      'ip' => $ip55,//ip
      'substatus' => $subbed,
      'datetime' => $sDate,//date and time


    ))->execute();
    //sending confirmation email to the user, letting them know they can unsub at any time.

    $values = $form_state['values'];
    $to = $form_state['values']['email'];
    $subject = 'Confirmation';
    $message ="Thank you for your submission. You may unsubscribe at any time by refilling out this form http://www.localhost.ca/casl-consent/form and selecting the 'unsubscribe' option. Or, you can simply email us a request to unsubscribe, and we will remove you from our database immediately. 
    If you have any questions or concerns, you can email us at this link: http://www.localhost.ca/contact";
    mail($to, $subject, $message);
    drupal_set_message("Thank you! Your information has been received successfully and you have been sent a confirmation email.");



    //thank you message after submission




}


?>

1 个答案:

答案 0 :(得分:1)

根据您的评论,听起来您尝试将已知的电子邮件地址与IP地址相关联。您说您正在发送电子邮件,他们正在点击链接。我假设每个电子邮件上的每个链接都是唯一生成的,与特定地址相关联。在这种情况下,您的问题归结为&#34;如何找到客户端的IP地址?&#34;。

在PHP中,您可以使用$_SERVER['REMOTE_ADDR']执行此操作。这是最可靠的,但绝不是万无一失的。

  

&#39; REMOTE_ADDR&#39;       用户正在查看当前页面的IP地址。

如果用户位于代理后面,则可能已设置$_SERVER['HTTP_X_FORWARDED_FOR'],但客户端和代理可能会欺骗此值。它不能保证准确,也不应该被信任。但是,如果用户使用代理,REMOTE_ADDR中的值将是访问您的网络服务器的代理的IP地址,而不是客户端的IP地址。

因此,假设您要使用REMOTE_ADDR,如何将电子邮件地址与IP地址相关联?

用户单击您使用已知电子邮件地址存储的唯一链接。该链接运行一个非常简单的脚本,并查看REMOTE_ADDRHTTP_X_FORWARDED_FOR并存储这两个值。

你的桌子应该像这样简单:

email_address | unique_link | remote_addr | http_x_forwarded_for

用户点击unique_link后,会填写最后两个值。你最初如何填充email_address仍然是你的秘密。