Site icon A5THEORY

Export Gmail Contacts in a CSV using PHP.

gmailexportfeaturesimg

Hello Friends, In this blog post(Export Gmail contacts in PHP), I am going to let you know how to export Gmail contacts into a CSV file Excel sheet, or Excel file and use them for a different purpose.

Within this blog(Export Gmail contacts in PHP), we will explore How to export my Google contacts to a CSV file. How do I import contacts from Excel to CSV and Gmail? How do I export a CSV file in PHP?……

….How do I convert contacts to CSV? Import Google contacts in PHP, How to import Gmail contacts in PHP, Google contacts API PHP, Working with contacts in PHP.|Export Gmail contacts in PHP|

It is hard to find a user on this planet who does not use Google products like Gmail, Google Drive, etc, even would have used at least one which might be Gmail.

Out of all Google products, Gmail is the most usable service used by millions of people across the world.|Export Gmail contacts in PHP|

Gmail is a public email system that is used to send and receive emails from one place to another place. People use Gmail for their personal as well as professional uses.

So if sometimes you need to export Gmail contacts in terms of email IDs as you decide on them for email marketing purposes or something else then this application can be a great help that will help you in downloading all your Gmail contacts in a CSV file.

Try Live Demo

we may require Google contacts or email IDs for several purposes one of them could be sending the email campaign to all your Gmail contacts in one day which would not be feasible with Gmail as they have a daily limit for email sending.


So now the question is, how can you extract or export Gmail contacts using our application?

This would be simple to develop this functionality to export Gmail contacts from your Gmail account. So before getting you to the main coding part let me just introduce some important aspects of this core functionality.

Before going to the complex coding part just have a look at the basic terminology listed below.

While you are developing this code product then you will encounter one term again and again which is Google OAuth.


Now one question will arise in your mind what is this Google OAuth and how is it useful to export Gmail contacts?

OAuth stands for “open standard for authorization” and provides a strong authorization scheme on behalf of the owner, OAuth was started with oauth1.0

but it has been deprecated and replaced with oauth2.0, make sure you are using oauth2.0

OAuth Code: This token is only available with successful authorization and further we can request an access token.

Access Token: The access token is just like a key that is used to access almost all APIS data.

Refresh Token: Often! we are confused about refreshing the token. The refresh token is not separate even though it is the same access token, but its value gets refreshed after leaving one complete flow or logging out from your session.

So Simply get the access token make its session and maintain it till you log and at the time of re-login you will get a new access token or you can call it a refresh token.

Steps for getting Google contacts or exporting Gmail Contacts: here we are explaining the step-by-step process for accessing Google contacts.

our goal is to get three important things to make this code possible.

Client ID, Client Secret, and API key. See the below procedure to obtain all these values.


First, copy and paste this link into the browser https://console.developers.google.com/project, this is a link to the developer console.

Log in to your Google or Gmail account if you have not logged in yet.













Once you get your client ID, client secret, and API then you just need to create a project, here we are creating a project at NetBeans IDE. See the file structure below.


index.php:

[code]
<html>
<head>
<link href="css/style.css" rel="stylesheet" type="text/CSS"/>
<link href='http://fonts.googleapis.com/css?family=Source+Sans+Pro|Open+Sans+Condensed:300|Raleway' rel='stylesheet' type='text/css'>
<title>Export Google Contacts Using PHP</title>
<meta name="robots" content="no-index, no-follow">

</head>
<body>

<div id="main" class="col-sm-6 col-MD-4 col-Lg-2">
<div id="envelope" class="col-sm-6 col-MD-4 col-Lg-2">

<header id="sign_in" class="col-sm-6 col-MD-4 col-Lg-2">
<center>
<h2>Export Gmail Contacts With Google Oauth PHP</h2>
</center>
</header>
<hr>
<div id="content" class="col-sm-6 col-MD-4 col-Lg-2">
<center>
<h3 id ="sign">Sign In with Google for retrieving Contacts</h3>
<div class="col-sm-6 col-MD-4 col-Lg-2">
<a href="https://accounts.google.com/o/oauth2/auth?client_id=<PASTE YOUR CLIENT ID>&redirect_uri=http://localhost/gmail_export/callback.php&scope=https://www.google.com/m8/feeds/&response_type=code"><img src="images/sign1.png" alt="" id="signing"/></a>
</div>
</center>
</div>
</div>
</div>
</body>
</html>
[/code]

