Mailchimp API - Subscribe to list - php

I'm working on adding a script to my site for a MailChimp subscribe form. I think I have everything setup right but when I hit the subscribe button I'm getting a blank page.
Here is the script I have currently
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);
require_once 'Mailchimp.php';
$apikey = "XXXXXXXXXXXXXXX";
$Mailchimp = new Mailchimp($apikey);
if (!empty($_POST)) {
$id = "XXXXXXXXXX";
$email = array(
'email' => trim($_POST['email'])
);
$result = $Mailchimp->$lists->subscribe($id, $email, $double_optin=false, $replace_interests=false);
var_dump($result);
}
echo "TESTING";
So I'm not getting the $result variable or "TESTING echo'd right now, so I assume I must be doing something simple wrong. Anyone see anything obvious? I believe I'm using the correct default JSON format. (keys have been X'd out, but the one's I'm using are correct)
Any help is much appreciated.
Thanks!!
EDIT: I have updated the code to something I believe to be more correct, but it still isn't working. I could really use some help on this.

Here's how I've handled AJAX email submissions in the past for MailChimp's API (MCAPI):
define("MC_API_KEY", "Your mailchimp API key");
define("MC_API_LIST", "The list ID to subscribe user to");
define("EMAIL_TO", "email address in case subscription fails");
require "MailChimp.API.class.php";
function json($error = true, $message = "Unknown error") {
die(json_encode(array("error" => $error, "message" => $message)));
}
if(!empty($_POST)) {
$email = !empty($_POST['email']) && filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) ? $_POST['email'] : false;
if($email !== false) {
$api = new MCAPI(MC_API_KEY);
$result = $api->listSubscribe(MC_API_LIST, $email);
if($api->errorCode || !$result) {
if(isset($api->errorCode) && $api->errorCode == 214) { // already subscribed
json(true, "You are already subscribed!");
} else {
$error = "Unable to save user email via MailChimp API!\n\tCode=".$api->errorCode."\n\tMsg=".$api->errorMessage."\n\n";
$headers = "From: your#email.com\r\nReply-to: <{$email}>\r\nX-Mailer: PHP/".phpversion();
mail(EMAIL_TO, "Newsletter Submission FAIL [MC API ERROR]", "{$error}Saved info:\nFrom: {$email}\n\nSent from {$_SERVER['REMOTE_ADDR']} on ".date("F jS, Y \# g:iA e"), $headers);
json(false, "Thank you - your email will be subscribed shortly!");
}
} else {
json(false, "Thanks - A confirmation link has been sent to your email!");
}
} else {
json(true, "Please enter your valid email address");
}
} else json();

SOLVED:
Here is the correct code - hopefully this will help others looking to use the new api -
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);
require_once 'Mailchimp.php';
$apikey = "XXXXXXXXXXXXXXXXXXX";
$Mailchimp = new Mailchimp($apikey);
if (!empty($_POST)) {
$id = "XXXXXXXXXX";
$email = array(
'email' => trim($_POST['email'])
);
$result = $Mailchimp->lists->subscribe($id, $email, $merge_vars=null, $double_optin=false, $replace_interests=false);
}

Related

MailGun Check if an email already is subscribing

