2

我正在尝试“整合”从我的联系表格中订阅 mailchimp 通讯/列表的能力。因此,当用户单击复选框并提交表单时,它会订阅它们。

我试过这个教程:http ://www.joshuawinn.com/subscribe-to-mailchimp-newsletter-option-on-contact-form/

这是我的PHP代码:

<?php
if( isset($_POST) ){

//cut//

// SUBSCRIBE TO MAILING LIST OPTION – ADD TO MAILCHIMP USING API
if ( $_POST['emailUpdates'] == ‘yes’ )
{
// Include Mailchimp API class
require_once(‘http://22twenty.com/wordpress/wp-content/themes/~~~~HIDDEN/MCAPI.class.php’);

// Your API Key: http://admin.mailchimp.com/account/api/
$api = new MCAPI(‘KEYHIDDEN’);

// Your List Unique ID: http://admin.mailchimp.com/lists/ (Click “settings”)
$list_id = “IDHIDDEN”;

// Variables in your form that match up to variables on your subscriber
// list. You might have only a single ‘name’ field, no fields at all, or more
// fields that you want to sync up.
$merge_vars = array(
‘FNAME’ => $_POST['name'],
);

// SUBSCRIBE TO LIST
if ( $api->listSubscribe($list_id, $_POST['email'], $merge_vars) === true ){
$mailchimp_result = ‘Success! Check your email to confirm sign up.’;
} else {
$mailchimp_result = ‘Error: ‘ . $api->errorMessage;
}
}

希望有人可以帮助杰夫

4

2 回答 2

0

尝试这个:

<link href="http://cdn-images.mailchimp.com/embedcode/slim-081711.css" rel="stylesheet" type="text/css">
<style type="text/css">
    #mc_embed_signup{background:#fff; clear:left; font:14px Helvetica,Arial,sans-serif; }
    /* Add your own MailChimp form style overrides in your site stylesheet or in this style block.
       We recommend moving this block and the preceding CSS link to the HEAD of your HTML file. */
</style>
<div id="mc_embed_signup">
<form action="your-action-url" method="post" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate" target="_blank">
    <label for="mce-EMAIL">Subscribe to our mailing list</label>
    <input type="email" value="" name="EMAIL" class="email" id="mce-EMAIL" placeholder="email address" required>
    <div class="clear"><input type="submit" value="Subscribe" name="subscribe" id="mc-embedded-subscribe" class="button"></div>
</form>
</div>
于 2012-10-09T04:10:55.617 回答
0

解决它!

方法如下,`

//form validation vars
$formok = true;
$errors = array();

//sumbission data
$ipaddress = $_SERVER['REMOTE_ADDR'];
$date = date('d/m/Y');
$time = date('H:i:s');

//form data
$name = $_POST['name']; 
$email = $_POST['email'];
$telephone = $_POST['telephone'];
$enquiry = $_POST['enquiry'];
$message = $_POST['message'];
$apikey = 'key';
$listID = 'id';


    //
    if (!empty($_POST['newsletter'])) {
        $url = sprintf('http://api.mailchimp.com/1.2/?method=listSubscribe&apikey=%s&id=%s&email_address=%s&merge_vars[OPTINIP]=%s&merge_vars[NAME]=name&output=json', $apikey, $listID, $email, $_SERVER['REMOTE_ADDR']);
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $data = curl_exec($ch);
        curl_close($ch);
        $arr = json_decode($data, true);

}
//validate form data

//validate name is not empty
if(empty($name)){
    $formok = false;
    $errors[] = "You have not entered a name";
}

//validate email address is not empty
if(empty($email)){
    $formok = false;
    $errors[] = "You have not entered an email address";
//validate email address is valid
}elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)){
    $formok = false;
    $errors[] = "You have not entered a valid email address";
}

//validate message is not empty
if(empty($message)){
    $formok = false;
    $errors[] = "You have not entered a message";
}
//validate message is greater than 20 charcters
elseif(strlen($message) < 20){
    $formok = false;
    $errors[] = "Your message must be greater than 20 characters";
}

//send email if all is ok
if($formok){
    $headers = "From: email.ca" . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

    $emailbody = "<p>You have recieved a new message from the enquiries form on your website.</p>
                  <p><strong>Name: </strong> {$name} </p>
                  <p><strong>Email Address: </strong> {$email} </p>
                  <p><strong>Telephone: </strong> {$telephone} </p>
                  <p><strong>Message: </strong> {$message} </p>
                  <p>This message was sent from the IP Address: {$ipaddress} on {$date} at {$time}</p>";

    mail("email@gmail.com","New Enquiry",$emailbody,$headers);

}

//what we need to return back to our form
$returndata = array(
    'posted_form_data' => array(
        'name' => $name,
        'email' => $email,
        'telephone' => $telephone,
        'message' => $message
    ),
    'form_ok' => $formok,
    'errors' => $errors
);




//if this is not an ajax request
if(empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !== 'xmlhttprequest'){
    //set session variables
    session_start();
    $_SESSION['cf_returndata'] = $returndata;

    //redirect back to form
    header('location: ' . $_SERVER['HTTP_REFERER']);
}

}`

我改变了一点,但现在效果很好!

于 2012-10-11T03:55:33.517 回答