PHP mailing list without form - php
Is there anyway to get a user's email without a form in php? I want to able to send someone an email for a mailing list and instead of them filling out a form where they enter their email, the php page I have linked in the email. They click the link and it sends them to a thank-you page on the site. Then it stores their IP, date/time and email in a database I have set up. I've completed this with a form, but I'm wondering if there's any possible way to do it without a form. Here's my code for the form, I'm using Drupal form API. I've been googling this for hours, but have found nothing. I saw something about URL parameters, but it didn't really pertain to my question. Any help is appreciated.
<?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
}
?>
Based on your comment, it sounds like you are attempting to associate a known email address to an IP address. You say you are sending an email and they are clicking a link. I'm making the assumption that each of these links on each email is uniquely generated an associated to a specific address. In that case, your problem comes down to "How do I find a client's IP address?".
In PHP, you can do this using $_SERVER['REMOTE_ADDR']. This is the most reliable, but by no means fool proof.
'REMOTE_ADDR'
The IP address from which the user is viewing the current page.
If the user is behind a proxy, the $_SERVER['HTTP_X_FORWARDED_FOR'] may have been set, but this value can be spoofed by both the client and the proxy. It's not guaranteed to be accurate and shouldn't be trusted. If the user if using a proxy, though, the value in REMOTE_ADDR will be the IP address of the proxy that hits your webserver, not that of the client.
So, assuming you are going to use REMOTE_ADDR, how do you associate the email address to the IP address?
The user clicks a unique link that you have stored with the known email address. That link runs a very simply script and looks at REMOTE_ADDR and maybe HTTP_X_FORWARDED_FOR and you store those two values.
Your table should be as simple as this:
email_address | unique_link | remote_addr | http_x_forwarded_for
The last two values are filled out once the user clicks the unique_link. How you populate email_address initially, remains your secret.
Related
Laravel custom validation rule on valid email
So basically I have a simple form and one of the fields is an email. My controller responsible for this form is the following(showing only the essentials) $messages = array( 'rsvp_email.required' => 'A valid email is required.') ); $rules = array( 'rsvp_email' => 'required|max:150|email', ); $validator = Validator::make($request->all(), $rules,$messages); Now there are 2 scenarios: a) The email is not inserted and the above validation works with the custom message(This works OK) b) The email is not in a valid format (myemail#email) and the resulted error message is The rsvp email must be a valid email address. which is not what I want to be displayed. What additional rule should I include for a valid email? Thank
IF you want to change this message go to the following path: resources/lang/en/validation.php and change value of email index.
How to send mailgun email from form?
I would like to use a form to send a verification email though mailgun to users when they sign up for a service. I have a form collecting the required info for the email but need to put it into the email. The problem is the way the email is formatted and I do not know how to print the data. Here is my action: <?php require 'vendor/autoload.php'; use Mailgun\Mailgun; $mgClient = new Mailgun('MY-API-KEY'); $domain = "https://api.mailgun.net/v3/MY-DOMAIN"; $result = $mgClient->sendMessage($domain, array( 'from' => 'Verifier <MY-ADDRESS>', 'to' => '<?php print_r(GET_$[email]) ?>, second-address#email.com', 'subject' => 'Verifcation & Instructions', 'text' => 'Dear print_r(GET_$[username]), Thank you for requesting a service for print_r(GET_$[url]). To make sure this was you, please click here and verify ...' )); ?> I know the send is working because of the second address I have set up. Thanks for any help!
Here is a possible solution as given to me by Mailgun Support: http://blog.mailgun.com/double-opt-in-with-php-mailgun/
Android GCM collapse even if with different collapse keys
I'm using this lib to send two different messages with different collapse keys, but on my device I'm receiving the first and then the second is coming over the first. I would like to have the two separately in the Android notification header on device. For the record I'm using this Phonegap plugin to receive the push notification. Here is my code: $gcmApiKey = 'api key here'; $deviceRegistrationId = 'device regid here'; $numberOfRetryAttempts = 5; $collapseKey = '1'; $payloadData = ['title' => 'First Message Title', 'message' => 'First message']; $sender = new Sender($gcmApiKey); $message = new Message($collapseKey, $payloadData); $result = $sender->send($message, $deviceRegistrationId, $numberOfRetryAttempts); // Sending Second message $collapseKey = '2'; $payloadData = ['title' => 'Second Message Title', 'message' => 'Second Message']; $sender = new Sender($gcmApiKey); $message = new Message($collapseKey, $payloadData); $result = $sender->send($message, $deviceRegistrationId, $numberOfRetryAttempts);
If I understand you right, your problem is that the first notification is replaced by the second after it was shown. If that is the case, your mistake is not on the PHP-side here, but in your Java-code. If you show a notification you call this method: NotificationManager.notify(int id, Notification notification) Most likely, you are setting the id parameter to the same value each time you call this method. The effect of the id is that the system will only show one notification with the same ID - the newest. A typical use-case to use the same id as before would be to update a previous notification. If you want to display multiple notifications, you need to set a different id each time. You could use a random number or better yet use a previously defined ID of your content. The GCM collapse key has a different effect: When you define a collapse key, when multiple messages are queued up in the GCM servers for the same user, only the last one with any given collapse key is delivered. That means, for example, if your phone was off, you would only receive one message with the same collapse key. It doesn't do anything if your phone receives the first notification before you send the second. To set it with your PhoneGap plugin The plugin has a really messy documentation but if we look into the source code, we'll find this undocumented feature: int notId = 0; try { notId = Integer.parseInt(extras.getString("notId")); } mNotificationManager.notify((String) appName, notId, mBuilder.build()); That means, if you change your payload to, for example: $payloadData = ['title' => 'First Message Title', 'message' => 'First message', 'notId' => mt_rand()]; your notifications won't replace each other.
Laravel form validation - validating against 2 data columns
I have a database table that tracks and email address and a client id. The rule is that and email can only belong to one client id. An example would be that I could add, email#domian.com and the client_id 20 but I would not be able to save that again, and this is where the form validation comes in, I have the following validation rules in my controller, $data = Input::all(); $client_id = Input::get('client_id'); $validation = Validator::make( array('email' => Input::get('email')), array('email' => 'required|email|unique:emails, email, NULL, client_id, $client_id') ); if($validation->fails()) { return Response::json($validation->messages(), 400); } else { } Basically I am saying that the email should be a valid email and it should be unique against all the other emails adress that have the same client id. However I get a PHP error back, Undefined offset: 1 The POST that I am sending looks like this, client_id: "16" email: "simon#simonainley.info" involved: 1 project_id: "64" visible: true
Google Analytics server-side tracking
Google Analytics, by just placing its sourcecode on my website, automatically tracks everything I used to need (pageviews, unique visitors). But now, I need to track events, and the only way to do this is to do it server-side. Each time any users does an specific action i need to track, the server posts data to google to track the information, as explained here: https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide#event And it does works amazingly perfect, but, since I realiced, I am now receiving a LOT of visits from Spain, doubling the visits from USA. And before I implemented the event tracking, Spain wasn't even part of the top 10 countries. Today I have realiced that my servers are in Spain, and that may be causing the issue. How can I track the event, without making it count as a pageview? $url = 'http://www.google-analytics.com/collect'; $data = array('v' => '1', 'tid' => 'UA-HIDDEN-1', 'cid' => $_SERVER["REMOTE_ADDR"], 'ni' => '1', 't' => 'event', 'ec' => '', 'ea' => 'JUMP', 'el' => ''); $options = array( 'http' => array( 'header' => "Content-type: application/x-www-form-urlencoded\r\n", 'method' => 'POST', 'content' => http_build_query($data), ), ); $context = stream_context_create($options); $result = file_get_contents($url, false, $context); Thank you very much!!
You are sending the IP adress as a client id, which is wrong. For one, the client id is supposed to be an UUID. Secondly, Analytics won't recognize that these events belong to an existing user. You'd need to grab the existing client id for an existing user on the web page: ga(function(tracker) { var clientId = tracker.get('clientId'); }); and then send it back to the server and use it in your request (1). At the moment GA cannot assign correct geo information since the events do not belong to the session of the user who initiates the event (this quite possibly affects some other metrics, too). (1) You might as well read the GA cookie in PHP, but Google recommends against it since the cookie format might change without notice. The script above will always return a correct client id even if the cookie format changes. Updated: I have read a bit more documentation and while my answer seems still somewhat relevant it's probably wrong for the actual use case - Geo is determined by IP and the serverside script will still send the servers IP. So quite possibly (haven't done the science yet) this would look like one visitor with two devices instead of a single visitor. Update 2: Apparently it is now possible to include the users IP adress as parameter, so this answer is possibly no longer relevant. Here is a techopad presentation about mixing UA client- and serverside, maybe that helps.
An event in and of itself is not a pageview. See: Event Tracking Is there a specific reason why you need to track events server side and pageviews from the normal ga.js client-side code? You can easily track events from the client side, if you were unaware of that: Click Link to Track Event Assuming that you needed to keep events AND pageviews on the server side: <?php //Put SERVER_ADDR into a var $request_ip = $_SERVER['REMOTE_ADDR']; // Put any server IPs you need to filter out below in an array $localhosts = array('127.0.0.1','192.168.15.1','10.1.10.1'); // Use this later $url = 'http://www.google-analytics.com/collect'; Now, Figure out what to do with the REMOTE_ADDR check if its in our list above. then build an array of type to send GA (events, pageviews) $actions = array(); // Note that the values are arbitrary and will let you do what you need. if(in_array($request_ip)){ //Only track event, or track pageview differently, or track two events. $handle_myServer = true; $actions = ('event'); } else { // Track everyone else $handle_myServer = false; $actions = ('event','pageview','mySpecialPageview','mySpecialEvent'); } Finally We have built a list of events we can use in flow control with existing code for pageviews, user timing, events, etc. Be creative! foreach($actions as $action){ $data = null; $options=null; if($handle_myServer){ $someFlagForGA = 'RequestFromSpainServer'; } if($action == 'event'){ $data = array('v' => '1' , 'tid' => 'UA-HIDDEN-1', ,'cid' => $request_ip ,'ni' => '1' , 't' => 'event' , 'ec' => $someFlagForGA, ,'ea' => 'JUMP', 'el' => '' ); } elseif($action == 'pageview'){ $data = array('v' => '1', 'tid' => 'UA-HIDDEN-1' , 't' => 'pageview' , 'dh'=> 'yourGAenabledDomainHere.com' , 'dp'=> 'ViewedPage.html' , 'dt'=> 'homepage'.' SERVER VISITED '.$someFlagForGA ); } else { // Do whatever else } // Would be better to do below with a single function $options = array( 'http' => array( 'header' => "Content-type: application/x-www-form-urlencoded\r\n", 'method' => 'POST', 'content' => http_build_query($data), ) ,$data); $context = stream_context_create($options); $result = file_get_contents($url, false, $context) or die('Error!!'); } ?>