I have been trying to solve this in all ways I can figure out, but have surely missed something. I've tried to access the fields using for-loops, just calling it, arrays, objects etc. I just can't get it to work.
What I'm talking about is to check if a provided email in a form already is subscribing to my maillist at MailGun. I don't know how to check this and I've been searching the web for answer for about 1-2 hours now and I'm finally asking here aswell.
My code so far:
<?php
session_start();
ini_set('display_errors', 1);
require_once 'init.php';
if (!isset($_POST['email']) && isset($_POST['name'])) {
echo 'You have to provide an email!';
} else if (!isset($_POST['name']) && isset($_POST['email'])) {
echo 'You have to provide a name!';
} else if (isset($_POST['name'], $_POST['email'])) {
$name = $_POST['name'];
$email = $_POST['email'];
// This is temporary to test and only works if an email existing is provided.
// If an invalid email is provided, an error is cast
// See below for the error
if (!$mailgun->get('lists/' . MAILGUN_LIST . '/members' . $email)) {
echo "Email doesnt exist";
die();
$validate = $mailgunValidate->get('address/validate', [
'address' => $email
])->http_response_body;
if ($validate->is_valid) {
$hash = $mailgunOptIn->generateHash(MAILGUN_LIST, MAILGUN_SECRET, $email);
$mailgun->sendMessage(MAILGUN_DOMAIN, [
'from' => 'noreply#adamastmar.se',
'to' => $email,
'subject' => 'Please confirm your subscription to the mailing list',
'html' => "
Hello {$name},<br><br>
You signed up to our mailing list. Please confirm your subscription below.<br><br>
<a href='http://www.adamastmar.se/confirm.php?hash={$hash}'>Click here to confirm</a>"
]);
$mailgun->post('lists/' . MAILGUN_LIST . '/members', [
'name' => $name,
'address' => $email,
'subscribed' => 'no'
]);
$_SESSION['joined'] = "A message has been sent to the provided email. Please confirm the subscription by clicking the link in the mail.";
header('Location: ./');
}
} else {
$_SESSION['alreadysub'] = "You are already a subscriber to this list!";
header('Location: ./');
}
}
?>
The error I get if I use the code above:
Uncaught exception 'Mailgun\Connection\Exceptions\MissingEndpoint' with message 'The endpoint you've tried to access does not exist.
Check your URL.' in /home/jivusmc/domains/adamastmar.se/public_html/vendor/mailgun/mailgun-php/src/Mailgun/Connection/RestClient.php:258
Stack trace: #0 /home/jivusmc/domains/adamastmar.se/public_html/vendor/mailgun/mailgun-php/src/Mailgun/Connection/RestClient.php(110):
Mailgun\Connection\RestClient->responseHandler(Object(GuzzleHttp\Psr7\Response))
#1 /home/jivusmc/domains/adamastmar.se/public_html/vendor/mailgun/mailgun-php/src/Mailgun/Connection/RestClient.php(195):
Mailgun\Connection\RestClient->send('GET', 'lists/news#mail...') #2 /home/jivusmc/domains/adamastmar.se/public_html/vendor/mailgun/mailgun-php/src/Mailgun/Mailgun.php(215):
Mailgun\Connection\RestClient->get('lists/news#mail...', Array) #3 /home/jivusmc/domains/adamastmar.se/public_html/mailinglist.php(16):
Mailgun\Mailgun->get('lists/news#mail...') #4 {main} thrown in /home/jivusmc/domains/adamastmar.se/public_html/vendor/mailgun/mailgun-php/src/Mailgun/Connection/RestClient.php on line 258
Any help & tips/tricks is appreciated!
I found a solution to the issue I had. Instead of doing everything in an if-statement, I instead surrounded it in a try-catch. I try to check if the email can be fetched from the mailgun list and if it fails, it catches the error and instead adds the mail to the list. (I'm posting it here since it's nearly impossible to find a solution to this in a better way)
$name = $_POST['name'];
$email = $_POST['email'];
try {
$mailgun->get('lists/' . MAILGUN_LIST . '/members/' . $email);
$_SESSION['alreadysub'] = "You are already a subscriber to this list!";
header('Location: ./');
} catch (Exception $e) {
$validate = $mailgunValidate->get('address/validate', [
'address' => $email
])->http_response_body;
if ($validate->is_valid) {
$hash = $mailgunOptIn->generateHash(MAILGUN_LIST, MAILGUN_SECRET, $email);
$mailgun->sendMessage(MAILGUN_DOMAIN, [
'from' => 'noreply#adamastmar.se',
'to' => $email,
'subject' => 'Please confirm your subscription to the mailing list',
'html' => "
Hello {$name},<br><br>
You signed up to our mailing list. Please confirm your subscription below.<br><br>
<a href='http://www.adamastmar.se/confirm.php?hash={$hash}'>Click here to confirm</a>"
]);
$mailgun->post('lists/' . MAILGUN_LIST . '/members', [
'name' => $name,
'address' => $email,
'subscribed' => 'no'
]);
$_SESSION['joined'] = "A message has been sent to the provided email. Please confirm the subscription by clicking the link in the mail.";
header('Location: ./');
}
}
I know this problem and the answer are in PHP. But I just figured out a way in NodeJS and wish I've found a solution for it earlier. Maybe it helps someone.
FYI: I'm checking if an email exists in mailing list from Mailgun.
var DOMAIN = 'YOUR_DOMAIN_NAME';
var mailgun = require('mailgun-js')({ apiKey: "YOUR_API_KEY", domain: DOMAIN});
const array = await mailgun.getList().catch(console.error);
if(checkIfEmailExcistInMailinglist(array.items, dbUser.email)){
// if then do something
}
checkIfEmailExcistInMailinglist(array, email) {
for (var i = 0; i < array.length; i++) {
if (array[i].address === email) {
return true;
}
}
return false;
}

