Jump to content

Newsletter Function - help please!


vapour_

Recommended Posts

Hey everyone,

 

I have been trying, for a while now, to make my own newsletter function that takes a list of emails from a SQL Database (and from a text form) and sends out the newsletter that has been uploaded on the same page as an attachment. I'm not very experienced with PHP and so this is quite beyond me, so far I have had no luck grabbing bits of code, that seemed relevant, from other sources and as such I have a whole bunch of code that I don't really understand. Could someone help me out please? I have uploaded the page (just html) to this directory: http://www.ula.co.nz/test/newsletterUpload.html (it's usually a PHP file, hence why the page looks mucked up)

 

This is the code I am using on the next page (newsletterBrain.php) to send the emails with the attached file.

 

<?php 
include("../scripts/authentication.php");

session_start();

include("fileUploader.php");

$checked = $_POST['all'];

if ($checked == 1) {
include("scripts/db_connect_alt.php");

$sql = "SELECT *
		FROM users
		";

// initiate query
$query = mysql_query($sql);
if (!$query) {
	echo "Could not successfully run query ($sql) from DB: " . mysql_error();
	exit;
}

while ($results = mysql_fetch_assoc($query)) { 
	#
	//define the receiver of the email
	#
	define('MAIL_RCPT', $results['email']);
	#
	//define the subject of the email
	#
	define('MAIL_SUBJECT', 'ULA Newsletter ('.date("d-m-Y").')');

	#
	//supply the text and html versions of your email message
	#
	$text = 'Hello "'.$results['first'].'", the latest ULA Newsletter is attached  Enjoy! \n\nThank you!\nULA - www.ula.co.nz\n\n\nTo unsubscribe from the ULA Newsletter please click on 				        this link:\nhttp://www.ula.co.nz/newsletter/unsubscribe.php?id="'.$results['id'].'" ';
	#
	#
	//provide path to the file to be attached
	#
	$file = $_SESSION['fileDir'];
	#
	//create a boundary string. It must be unique
	#
	//so we use the MD5 algorithm to generate a random hash
	#
	$random_hash = md5(date('r', time()));
	#
	//add boundary string and mime type specification
	#
	$headers = "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-{$random_hash}\"";
	#
	//read the atachment file contents into a string,
	#
	//encode it with MIME base64,
	#
	//and split it into smaller chunks
	#
	$attachment = chunk_split(base64_encode(file_get_contents($file)  ));
	#
	//define the body of the message.
	#
	$body = "--PHP-mixed-{$random_hash}\r\n"
	#
		  . "Content-Type: multipart/alternative; boundary=\"PHP-alt-{$random_hash}\"\r\n"
	#
		  . "--PHP-alt-{$random_hash}\r\n"
	#
		  . "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n"
	#
		  . "Content-Transfer-Encoding: 7bit\r\n\r\n"
	#
		  . "{$text}\r\n\r\n"
	#
		  . "--PHP-alt-{$random_hash}--\r\n\r\n"
	#
		  . "--PHP-mixed-{$random_hash}\r\n"
	#
		  . "Content-Type: " . mime_content_type($file) . "; name=\"" . basename($file) . "\"\r\n"
	#
		  . "Content-Transfer-Encoding: base64\r\n"
	#
		  . "Content-Disposition: attachment\r\n\r\n"
	#
		  . "{$attachment}\r\n"
	#
		  . "--PHP-mixed-{$random_hash}--\r\n\r\n";
	#

	//send the email
	#
	mail(MAIL_RCPT, MAIL_SUBJECT, $body, $headers );

}

}

if ($_POST['emails'] != '') {

$emails = explode(", ", $_POST['emails']);

for($i = 0; $i < count($emails); $i++) {
	#
	//define the receiver of the email
	#
	define('MAIL_RCPT', $emails[$i]);
	#
	//define the subject of the email
	#
	define('MAIL_SUBJECT', 'ULA Newsletter ('.date("d-m-Y").')');

	#
	//supply the text and html versions of your email message
	#
	$text = 'Hello! The latest ULA Newsletter is attached  Enjoy! \n\nThank you!\nULA - www.ula.co.nz\n\n\nTo permanantly subscribe to the ULA Newsletter please click on 				        this link:\nhttp://www.ula.co.nz/newsletter/signup.php';
	#
	#
	//provide path to the file to be attached
	#
	$file = $_SESSION['fileDir'];
	#
	//create a boundary string. It must be unique
	#
	//so we use the MD5 algorithm to generate a random hash
	#
	$random_hash = md5(date('r', time()));
	#
	//add boundary string and mime type specification
	#
	$headers = "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-{$random_hash}\"";
	#
	//read the atachment file contents into a string,
	#
	//encode it with MIME base64,
	#
	//and split it into smaller chunks
	#
	$attachment = chunk_split(base64_encode(file_get_contents($file)  ));
	#
	//define the body of the message.
	#
	$body = "--PHP-mixed-{$random_hash}\r\n"
	#
		  . "Content-Type: multipart/alternative; boundary=\"PHP-alt-{$random_hash}\"\r\n"
	#
		  . "--PHP-alt-{$random_hash}\r\n"
	#
		  . "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n"
	#
		  . "Content-Transfer-Encoding: 7bit\r\n\r\n"
	#
		  . "{$text}\r\n\r\n"
	#
		  . "--PHP-alt-{$random_hash}--\r\n\r\n"
	#
		  . "--PHP-mixed-{$random_hash}\r\n"
	#
		  . "Content-Type: " . mime_content_type($file) . "; name=\"" . basename($file) . "\"\r\n"
	#
		  . "Content-Transfer-Encoding: base64\r\n"
	#
		  . "Content-Disposition: attachment\r\n\r\n"
	#
		  . "{$attachment}\r\n"
	#
		  . "--PHP-mixed-{$random_hash}--\r\n\r\n";
	#

	//send the email
	#
	mail(MAIL_RCPT, MAIL_SUBJECT, $body, $headers );

}
}
?>

 

The first part of the code is for the emails retrieved from the SQL Database, and the second is for the emails entered as extras on the Upload page.

 

As you can see, I have done quite a bit of snatching from random sources.. Any help would be appreciated to get those emails flowing! :)

 

Thanks,

Kind regards,

George Cooke.

Link to comment
Share on other sites

Added you, haven't heard a word from you though. Can anyone help me out? It's kinda urgent and I am really lost. I have looked through other similar threads, but everyone is saying use BBC or PEAR or CRONJOB etc... and I really don't know what that is. I'm not looking for something complex, just a simple way to send an email to lots of addresses (50+) with an attachment.

 

George.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.