Page 1 of 1

Need help with lists and sending HTML email in PHP

Posted: Mon Mar 26, 2012 1:11 pm
by ecros
Hello All, it has been about 5 years since I have had to write anything in PHP, so forgive me. I am trying to load data from a .csv file (name, email address, restaurant name) and then run through the number of records in the file and send an individualized email to each person on the list. The emails should go out as an HTML email.

I have been playing around with this, but it isn't right:

Code: Select all

<?php
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        echo "<p> $num fields in line $row: <br /></p>\n";

//data stored in $data
echo $data['fieldname'];
        $row++;
        for ($c=0; $c < $num; $c++) {
            echo $data[$c] . "<br />\n";
        }
    }
    fclose($handle);
}
?>
<? 
    //change this to your email. 
    $to = $data0; 
    $from = "rmy@yinfo.com"; 
    $subject = "Your Invitation To Appear In Food & Wine Magazine."; 

    //begin of HTML message 
    $message = <<<EOF 
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<style type="text/css">
<!--
.style1 {
	font-family: Arial, Helvetica, sans-serif;
	font-size: 14px;
}
-->
</style>
</head>

<body>
<table width="620" border="0" cellspacing="3" cellpadding="2">
  <tr>
    <td> <p class="style1">Dear <?php $data1 ; ?>,&nbsp;<br />
        <br />
      Please scroll down to view your invitation to appear in a special restaurant&nbsp;feature in Food & Wine magazine being presented in the July 2012 issue. </p></td>
  </tr>
  <tr>
    <td><div align="center"><br />
        <img src="http://domain.com/mailassets/foodwine_logo.jpg" width="366" height="68" /><br />
    </div></td>
  </tr>
  <tr>
    <td> <p class="style1">The feature is entitled<strong>&nbsp;"The Great Restaurants of America"</strong>, and is a monthly&nbsp;highlight which focuses on a different city each issue.&nbsp;<br />
        <br />
        The July issue will spotlight the Greater Atlanta areas leading restaurants.&nbsp;Restaurants are included strictly on an invitation-only basis. Please take a&nbsp;moment and read the attached few pages which will give you full information&nbsp;on this important feature.&nbsp;<br />
        <br />
        Please feel free to call me to answer any questions you may have. Looking&nbsp;forward to seeing you in Food & Wine magazine. </p>
    <p class="style1">&nbsp;</p></td>
  </tr>
  <tr>
    <td><p class="style1">Many Thanks,</p></td>
  </tr>
  <tr>
    <td><img src="http://domain.com/mailassets/signature.jpg" width="210" height="102" /></td>
  </tr>
  <tr>
    <td><p class="style1">RMY<br />
      305-517- 6620<br />
      <a href="mailto:rmy@yinfo.com" target="_blank">rmy@yinfo.com</a>&nbsp; </p></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td><img src="http://domain.com/mailassets/p1.jpg" width="612" height="792" /></td>
  </tr>
  <tr>
    <td><img src="http://domain.com/mailassets/p2.jpg" width="612" height="792" /></td>
  </tr>
  <tr>
    <td><img src="http://domain.com/mailassets/p3.jpg" width="612" height="792" /></td>
  </tr>
  <tr>
    <td><img src="http://domain.com/mailassets/p4.jpg" width="612" height="792" /></td>
  </tr>
  <tr>
    <td><img src="http://domain.com/mailassets/p5.jpg" width="612" height="792" /></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
  </tr>
</table>
</body>
</html> 
EOF; 
   //end of message 
    $headers  = "From: $from\r\n"; 
    $headers .= "Content-type: text/html\r\n"; 

    //options to send to cc+bcc 
    //$headers .= "Cc: [email]maa@p-i-s.cXom[/email]"; 
    //$headers .= "Bcc: [email]email@maaking.cXom[/email]"; 
     
    // now lets send the email. 
    mail($to, $subject, $message, $headers); 

    echo "Message has been sent....!"; 
?>

I'm really lost and I am sure this is simple, but 5 years of not looking at this stuff made me forget a lot. And no, this isn't spam, this is a list we have been compiling for quite some time.

Thanks for any help!

Re: Need help with lists and sending HTML email in PHP

Posted: Mon Mar 26, 2012 1:23 pm
by Celauran
Could you perhaps explain what the problem is? "it isn't right" is not particularly helpful.

Re: Need help with lists and sending HTML email in PHP

Posted: Mon Mar 26, 2012 2:30 pm
by ecros
The problem is, the csv open fine and spits the information out to the screen, but the emails don't send and I'm not sure it is even getting the data to the message from each line of the csv.

Re: Need help with lists and sending HTML email in PHP

Posted: Mon Mar 26, 2012 2:41 pm
by Celauran
This

Code: Select all

$to = $data0;
looks like it should probably read

Code: Select all

$to = $data[0]; 
If you're not certain the emails are being sent (which is different from their not being received), why not assign the return value of mail() to a variable, which you can then inspect?