PHP Form Sending Issues (using SendInBlue)

I have a basic PHP contact form which I use on a couple of sites, which sends email using the SendInBlue API.
My problem is that I have the form working perfectly on 1 site, now I am using the EXACT same code for a second site and just changing the email, name, etc - however when testing it I now get this error:
Fatal error: Call to undefined method Mailin::send_email() in
/URL/contactpage.php on line 71
FYI, line 71 is:
$mailin->send_email($data);
I have attached the complete code below - this works perfectly on 1 site, but I get this error on my second site.
Any ideas?
Thank you!
<?php
//Email Details for Form Notifications
$email_to = 'Test#test.com'; //the address to which the email will be sent
$email_to_name = 'Test';
//Form Fields
$fname = $_POST['firstname'];
$lname = $_POST['lastname'];
$email = $_POST['email'];
$fullmessage = $_POST['message'];
//Subject Lines
$subject_to = 'Test';
$subject_touser = 'Test';
//URL Redirects
$confirm = 'TestConfirm';
$errorurl = 'TestError';
//Validate
$emailval = filter_var( $email, FILTER_VALIDATE_EMAIL );
if ($emailval == false)
{
header("Location: $errorurl");
} else {
// The email to us
$message_to = 'Message here';
//
// The email to them
$message_touser = 'Message here';
//
require('Mailin.php');
//Notification Email
$mailin = new Mailin('https://api.sendinblue.com/v2.0','MY_KEY');
$data = array( "to" => array($email_to=>$email_to_name),
"cc" => array($email_to_cc=>$email_to_cc_name),
"from" => array($email,$fname),
"subject" => $subject_to,
"html" => $message_to
);
$mailin->send_email($data);
//Email to User
$mailin = new Mailin('https://api.sendinblue.com/v2.0','MY_KEY');
$data2 = array( "to" => array($email=>$fname),
"from" => array($email_to,$email_to_name),
"subject" => $subject_touser,
"html" => $message_touser
);
$mailin->send_email($data2);
header("Location: $confirm");
}
?>
I was using a deprecated Mailin.php. Updated the file and everything works now.

SwiftMailer 's setFrom not working with variable