style.css:

[code]
body {
margin:0;
padding:0;
font-family: railway;
background-image: URL(../images/repeat-bg.png);
}
#main{
width: 70%;
height: 100%;
margin-left: 30%;
margin-top: 5%;
position: relative;
}

h2{
font-family: railway;
color: black;
}
div#envelope{
position: relative;
width: 60%;
border: 1px solid #CFCFD0;
border-radius: 10px;
background-color: #FFFFFF;

}
div#content{
width: 100%;

padding:12px auto;
overflow: hidden;
word-break: break-all;
overflow-wrap: break-word;
border-radius: 0 0 10px 10px;
background-color: #f7ecec;
}
header#sign_in{
font-family: railway;
background-color: #4e562dc7;
text-align: center;
padding-top: 12px;
//padding-left: 5px;
padding-bottom: 15px;
margin-bottom: -8px;
border-radius: 10px 10px 0 0;
color: white;
}
header#info{
font-family: railway;
background-color: #079BAE;
margin-top: -11px;
padding-top:15px;
padding-bottom: 42px;
padding-left: 15px;
padding-right: 15px;
font-size: 16px;
border-radius: 10px 10px 0 0;
color: white;

}
header#info a.user_name{
font-family: railway;
text-decoration: none;
color: white;
}
div. logout{
padding-top: 30px;
float: right;
}
a.logout{
font-family: railway;
font-size: 20px;
font-weight: 600;
text-decoration: none;
padding-right: 0 auto;
float: right;
color: white;
}
p {
padding: 0 25px;
}
p.profile {
font-weight: bold;
font-size: 20px;
}
Img.user_img{
border-radius:50% 50% 50% 50%;
border:3px solid white;
}
p.welcome{
margin-top: -50px;
margin-left: 12%;
}
#sign{
//margin-left: 150px;
}
#signimg{
width:70%;

}
#logoutimg{

margin-left: 500px;
margin-bottom: 5px;
color: blue;
float: left;
position: relative;
margin-top: -35px;

}
#tbdata
{
border: 1px solid light grey;
margin-top: -20px;
font-family: railway;
background color: light gray;
border: transparent;

}

#tb{
width:97.5%;
margin-left: 10px;
border: 1px solid gray;
box-shadow:#079BAE;

}
#btnimg
{
width:50px;
height: 50px;

color: blue;
position: absolute;
margin-bottom: -15px;
margin-left: -270px;

}

#name{
padding-right: 30px;
padding-left: 30px;
}
[/code]

callback.php:

[code]
<?php
session_start();
?>
<?php
$accesstoken = '';
$client_id = '<PASTE YOUR CLIENT ID HERE>';
$client_secret = '<PASTE YOUR CLIENT SECRET HERE>';
$redirect_uri = 'http://localhost/gmail_export/callback.php';
$simple_api_key = '<PASTE YOUR API KEY HERE>';
$max_results = 500;
$auth_code = $_GET["code"];

function curl_file_get_contents($url) {
$curl = curl_init();
$userAgent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)';

curl_setopt($curl, CURLOPT_URL, $url); //The URL to fetch. This can also be set when initializing a session with curl_init().
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); //TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5); //The number of seconds to wait while trying to connect.

curl_setopt($curl, CURLOPT_USERAGENT, $userAgent); //The contents of the "User-Agent: " header is to be used in an HTTP request.
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE); //To follow any "Location: " header that the server sends as part of the HTTP header.
curl_setopt($curl, CURLOPT_AUTOREFERER, TRUE); //To automatically set the Referer: field in requests where it follows a Location: redirect.
curl_setopt($curl, CURLOPT_TIMEOUT, 10); //The maximum number of seconds to allow cURL functions to execute.
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); //To stop cURL from verifying the peer's certificate.
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);

