How to send same email to multiple recipients in PHP by CSV upload

by · December 11, 2012

Send email message at the same time to multiple emails by using PHP and CSV excel file. Just upload your CSV file having email ids in first column and type your message in text area  and click on send button. This will work as email shooter.

The main benefit to use this is that no one is in carboncopy(cc), So no one can see any others email.

Why to use this:

1)      You can do this for Email Marketing.

2)      Email Promotions of your product.

3)      Invitations to group.

4)      Send assignments to students.

Email Shooter

Email Shooter

You can do this by 3 steps

1) HTML Form:

HTML Form for frontend shooter user display.

<div style=”width:1000px;margin:auto;”>

<h1>knowAhead  E-mail Shooter</h1>

<form method=”post” enctype=”multipart/form-data”>

Upload CSV file<input type=”file” name=”file_up” />

<br/>Message<textarea class=”ckeditor” name=”editor1″></textarea>

<br/><input type=”submit” name=”submit” value=”Submit” />

</form>

</div>

2) Get email id’s from CSV file:

$input_filename = $_FILES['file_up']['tmp_name'];

$input_filesize = filesize($input_filename);

if (($handle = fopen($input_filename, “r”)) === FALSE) {

die(‘Error opening file’);

}

 

$cardCodes = array();

while ($row = fgetcsv($handle, $input_filesize, “,”)) {

$cardCodes[] = $row;

}

 

$cardCodes is an array containing all of the email ids from csv file.

 

3) Email Validation by PHP and Send HTML mail without getting it to spam folder:

Major challenge while sending these emails are they can’t be sent in SPAM folder. To do that we should use multiple headers.

After that Validate email ids from array that they  are valid or not. If valid then send mail.

 

foreach($cardCodes as $cardCodes1){

// multiple recipients

$to  = $cardCodes1[0];

// subject

$subject = ‘Your company name newsletter’;

// To send HTML mail, the Content-type header must be set

$headers  = ‘MIME-Version: 1.0′ . “\r\n”;

$headers .= ‘Content-type: text/html; charset=iso-8859-1′ . “\r\n”;

// Additional headers

$headers .= ‘To: <’.$cardCodes1[0].’>’ . “\r\n”;

$headers .= ‘From: Your Company name<info@yourcompanyname.in>’ . “\r\n”;

if (filter_var($to, FILTER_VALIDATE_EMAIL)) {

// Mail it

mail($to, $subject, $message, $headers);

}

}

 

Complete Code:

 

<?php

if(isset($_POST['submit'])){

$input_filename = $_FILES['file_up']['tmp_name'];

$input_filesize = filesize($input_filename);

if (($handle = fopen($input_filename, “r”)) === FALSE) {

die(‘Error opening file’);

}

/////////////// Reading the data from CSV File ///////////////////

//$headers = fgetcsv($handle, $input_filesize, ‘,’);

$cardCodes = array();

while ($row = fgetcsv($handle, $input_filesize, “,”)) {

$cardCodes[] = $row;

}

// message

$message =

‘<table width=”700″ cellspacing=”0″ cellpadding=”0″ border=”0″ align=”center”>

<tr>

<td>

<h2>Email shooter:</h2> Email permotion by knowAhead

</td>

</tr>

<tr>

<td>

‘;

$message .= $_POST['editor1'];

$message .=’

</td>

</tr>

<tr>

<td>&nbsp;

</td>

</tr>

‘;

$message .=’ <tr>

<td>

For more informaion visit our website at<a href=”#” target=”_blank”>http://www.knowAhead.in</a>

</td>

</tr>

</table>’;

// message end

foreach($cardCodes as $cardCodes1){

// multiple recipients

$to  = $cardCodes1[0];

// subject

$subject = ‘Your company name  newsletter’;

// To send HTML mail, the Content-type header must be set

$headers  = ‘MIME-Version: 1.0′ . “\r\n”;

$headers .= ‘Content-type: text/html; charset=iso-8859-1′ . “\r\n”;

// Additional headers

$headers .= ‘To: <’.$cardCodes1[0].’>’ . “\r\n”;

$headers .= ‘From: YourCompanyName <info@yourcompanyname.in>’ . “\r\n”;

if (filter_var($to, FILTER_VALIDATE_EMAIL)) {

// Mail it

mail($to, $subject, $message, $headers);

}

}

/////////////// Reading the data from CSV File end ////////////////

}

?>

<div style=”width:1000px;margin:auto;”>

<h1>knowAhead E-mail Shooter</h1>

<form method=”post” enctype=”multipart/form-data”>

Upload CSV file<input type=”file” name=”file_up” />

<br/><br/>Message<textarea name=”editor1″></textarea>

<br/><br/>Upload image<input type=”file” name=”main_image” /><br/><br/>

<input type=”submit” name=”submit” value=”Submit” />

</form>

</div>

VN:F [1.9.22_1171]
Rating: 8.4/10 (7 votes cast)
VN:F [1.9.22_1171]
Rating: +3 (from 3 votes)
How to send same email to multiple recipients in PHP by CSV upload, 8.4 out of 10 based on 7 ratings

You may also interested in:

Category howTo

  • User

    This is of great help!!
    Thanks

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)