I'm trying to send mail for a contact form locally by swiftmailer and gmail. I've checked every line and I know the problem is 'setFrom' .
$app->post('/contact', function ($request, $response, $args){
$body = $this->request->getParsedBody();
$name = $body['name'];
$email = $body['email'];
$msg = $body['msg'];
if(!empty($name) && !empty($email) && !empty($msg) ){
$cleanName = filter_var($name,FILTER_SANITIZE_STRING);
$cleanEmail = filter_var($name,FILTER_SANITIZE_EMAIL);
$cleanMsg = filter_var($name,FILTER_SANITIZE_STRING);
}else {
//redirecting to contact page
}
//sending email
$transporter = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')
->setUsername('xxx#gmail.com')
->setPassword('xxx');
$mailer= \Swift_Mailer::newInstance($transporter);
$message = \Swift_Message::newInstance();
$message->setSubject('Email from our website');
$message->setTo(array('ns.falahian#gmail.com'));
$message->setBody($cleanMsg);
$message->setFrom([
$cleanEmail => $cleanName
]);
$result=$mailer->send($message);
if ($result > 0) {
$path = $this->get('router')->pathFor('home');
return $response->withRedirect($path);
} else {
$path = $this->get('router')->pathFor('contact');
return $response->withRedirect($path);
}
});
as you can see I also use Slim 3 framework. when I run the code I get this error:
Slim Application Error
A website error has occurred. Sorry for the temporary inconvenience.
But if I replace the $cleanEmail with 'x#y.z' the code works!
what should I do?
I know that by using gmail I can't change the sender name but I want to upload this code in a webhost and I don't want to get this issue there.
And can anyone suggest a better way for redirecting in Slim 3? Instead of these two lines:
$path = $this->get('router')->pathFor('contact');
return $response->withRedirect($path);
I've set names for my routes like this:
$app->get('/contact', function ($req, $res, $args) {
return $this->view->render($res, "contact.twig");
})->setName('contact');
thanks a lot!
You probably want to do the following instead.
$cleanEmail = filter_var($email,FILTER_SANITIZE_EMAIL);
$cleanMsg = filter_var($msg,FILTER_SANITIZE_STRING);

php json return creating syntax error on responce