$contents = curl_exec($curl);
curl_close($curl);
return $contents;
}

$fields = array(
'code' => urlencode($auth_code),
'client_id' => urlencode($client_id),
'client_secret' => urlencode($client_secret),
'redirect_uri' => urlencode($redirect_uri),
'grant_type' => urlencode('authorization_code')
);
$post = '';
for each ($fields as $key => $value) {
$post .= $key . '=' . $value. '&';
}
$post = rtrim($post, '&');

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://accounts.google.com/o/oauth2/token');
curl_setopt($curl, CURLOPT_POST, 5);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
$result = curl_exec($curl);

curl_close($curl);

$response = json_decode($result);
if (isset($response->access_token)) {
$accesstoken = $response->access_token;
$_SESSION['access_token'] = $response->access_token;
}

if (isset($_GET['code'])) {

$accesstoken = $_SESSION['access_token'];
}

if (isset($_REQUEST['logout'])) {
unset($_SESSION['access_token']);
}

$url = 'https://www.google.com/m8/feeds/contacts/default/full?max-results=' . $max_results . '&oauth_token=' . $accesstoken;
$xmlresponse = curl_file_get_contents($url);

if ((Strlen(Stristr($xmlresponse, 'Authorization required')) > 0) && (Strlen(Stristr($xmlresponse, 'Error ')) > 0)) {
echo "<h2>OOPS !! Something went wrong. Please try reloading the page.</h2>";
exit();
}

//echo " <a href ='http://127.0.0.1/gmail_contact/callback.php?downloadcsv=1&code=4/eK2ugUwI_qiV1kE3fDa_92geg7s1DusDsN9BHzGrrTE# '><img src='images/excelimg.jpg' alt=''id ='downcsv'/></a>";
// echo "<h3>Email Addresses:</h3>";
$xml = new SimpleXMLElement($xmlresponse);
$xml->registerXPathNamespace('gd', 'http://schemas.google.com/g/2005/Atom');

$result = $xml->xpath('//gd:email');

foreach ($result as $title) {
$arr[] = $title->attributes()->address;
echo $title->attributes()->displayName;
}
//print_r($arr);
foreach ($arr as $key) {
//echo $key."<br>";
}

$response_array = json_decode(json_encode($arr), true);

// echo "<pre>";
// print_r($response_array);
//echo "</pre>";

$email_list = '';
for each ($response_array as $value2) {

$email_list = ($value2[0] . ",") . $email_list;
}

//echo $abc;
// $final_array[] = $abc;
// $farr =$final_array;
//echo "<pre>";
//print_r($final_array);
// echo "</pre>";
//<input type="text" value="<?php echo ($abc);?" name="email">
?>

<html>
<head>
<link href="css/style.css" rel="stylesheet" type="text/css"/>
<link href="css/table.css" rel="stylesheet" type="text/css"/>
<link href='http://fonts.googleapis.com/css?family=Source+Sans+Pro|Open+Sans+Condensed:300|Raleway' rel='stylesheet' type='text/css'>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Export Google Contacts Using PHP</title>
<meta name="robots" content="noindex, nofollow">

</head>
<body>

<div id="main" >

<div class="col-sm-6 col-md-4 col-lg-2">
<div id="envelope" class="col-sm-6 col-md-4 col-lg-2">

<header id="sign_in">
<h2> <form action='csvdownload.php' method='post'>
<input type='text' value= '<?php echo $email_list; ?>' name='email' style='display: none'>

<input type='image' width='100' value='submit' src='images/excelimg1.gif' alt='submit Button' id ='btnimg'>
</form>Export Gmail Contacts<a href='https://www.google.com/accounts/Logout?continue=https://appengine.google.com/_ah/logout?continue=http://localhost/gmail_export/index.php' ><img src='images/logoutimg1.png' alt=''id ='logoutimg'/></a></h2>
</header>
<hr>
<div id="content" class="col-sm-6 col-md-4 col-lg-2">
<div class="col-sm-6 col-md-4 col-lg-2">

