I am trying, rather unsuccessfully to send an html email from a table in mysql with an image attachment. This is not working, I get an SMTP 550 error. Please help, I'm sure that my headers are wrong. Here is my code: <?php mysql_connect('localhost','root','xxxxxxxxx') or die(mysql_error()); mysql_select_db('ivite'); $result = mysql_query("SELECT * FROM invitations WHERE reply IS NULL") or die(mysql_error()); while ($row = mysql_fetch_array($result)){ $to = $row['email']; $subject = "Invitation from Harbor Star Shipping Services"; $message = " <html> <head> <title>Harbor Star Shipping Services</title> </head> <body bgcolor=\"#016502\"><div align=\"center\"><img src=\"http://124.104.101.126/harborstar/harborstar.jpg\" width=\"804\" height=\"552\" border=\"0\" /> <br /> <br /> <a style=\"background:url(http://124.104.101.126/harborstar/button.gif) no-repeat; background-position:center;color:#FFFFFF;display:block;font-weight:bold; font-size:18px;height:40px;text-decoration:none;padding-top:10px\" href=\"http://124.104.101.126/harborstar/index.php?firstname=".$row['firstname']."&lastname=".$row['lastname']."&email=".$row['email']."&company=".$row['company']."\">View Invitation</a></div> </body> </html> "; $unique_sep = md5(uniqid(time())); $headers .= "MIME-Version: 1.0\nContent-Type:"." multipart/mixed;boundary=\"$unique_sep\";\n"; $headers .= "charset=\"iso-8859-1\"\nContent-Transfer-Encoding:"."7bit\n\n"; $headers .= "Content-Type: text/html; charset=\"iso-8859-1\"\n"; $headers .= "Content-Transfer-Encoding: 7bit\n\n"; $headers .= "From: david.davis@d-c-g.com\n"; $headers .= "--$unique_sep\n"; $headers .= "Content-Type: {$val['image/jpeg']}; "."name=\"{$val['Roadmap1']}\"\n"; $headers .= "Content-Transfer-Encoding: "."base64\n"; $headers .= "Content-Disposition: attachment\n\n"; $headers .= chunk_split(base64_encode('C:\xampp\htdocs\harborstar\roadmap1.jpg')); mail($to,$subject,$message,$headers); echo "Email Successfully Sent to:" .$to['email']."\r\n"; } ?> Code (markup):
base64_encode('C:\xampp\htdocs\harborstar\roadmap1.jpg') PHP: You have to escape the backslashes with a backslash. Meaning... you have to use 2 backslashes. base64_encode('C:\\xampp\\htdocs\\harborstar\\roadmap1.jpg') PHP: Plus you won't be able to send emails from your local host unless you have your server configured to do so.
Thanks, but that didn't quite work for me. I revised it like this, and it works when only using one result, but now I am getting this error when trying to go through all rows in my database table: Fatal error: Cannot redeclare send_mail() (previously declared in C:\xampp\htdocs\harborstar\mail2.php:6) in C:\xampp\htdocs\harborstar\mail2.php on line 6 Here is my code: <?php mysql_connect('localhost','root','xxxxxxx') or die(mysql_error()); mysql_select_db('ivite'); $result = mysql_query("SELECT * FROM invitations WHERE reply IS NULL") or die(mysql_error()); while ($row = mysql_fetch_array($result)){ function send_mail($emailaddress, $fromaddress, $emailsubject, $body, $attachments=false) { # Common Headers $headers .= 'From: Harbor Star Shipping Services Inc<'."david.davis@d-c-g.com".'>'."\r\n"; $headers .= 'Reply-To: Harbor Star Shipping Services Inc<'."david.davis@d-c-g.com".'>'."\r\n"; $headers .= 'Return-Path: Harbor Star Shipping Services Inc<'."david.davis@d-c-g.com".'>'."\r\n"; // these two to set reply address $headers .= "Message-ID: <".$now." Administration@".$_SERVER['SERVER_NAME'].">"."\r\n"; $headers .= "X-Mailer: PHP v".phpversion()."\r\n"; // These two to help avoid spam-filters # Boundry for marking the split & Multitype Headers $headers .= 'MIME-Version: 1.0'."\r\n"; $headers .= "Content-Type: multipart/mixed; boundary=\"".md5(time())."\""."\r\n"; $msg = ""; # HTML Version $msg .= "--".md5(time())."\r\n"; $msg .= "Content-Type: text/html; charset=iso-8859-1"."\r\n"; $msg .= "Content-Transfer-Encoding: 7bit"."\r\n"; $msg .= " <html> <head> <title>Harbor Star Shipping Services</title> </head> <body bgcolor=\"#016502\"><div align=\"center\"><img src=\"http://124.104.101.126/harborstar/harborstar.jpg\" width=\"804\" height=\"552\" border=\"0\" /> <br /> <br /> <a style=\"background:url(http://124.104.101.126/harborstar/button.gif) no-repeat; background-position:center;color:#FFFFFF;display:block;font-weight:bold; font-size:18px;height:40px;width:225px;text-decoration:none;padding-top:10px\" href=\"http://124.104.101.126/harborstar/index.php?firstname=".$row['firstname']."&lastname=".$row['lastname']."&email=".$row['email']."&company=".$row['company']."\">View Invitation</a></div> </body> </html> "."\r\n"; if ($attachments !== false) { for($i=0; $i < count($attachments); $i++) { if (is_file($attachments[$i]["file"])) { # File for Attachment $file_name = substr($attachments[$i]["file"], (strrpos($attachments[$i]["file"], "/")+1)); $handle=fopen($attachments[$i]["file"], 'rb'); $f_contents=fread($handle, filesize($attachments[$i]["file"])); $f_contents=chunk_split(base64_encode($f_contents)); //Encode The Data For Transition using base64_encode(); fclose($handle); # Attachment $msg .= "--".md5(time())."\r\n"; $msg .= "Content-Type: ".$attachments[$i]["content_type"]."; name=\"".$file_name."\""."\r\n"; $msg .= "Content-Transfer-Encoding: base64"."\r\n"; $msg .= "Content-Disposition: attachment; filename=\"".$file_name."\""."\r\n"."\r\n"; // !! This line needs TWO end of lines !! IMPORTANT !! $msg .= $f_contents."\r\n"."\r\n"; } } } # Finished $msg .= "--".md5(time())."--"."\r\n"."\r\n"; // finish with two eol's for better security. see Injection. # SEND THE EMAIL ini_set(sendmail_from,$fromaddress); // the INI lines are to force the From Address to be used ! mail($emailaddress, $emailsubject, $msg, $headers); ini_restore(sendmail_from); echo 'mail sent to: '.$emailaddress.'<br>'; } # To Email Address $emailaddress=$row['email']; # From Email Address $fromaddress = "david.davis@d-c-g.com"; # Message Subject $emailsubject="Invitation from Harbor Star Shipping Services, Inc."; # Use relative paths to the attachments $attachments = Array( Array("file"=>"roadmap1.jpg", "content_type"=>"image/jpeg"), Array("file"=>"roadmap2.jpg", "content_type"=>"image/jpeg")); send_mail($emailaddress, $fromaddress, $emailsubject, $body, $attachments); } ?> Code (markup): Any suggestions???
I fixed this already. I had to place the functon outside the loop. Now it works. Thanks! If you are curious, here is my code: <?php function send_mail($emailaddress, $fromaddress, $emailsubject, $body, $attachments=false) { # Common Headers $headers .= 'From: Harbor Star Shipping Services Inc<'."david.davis@d-c-g.com".'>'."\r\n"; $headers .= 'Reply-To: Harbor Star Shipping Services Inc<'."david.davis@d-c-g.com".'>'."\r\n"; $headers .= 'Return-Path: Harbor Star Shipping Services Inc<'."david.davis@d-c-g.com".'>'."\r\n"; // these two to set reply address $headers .= "Message-ID: <".$now." Administration@".$_SERVER['SERVER_NAME'].">"."\r\n"; $headers .= "X-Mailer: PHP v".phpversion()."\r\n"; // These two to help avoid spam-filters # Boundry for marking the split & Multitype Headers $headers .= 'MIME-Version: 1.0'."\r\n"; $headers .= "Content-Type: multipart/mixed; boundary=\"".md5(time())."\""."\r\n"; $msg = ""; # HTML Version $msg .= "--".md5(time())."\r\n"; $msg .= "Content-Type: text/html; charset=iso-8859-1"."\r\n"; $msg .= "Content-Transfer-Encoding: 7bit"."\r\n"; $msg .= " <html> <head> <title>Harbor Star Shipping Services</title> </head> <body bgcolor=\"#016502\"><div align=\"center\"><img src=\"http://124.104.101.126/harborstar/harborstar.jpg\" width=\"804\" height=\"552\" border=\"0\" /> <br /> <br /> <a style=\"background:url(http://124.104.101.126/harborstar/button.gif) no-repeat; background-position:center;color:#FFFFFF;display:block;font-weight:bold; font-size:18px;height:40px;width:225px;text-decoration:none;padding-top:10px\" href=\"http://124.104.101.126/harborstar/index.php?firstname=".$row['firstname']."&lastname=".$row['lastname']."&email=".$row['email']."&company=".$row['company']."\">View Invitation</a></div> </body> </html> "."\r\n"; if ($attachments !== false) { for($i=0; $i < count($attachments); $i++) { if (is_file($attachments[$i]["file"])) { # File for Attachment $file_name = substr($attachments[$i]["file"], (strrpos($attachments[$i]["file"], "/")+1)); $handle=fopen($attachments[$i]["file"], 'rb'); $f_contents=fread($handle, filesize($attachments[$i]["file"])); $f_contents=chunk_split(base64_encode($f_contents)); //Encode The Data For Transition using base64_encode(); fclose($handle); # Attachment $msg .= "--".md5(time())."\r\n"; $msg .= "Content-Type: ".$attachments[$i]["content_type"]."; name=\"".$file_name."\""."\r\n"; $msg .= "Content-Transfer-Encoding: base64"."\r\n"; $msg .= "Content-Disposition: attachment; filename=\"".$file_name."\""."\r\n"."\r\n"; // !! This line needs TWO end of lines !! IMPORTANT !! $msg .= $f_contents."\r\n"."\r\n"; } } } # Finished $msg .= "--".md5(time())."--"."\r\n"."\r\n"; // finish with two eol's for better security. see Injection. # SEND THE EMAIL ini_set(sendmail_from,$fromaddress); // the INI lines are to force the From Address to be used ! mail($emailaddress, $emailsubject, $msg, $headers); ini_restore(sendmail_from); echo 'mail sent to: '.$emailaddress.'<br>'; } mysql_connect('localhost','root','xxxxxxxx') or die(mysql_error()); mysql_select_db('ivite'); $result = mysql_query("SELECT * FROM invitations WHERE reply IS NULL") or die(mysql_error()); while ($row = mysql_fetch_array($result)) { # To Email Address $emailaddress=$row['email']; # From Email Address $fromaddress = "david.davis@d-c-g.com"; # Message Subject $emailsubject="Invitation from Harbor Star Shipping Services, Inc."; # Use relative paths to the attachments $attachments = Array( Array("file"=>"roadmap1.jpg", "content_type"=>"image/jpeg"), Array("file"=>"roadmap2.jpg", "content_type"=>"image/jpeg")); send_mail($emailaddress, $fromaddress, $emailsubject, $body, $attachments); } ?> Code (markup):