I have looked at many posts here and elswhere on this error without success of resolving my error. In console the form data is being sent via json as expected to my php processing page, the php process is not returning any errors so I'm confident the process is completing. I have checked the way the result is formatted and cannot see anything wrong there, although I'm no expert at ajax/json so I could be wrong, Here is my code
Jquery code
<script type="text/javascript">
$(document).ready(function() {
$("#admin_login_but").click(function() {
if($("#admin_name").val()=="" || $("#admin_password").val()=="" || $("#admin_email").val()=="") {
$(".adminloginError").html("Please enter Admin Name, Admin Email and Admin Password");
return false;
}else{
$(".adminloginError").html('<img src="image/ajax-loader.gif" width="16" height="16" alt=""/>');
var adminName=$("#admin_name").val();
var adminPassword=$("#admin_password").val();
var adminEmail=$("#admin_email").val();
$.post("includes/admin_login.inc.php",{admin_name:adminName,admin_password:adminPassword, admin_email:adminEmail},function(json) {
if(json.result === "success") {
$(".adminloginError").html( "Welcome "+adminName+"!");
setTimeout(function(){
window.location= "admin_secure.php";
},2000);
}else{
$(".adminloginError").html(json.message);
}
});
}
Here is my php processing code
<?php
include_once 'functions.php';
include_once 'db_connect.php';
header("Content-Type: application/json"); //this will tell the browser to send a json object back to client not text/html (as default)
//convert variable (array) into a JSON object
function result($var){
echo json_encode($var);
exit();
}
sec_session_start();
error_reporting(E_ALL); ini_set('display_errors', 1);
//check if surname is empty
if (isset($_POST["admin_login_but"])) {
//check if first_name is empty
if (empty($_POST["admin_name"])) {
$response = array('result'=>'fail', 'message' => 'Missing Admin Name');
result($response);
}else{
// if not empty sanitize first_name input
$admin_name = filter_input(INPUT_POST, 'admin_name', FILTER_SANITIZE_STRING);
}
//Check if email is empty and santize and validate email
if (empty($_POST['admin_email'])) {
$response = array('result'=>'fail', 'message' => 'Missing Admin Email');
result($response);
}else{
$admin_email = filter_var($_POST['admin_email'], FILTER_SANITIZE_EMAIL);
}
if (!filter_var($admin_email, FILTER_VALIDATE_EMAIL)) {
$response = array('result'=>'fail', 'message' => 'The Email is not in a Valid Email Format!');
result($response);
}
//check if register password input is empty
if (empty($_POST["admin_password"])) {
$response = array('result'=>'fail', 'message' => 'Missing Admin Password');
result($response);
} else {
//Sanitize the data passed in 'password'
$admin_password = filter_input(INPUT_POST, 'admin_password', FILTER_SANITIZE_STRING);
}
//validate the data passed in 'password'
if (!preg_match("/^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$/", $admin_password)) {
$response = array('result'=>'fail', 'message' => 'Password is in the Wrong Format!');
result($response);
}
//query database
$results = mysqli_query($mysqli, "SELECT * FROM admin WHERE name = '$admin_name' AND email = '$admin_email' AND hash = '$admin_password'");
// Check if SQL query had erors
if(!$results){
$response = array('result'=>'fail', 'message' => 'sql error: ' . mysqli_error($mysqli));
result($response);
}
// If query was successfull and there are rows do this:
if (mysqli_num_rows($results)>0){
$_GET['name'] = $admin_name;
$response = array('result'=>'success', 'message' => 'User is Authenticated');
result($response);
} else {
$response = array('result'=>'fail', 'message' => 'User Authentication Failed');
result($response);
}
}
?>
I cannot figure out how to resolve this. Can anyone help please
I just figured it out, I hadn't put an else option at the end of my php for the initial post check
if(!isset($_POST)) {
//all the processing code
}else{
$response = array('result'=>'success', 'message' => 'Post is Empty');
result($response);
}

Microsoft oAuth doesn't seems to be working

I'm trying to add microsoft login to an application I'm developing, but I'm repeatedly getting this error which I'm unable to understand.
The URL is :-
https://login.live.com/err.srf?lc=1033#error=invalid_request&error_description=The%20provided%20value%20for%20the%20input%20parameter%20'redirect_uri'%20is%20not%20valid.%20The%20expected%20value%20is%20'https://login.live.com/oauth20_desktop.srf'%20or%20a%20URL%20which%20matches%20the%20redirect%20URI%20registered%20for%20this%20client%20application.&state=1403724714-562028
Code
<?php
require('lib/http.php');
require('lib/oauth_client.php');
$client = new oauth_client_class;
$client->server = 'Microsoft';
//$client->redirect_uri = 'http://'.$_SERVER['HTTP_HOST'].
//dirname(strtok($_SERVER['REQUEST_URI'],'?')).'/login.php';
$client->redirect_uri='http://novostack.com/mscr/login.php';
$client->client_id = 'clietidhere'; $application_line = __LINE__;
$client->client_secret = 'secrethere';
if(strlen($client->client_id) == 0
|| strlen($client->client_secret) == 0)
die('Please go to Microsoft Live Connect Developer Center page '.
'https://manage.dev.live.com/AddApplication.aspx and create a new'.
'application, and in the line '.$application_line.
' set the client_id to Client ID and client_secret with Client secret. '.
'The callback URL must be '.$client->redirect_uri.' but make sure '.
'the domain is valid and can be resolved by a public DNS.');
/* API permissions
*/
$client->scope = 'wl.basic wl.emails wl.birthday';
if(($success = $client->Initialize()))
{
if(($success = $client->Process()))
{
if(strlen($client->authorization_error))
{
$client->error = $client->authorization_error;
$success = false;
}
elseif(strlen($client->access_token))
{
$success = $client->CallAPI(
'https://apis.live.net/v5.0/me',
'GET', array(), array('FailOnAccessError'=>true), $user);
}
}
$success = $client->Finalize($success);
}
if($client->exit)
exit;
if($success)
{
session_start();
$_SESSION['userdata']=$user;
header("location: index.php");
}
else
{
echo 'Error:'.HtmlSpecialChars($client->error);
}
?>
Here's a link to check online :- www.novostack.com/mcr/
I do have the correct settings in my developer console.
What seems to be the problem here?
All suggestions are appreciated.
Make sure that the "Root domain" under APP Settings is equal to your caller domain (www.novostack.com).

Categories