<div class="col-sm-6 col-md-4 col-lg-2">

<table cellspacing='0'>
<thead>
<td id="name">S.No</td>
<td>Email Addresses</td>
</thead>
<?php
$count = 0;
foreach ($result as $title) {
?>
<tr>
<td><?php echo $count;
$count++ ?></td>
<link href="style.css" rel="stylesheet" type="text/css"/>
<td><?php echo $title->attributes()->address; ?></td>
</tr>
<?php
}
?>

</table>

</div>

</div>
</div>
</div>
</div>
</div>

</body></html>
[/code]

csvdownload.php:

[code]
<?php

$data = $_POST['email'];

$arry[] = explode(',', $data);

for each ($arry as $row) {
$arlength = count($row);
for ($i = 0; $i < $arlength; $i++) {
$farry[] = explode(',', $row[$i]);
}
}

header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=file.csv");
header("Pragma: no-cache");
header("Expires: 0");

$file = fopen('php://output', 'w');
fputcsv($file, array('Email'));
foreach ($farry as $row) {
fputcsv($file, $row);
}
exit();
[/code]

table.css:
[code]

table a: link {
color: #666;
font-weight: bold;
text-decoration: none;
}
table a: visited {
color: #999999;
font-weight: bold;
text-decoration: none;
}
table a: active,
table a: hover {
color: #bd5a35;
text-decoration: underline;
}
table {
font-family: 'Raleway', sans-serif;
//color:#666;
font-size:12px;
text-shadow: 1px 1px 0px #fff;
background:#eaebec;
//margin:20px;
border:#ccc 1px solid;
font-size: 16px;
-moz-border-radius:3px;
-WebKit-border-radius:3px;
border-radius:3px;

-moz-box-shadow: 0 1px 2px #d1d1d1;
-webkit-box-shadow: 0 1px 2px #d1d1d1;
box-shadow: 0 1px 2px #d1d1d1;
margin: 50px 50px;
width: 85%;

}
table Th {
padding:21px 25px 22px 25px;
border-top:1px solid #fafafa;
border-bottom:1px solid #e0e0e0;

background: #ededed;
background: -Webkit-gradient(linear, left top, left bottom, from(#ededed), to(#ebebeb));
background: -Moz-linear-gradient(top, #ededed, #ebebeb);
}
table Th:first-child {
text-indent: center;
padding-left:20px;
}
table tr:first-child Th:first-child {
-Moz-border-radius-topleft:3px;
-WebKit-border-top-left-radius:3px;
border-top-left-radius:3px;
}
table tr:first-child Th:last-child {
-Moz-border-radius-topright:3px;
-WebKit-border-top-right-radius:3px;
border-top-right-radius:3px;
}
table tr {
text-indent: center;
padding-left:20px;
}
table Td:first-child {
text-align: center;
padding-left:20px;
border-left: 0;
}
table Td {
padding:18px;
border-top: 1px solid #ffffff;
border-bottom:1px solid #e0e0e0;
border-left: 1px solid #e0e0e0;

background: #fafafa;
background: -Webkit-gradient(linear, left top, left bottom, from(#fbfbfb), to(#fafafa));
background: -Moz-linear-gradient(top, #fbfbfb, #fafafa);
}
table tr. even Td {
background: #f6f6f6;
background: -Webkit-gradient(linear, left top, left bottom, from(#f8f8f8), to(#f6f6f6));
background: -Moz-linear-gradient(top, #f8f8f8, #f6f6f6);
}
table tr:last-child Td {
//border-bottom:0;
}
table tr:last-child Td:first-child {
-Moz-border-radius-bottomleft:3px;
-WebKit-border-bottom-left-radius:3px;
border-bottom-left-radius:3px;
}
table tr:last-child Td:last-child {
-Moz-border-radius-bottomright:3px;
-WebKit-border-bottom-right-radius:3px;
border-bottom-right-radius:3px;
}
table tr: hover Td {
background: #f2f2f2;
background: -Webkit-gradient(linear, left top, left bottom, from(#f2f2f2), to(#f0f0f0));
background: -Moz-linear-gradient(top, #f2f2f2, #f0f0f0);
}
[/code]

contact.css:

[code]

@import Url(http://fonts.googleapis.com/css?family=Raleway);

h1{
text-align: center;
//color: black;
font-size: 2em;
}

#main{
margin: -150px 100px;
font-family: 'Raleway', sans-serif;
}

h2{
background-color: #FEFFED;
text-align: center;
border-radius: 10px 10px 0 0;
margin: -58px -40px;
padding: 40px 40px;
color: black;
font-weight: bolder;
font-size: 1.5em;
margin-bottom: -67px;
}

hr{
border:0;
border-bottom:1px solid #ccc;
margin: 10px -40px;
}

#login{
width:580px;
float: left;
border-radius: 10px;
font-family:railway;
border: 2px solid #ccc;
padding: 0px 40px 0px;
//margin-top: 70px;
//margin: 50px;
margin: 15px 345px;
}

input[type=text],input[type=email],input[type=password]{
width:99.5%;
padding: 10px;
margin-top: 8px;
border: 1px solid #ccc;
padding-left: 5px;
font-size: 16px;
font-family:railway;
}

textarea{
width:99.5%;
padding: 10px;
margin-top: 8px;
border: 1px solid #ccc;
padding-left: 5px;
margin-bottom: 5px;
font-size: 16px;
font-family:railway;
}

input[type=submit]{
width: 40%;
background-color:#FFBC00;
color: white;
border: 2px solid #FFCB00;
padding: 10px;
font-size:20px;
cursor: pointer;
border-radius: 5px;
margin-bottom: 12px;
}

h3{
text-align: center;
font-weight: bold;
}

table{
width: 100%;
text-align: center;
}

thead{
font-weight: bold;
}

#contab td{
border: 1px solid black;
}

#download{
float: left;
margin-top: 7px;
}

#logout{
float: right;
margin-top: -31px;
margin-right: -14px;
}

a{
text-decoration: none;
font-weight: bold;
}

img{
// margin-top: -60px;
}

h2 span {
margin-left: -67px;
}
[/code]

You can also go through a few important blog links related to email marketing below:

How to stop email from falling into the user’s spam folder.

See a few best tips for effective email marketing.

Learn Email marketing in Hindi.

30+ Best Email marketing service providers study and reviews.

8 Best Tips for sending a successful email campaign.

Domain Registration vs Domain Hosting In Hindi…

Email को Spam folder में जाने से कैसे रोके?…

Which domain is best for effective email marketing…

What does it mean if your account is suspended?…

How badly do emails impact your email campaign…

SPF और DKIM रिकार्ड्स क्या होते है, कहा पर मिलते है और कहा पर उन्हें रखना होता है ?…

Email marketing क्या है और इसका उपयोग कैसे करते है ?… How do I check if a sent email ended up in the receiver’s spam box automatically… हम Email में कितनी बड़ी file attachment भेज सकते है … Can Yahoo Send Scheduled Emails… SMTP क्या है और कैसे काम करता है… Most Common Email Marketing Mistakes In Hindi… Email Scraping In Hindi… Email marketing क्या है और इसका उपयोग कैसे करते है… SPF and DKIM records in Hindi… 5 Free Email Marketing Software…

Conclusion:

Using this blog(Export Gmail contacts in PHP) we have learned how to use Google API to fetch Gmail contacts in PHP. We have also explored How to export my Google contacts to a CSV file. How do I import contacts from Excel to CSV and Gmail? How do I export a CSV file in PHP? How do I convert contacts to CSV? Import Google contacts in PHP, How to import Gmail contacts in PHP, Google contacts API PHP, Working with contacts in PHP|Export Gmail contacts in PHP|

In case of any queries, you can write to us at a5theorys@gmail.com we will get back to you ASAP.|Export Gmail contacts in PHP|

Hope! you would have enjoyed this post to export Gmail contacts in a CSV file.|Export Gmail contacts in PHP|

Please feel free to give your important feedback in the comment section below.|Export Gmail contacts in PHP|

Have a great time! Sayonara!

Exit mobile version