Jump to content

Question using PEAR and getting a variable


tryingtolearn

Recommended Posts

Hi all, Trying again,

Hoping someone can shed some light on this question.

 

Im writing a very basic email function

I was using mail() but when it was sent - all mails were sent but the page itself would timeout

I hit refresh (Which was a big mistake) and it sent all the emails out again!

 

So I rewrote it to use PEAR

but I need to pass the recipients email as a variable to track who it was sent to.

Using PEAR I have an array of recipients

 

Is it possible to pass the individual email in a variable in the message itself?

 

If you look at the $html variable in the code below,

Where it says ***NEEDS TO BE EMAIL ADDRESS***

Thats where I need the single email

so when the email is opened that 1 email address gets inserted into the DB

 

I tried adding $html in a foreach but only got the last email address

Then I tried just passing the $recipients variable but anly got ARRAY in the DB

 

Any help would be appreciated

 

include('Mail.php');
        include('Mail/mime.php');

        // Constructing the email
        $sender = "from address";                                  // Your name and email address
        $track_id = date("D M j Y - g:i a");
	$recipients = array();
	$query = "SELECT * FROM mailer WHERE mail_receive='Yes'";		
    $result = @mysql_query ($query); // Run the query.
	while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
        $recipients[] = $row['mail_email'];            // Recipient name and email address
	}

	$html = "<img src=\"http://www.site.com/tracksent.php?t=$em&e=***NEEDS TO BE EMAIL ADDRESS***&track_id=$track_id\" width=\"1\" height=\"1\"><br />$message";

        $subject = $em;                                                // Subject for the email
        $text = 'Your eMail will only accept text messages.To see this message as intended visit this link '; 
	$crlf = "\n";
        $headers = array(
                        'From'          => $sender,
                        'Return-Path'   => $sender,
                        'Subject'       => $subject 
                        );

        // Creating the Mime message
        $mime = new Mail_mime($crlf);

        // Setting the body of the email
        $mime->setTXTBody($text);
        $mime->setHTMLBody($html);

        // Add an attachment
        //$file = "Hello World!";
        //$file_name = "Hello text.txt";
        //$content_type = "text/plain";
        //$mime->addAttachment ($file, $content_type, $file_name, 0);

        // Set body and headers ready for base mail class 
        $body = $mime->get();
        $headers = $mime->headers($headers);

        // SMTP params
        $smtp_params["host"] = "smtp.site.com"; // SMTP host
        $smtp_params["port"] = "587";
	$smtp_params["auth"]     = true;
        $smtp_params["username"] = "uname";
        $smtp_params["password"] = "upass";
        // Sending the email using smtp
        $mail =& Mail::factory("smtp", $smtp_params); 
        $result = $mail->send($recipients, $headers, $body);
        if($result)
        {
          echo("Your message has been sent!");
        echo"<h3>$em was sent to:</h3>";
	foreach($recipients as $k => $value){
	echo "$value <br />";
	$query = "INSERT INTO sent (title, sent_to, track_id, track_key) VALUES ('$em', '$value', '$track_id', '$k')";		
        $result = @mysql_query ($query); // Run the query.
	}		
	}
        else
        {
          echo("Your message was not sent: " . $result);
        }

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.