PHP - Send Inline Images And Attachments With Email Via Php
I have a script that generates an excel spreadsheet, and attaches it to an email ready for mailing. Now, I can make it email the attachment no problem.
I have searched for solutions on inserting 3 images in the email as well, but to no avail. I found a script that I thought could work, but it just sends the script via email and nothing else. Does anybody know of a good way to do this? Perhaps someone can help me with this script? <?php include "get_contact_list.php"; ################################################################################ ########### ### An email script that will attach images inline in an HTML and plain text e-mail ### ### Just enter you own email infomation and file names with paths where indicated in ### ### the script. Have fun with it alter it add to it or whatever you want. When you are ### ### done reading all the confusing tutorials about this kind of using mail to send your ### ### own without PHPmailer then you can just use this file to suit your self. ### ################################################################################ ########### $to = 'xxxxxxx@xxxxx.com';// same as above $subject = 'CONTACT & SUPERVISORS LIST_'.$date;//your own stuff goes here // Create a boundary string. It needs to be unique $random_hash = sha1(date('r', time())); // Add in our content boundary, and mime type specification: $headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; // Read in our file attachment $attachment = file_get_contents($path); $encoded = base64_encode($attachment); $attached = chunk_split($encoded); // additional headers $headers = "From: Nxxxxxx Cxxxxxx <xxxxxxxx@xxxxx.com>" . "\r\n" . 'Reply-To: xxxxxxxx@xxxxx.com' . "\r\n";//put you own stuff here or use a variable $inline = chunk_split(base64_encode(file_get_contents('../../images/emails/vcelogo.jpg'))); // Your message he ob_start();?> --PHP-mixed-<?php echo $random_hash; ?>-- Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>--" --PHP-alt-<?php echo $random_hash; ?>-- Content-Type: text/plain Hai, Its me! --PHP-alt-<?php echo $random_hash; ?>-- Content-Type: multipart/related; boundary="PHP-related-<?php echo $random_hash; ?>--" --PHP-alt-<?php echo $random_hash; ?>-- Content-Type: text/html <html> <head> <title>Test HTML Mail</title> </head> <body> <font color='red'>Hai, it is me!</font> Here is my pictu <img src="cid:PHP-CID-<?php echo $random_hash; ?>" /> </body> </html> --PHP-related-<?php echo $random_hash; ?>-- Content-Type: image/jpeg; Content-Transfer-Encoding: base64 Content-ID: <PHP-CID-<?php echo $random_hash; ?>--> <?php echo $inline; ?> --PHP-related-<?php echo $random_hash; ?>-- --PHP-alt-<?php echo $random_hash; ?>-- --PHP-mixed-<?php echo $random_hash; ?>-- Content-Type: application/vnd.ms-excel; name='CONTACT & SUPERVISORS LIST_'.$date.'.xls'; Content-Transfer-Encoding: base64 Content-Disposition: attachment <?php echo $attachment; ?> --PHP-mixed-<?php echo $random_hash; ?>-- <?php $body = ob_get_clean(); // Finally, send the email $send = mail($to, $subject, $body, $headers); if ($send) { unlink($path); header("Location: hr_options.php?message=Sent successfully"); } else { header("Location: hr_options.php?message=Failed Sending"); } ?> Similar TutorialsBasically I have successfully found a function that can send email with attachment. However wasn't able to send multiple attachments. I tried sending two attachments manually, but only the second was successfully attached. Can someone look through the code below and point out my mistake. Thank you in advance.... <?php if(isset($_POST['send'])) { $fileatt_one = $_FILES['attachment']['tmp_name'][0]; $fileatt_type_one = "application/octet-stream"; // File Type $fileatt_name_one = $_FILES['attachment']['name'][0]; // Filename that will be used for the file as the attachment $fileatt_two = $_FILES['attachment']['tmp_name'][1]; $fileatt_type_two = "application/octet-stream"; // File Type $fileatt_name_two = $_FILES['attachment']['name'][1]; // Filename that will be used for the file as the attachment $email_from = "a@gmail.com"; // Who the email is from $email_subject = "Test"; // The Subject of the email $email_txt = "Testing 123"; // Message that the email has in it $email_to = "a@gmail.com"; // Who the email is too $headers = "From: ".$email_from; $file_one = fopen($fileatt_one,'rb'); $data_one = fread($file_one,filesize($fileatt_one)); fclose($file_one); $file_two = fopen($fileatt_two,'rb'); $data_two = fread($file_two,filesize($fileatt_two)); fclose($file_two); $semi_rand = md5(time()); $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; $data_one = chunk_split(base64_encode($data_one)); $data_two = chunk_split(base64_encode($data_two)); $headers .= "\nMIME-Version: 1.0\n" ."Content-Type: multipart/mixed;\n" ." boundary=\"{$mime_boundary}\""; $email_message .= "This is a multi-part message in MIME format.\n\n" ."--{$mime_boundary}\n" ."Content-Type:text/html; charset=\"iso-8859-1\"\n" ."Content-Transfer-Encoding: 7bit\n\n" .$email_txt . "\n\n"; $email_message .= "--{$mime_boundary}\n" ."Content-Type: {$fileatt_type_one};\n" ." name=\"{$fileatt_name_one}\"\n" ."Content-Transfer-Encoding: base64\n\n" .$data_one . "\n\n" ."--{$mime_boundary}--\n"; $email_message .= "--{$mime_boundary}\n" ."Content-Type: {$fileatt_type_two};\n" ." name=\"{$fileatt_name_two}\"\n" ."Content-Transfer-Encoding: base64\n\n" .$data_two . "\n\n" ."--{$mime_boundary}--\n"; $ok = @mail($email_to, $email_subject, $email_message, $headers); if($ok) { echo "<font face=verdana size=2>The file was successfully sent!</font>"; } else { die("Sorry but the email could not be sent. Please go back and try again!"); } } ?> <form enctype="multipart/form-data" name="send" method="post" action="<?=$_SERVER['PHP_SELF']?>"> <input type="file" name="attachment[]" size="50" /> <input type="file" name="attachment[]" size="50" /> <input type="submit" name="send" value="Submit"> </form> Hello, i have this code for sending email it works great but i don't know where to start in sending emails with a pdf file attached ex: test.pdf. Any suggestions? <?php session_start(); require_once('framework/framework.php'); include('framework/class.smtp.inc'); //--- databse settings $dsn = array( 'dbtype' => 'mysql', 'username' => '', 'password' => '', 'host' => '', 'database' => '' ); try { $db = db::connect($dsn); } catch (Exception $e) { die($e->getMessage()); } //--- $subject="Formular online pentru participantii Diaspora Stiintifica 2010"; // +-------------- BEGIN ---- Functia de trimitere mail prin smtp -------------------+ */ function trimite($destinatar='', $subiect='', $mesaj='') { //--- mail settings $smtp_server_host="mail.server.com"; $smtp_server_port="25"; $sender="email@email.com"; $return_path="email@email.com"; $smtp_username="email@email.com"; $smtp_pswd="123"; /*************************************** ** Setup some parameters which will be ** passed to the smtp::connect() call. ***************************************/ $params['host'] = $smtp_server_host; // The smtp server host/ip $params['port'] = $smtp_server_port; // The smtp server port $params['helo'] = exec('hostname'); // What to use when sending the helo command. Typically, your domain/hostname $params['auth'] = false; // Whether to use basic authentication or not // $params['user'] = $smtp_username; // Username for authentication // $params['pass'] = $smtp_pswd; // Password for authentication $params['timeout'] = '60'; /*************************************** ** These parameters get passed to the ** smtp->send() call. ***************************************/ $send_params['recipients'] = $destinatar; // The recipients (can be multiple) $send_params['headers'] = array( "MIME-Version: 1.0", "X-Mailer: PHP/" . phpversion(), "Return-Path: ".$return_path, "From: ".$sender, "To: ".$destinatar, "Subject: ".$subiect, "Content-type: text/html; charset=UTF-8", "Content-Transfer-Encoding: 8bit"); $send_params['from'] = $sender; // This is used as in the MAIL FROM: cmd // It should end up as the Return-Path: header $send_params['body'] = $mesaj; /*************************************** ** The code that creates the object and ** sends the email. ***************************************/ $smtp = new smtp($params); $smtp->connect(); $trimis=$smtp->send($send_params); $raspuns=array($destinatar, $trimis, $smtp->errors); // $smtp->rset(); //--- Bcc: /* $send_params['recipients'] = $sender; $trimisi=$smtp->send($send_params); $errmsgi=$smtp->errors; print "<br>bcc: ".$send_params['recipients']."; ".$trimisi."<br>"; for ($n = 0 ; $n <= count($errmsgi) - 1; $n++) { print "<br>"." bccerr: <strong>".$errmsgi[$n]."</strong><br>"; } */ $smtp->quit(); return $raspuns; } // +-------------- END ---- Functia de trimitere mail prin smtp -------------------+ */ // +---------------------------------------------------------------------------+ $afisez = ""; $cond=0; $query = "SELECT email_1 FROM inregistrari WHERE `completat`=0"; $useri = $db->getAll($query); $i=0; $j=0; $k=0; foreach ($useri as $row) { $tpl = new HTML_Template_Sigma('mesaj'); $tpl->loadTemplateFile("mesaj.html"); $tpl->setVariable($row); $html=$tpl->get(); //*/ $html=nl2br($html); $html=str_replace(chr(10), "", $html); $html=str_replace(chr(13), "", $html); // */ // +----------------------+ $subiect = $subject; if (!empty($row['email_1'])) { $subelements=preg_split("/, /", $row['email_1'], -1, PREG_SPLIT_NO_EMPTY); for ($m = 0 ; $m <= count($subelements) - 1; $m++) { $rez = trimite($subelements[$m], $subiect, $html); if ($rez[1]==true) { $afisez .= $rez[0].";1<br>"; $i++; } else { $afisez .= $rez[0].";0<br>"; $errmsg=$rez[2]; for ($n = 0 ; $n <= count($errmsg) - 1; $n++) { $afisez .= " <strong>".$errmsg[$n]."</strong><br>"; } $j++; } } } else { $afisez .= $row['email_1'].";no email;n<br>"; $k++; } } $afisez .= "<br>{$i} mesaje Trimise<br>"; $afisez .= "{$j} mesaje Netrimise<br>"; $afisez .= "{$k} mesaje fara adresa<br>"; print $afisez; ?> Hi all, Does anyone know how to extract attachments from an email that has more than one attachment associated with it? Getting the overall attachment data works fine using Code: [Select] <?php $attachment_data = imap_fetchbody($inbox,$email_number,2); ?> ..but breaking up that data into its component attachments is eluding me, and I can't find anything on the PHP doco that enabled me to do it. All info appreciated, WoolyG hi there. this is my first post. i'm sory for my english (i'm portuguese) i have i very bad situation. on my code. i'm sending an email from my aplication and when arrives to the client the Attachments is send with the extension ".dat". in my pc the email is sent correct. but from other pc (server) the extension arrives ".dat". does anyone have the same problem ??? please help me. tnks Hi all I'm trying to send an automated email of a zip file. However it seems to be just reading the files into the body of the mail rather than attaching a single zip archive containing several files. I receive the mails, but I effectively want a blank message body with a zip file attached. Please see code below, any ideas why this is not working. I'm a bit of a newbie and never used this before so any pointers would be good. <?php //Send Zip file as attachment //Set Email and attachment details $to = "me@123.com"; $from = "Visionnet <DO_NOT_REPLY>"; $subject = "Subject"; $message = "Please see attached file"; $fileatt = "wiki_zip.zip"; $fileatt_type = "application/zip"; $fileatt_name = "wiki_zip.zip"; //Read in the attachment $file=fopen($fileatt,'rb'); $data=fread($file,filesize( $fileatt)); fclose($file); //Add the MIME content $semi_rand = md5(time()); $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; // Add the Headers for file attachment $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; $message = "This is a multipart message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; //Base64 encode file data $data = chunk_split(base64_encode($data)); //Add attachment to message $message .= "--{mime_boundary}\n" . "Content-Type: {$fileatt_type};\n" . " name=\"{$fileatt_name}\"\n" . "Content-Disposition: attachment;\n" . " filename=\"{$fileatt_name}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" . "--{$mime_boundary}--\n"; //Send message $ok = @mail($to, $subject, $message, $headers, "$from"); if($ok) { echo "mail sent"; } else { echo "mail failed"; } ?> Hi, I have done this script that sends emails without attachments. What I want to do now is add the functionality to attach multiple files using one single field, kind of what Outlook does...you can select one or more files to attach to your email. Any ideas how I can do this? Thanks Code: [Select] <?php $EmailTo = Trim(stripslashes($_POST['to'])); $Subject = Trim(stripslashes($_POST['subject'])); $email = Trim(stripslashes($_POST['email'])); // 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 .= 'From: My company<info@mycompany.com>' . "\r\n"; $headers .= "Cc: {$_POST['cc']}\r\n"; $headers .= "Bcc: {$_POST['bcc']}\r\n"; // prepare email body text $Body = ""; $Body .= $email; $Body .= "\n"; $Body .= "\n"; // send email $success = mail($EmailTo, $Subject, stripslashes($Body), $headers); // redirect to success page if ($success){ print "<meta http-equiv=\"refresh\" content=\"0;URL=../PC_email_compose.php?emailsentok=true\">"; } else{ print "<meta http-equiv=\"refresh\" content=\"0;URL=../PC_email_compose.php?emailsenterror=true\">"; } ?> I am piping email to a php script, from the email I want to get the email attachment that came with it. For our sake, the curent attachment I am working with is jpg. how can I get an image that is base64 encoded, then save it to a jpg file? "Content-Transfer-Encoding: base64" Hello I have a database table containing users uploaded word docs as long blobs (I know in an ideal world these should be in a filesystem, not a table but that wasn't possible) I'd like to be able to generate an email with php and send out these files as attachments. I have pear mail installed and can get this to send out emails with attachments form a file share no problems. Getting it to work with blobs escapes me though. Is this possibile of do I need a different class? Code: [Select] include('../db_connection.php'); $query1 = "SELECT id, name, type, content, size FROM upload WHERE lref='" . $_POST['lref'] . "'"; $result2 = $mysqlilsr->query($query1) or die ($mysqlilsr->error); list($id, $name, $type, $content, $size) = $result2->fetch_array(); //echo $name . '<br/>'; //echo $content . '<br/>'; //echo $type . '<br/>'; //$fileattname = file_get_contents($_POST['attachment']); $fileattname = file_get_contents($_POST['attachment']); $from_mail = "email address"; $replyto = "email address"; //phpinfo(); //mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message); $headers = array( 'From' => $from_mail, 'Subject' => $subject, 'Cc' => $replyto, ); $body = $header . $spacer .$message . $spacer . $footer; $mime = new Mail_mime(); $mime->setTXTBody($body); $mime->addAttachment(base64_encode($content), $name, $type, "iso-8859-1"); $body = $mime->get(); $headers = $mime->headers($headers); $mailer =& Mail::factory('mail'); $res = $mailer->send($_POST['email'], $headers, $body); if (PEAR::isError($res)) { echo 'error'; } else { 'success'; } Hey All, I am having issues understanding how this works. I am trying to do a quote and send out 5 images of the product. I cannot seem to get how I get the images sent with the email. If anyone can take a look I would greatly appreciate it. PHP <?php $email = $_POST ['email']; $quote = $_POST ['quote']; $item = $_POST ['item']; $inventory = $_POST ['inventory']; $category = $_POST ['category']; $manu = $_POST ['manu']; $model = $_POST ['model']; $condition = $_POST ['condition']; $dimension = $_POST ['dimension']; $desc = $_POST ['desc']; $skidprice = $_POST ['skidprice']; $unitprice = $_POST ['unitprice']; $totalprice = $_POST ['totalprice']; $img1 = $_POST ['img1']; $img2 = $_POST ['img2']; $img3 = $_POST ['img3']; $img4 = $_POST ['img4']; $img5 = $_POST ['img5']; //Get the uploaded file information $name_of_uploaded_file = basename($_FILES['uploaded_file']['name']); //get the file extension of the file $type_of_uploaded_file = substr($name_of_uploaded_file, strrpos($name_of_uploaded_file, '.') + 1); $size_of_uploaded_file = $_FILES["uploaded_file"]["size"]/1024;//size in KBs //Validations if($size_of_uploaded_file > $max_allowed_file_size ) { $errors .= "\n Size of file should be less than $max_allowed_file_size"; } //------ Validate the file extension ----- $allowed_ext = false; for($i=0; $i<sizeof($allowed_extensions); $i++) { if(strcasecmp($allowed_extensions[$i],$type_of_uploaded_file) == 0) { $allowed_ext = true; } } if(!$allowed_ext) { $errors .= "\n The uploaded file is not supported file type. ". " Only the following file types are supported: ".implode(',',$allowed_extensions); } //copy the temp. uploaded file to uploads folder $path_of_uploaded_file = $upload_folder . $name_of_uploaded_file; $tmp_path = $_FILES["uploaded_file"]["tmp_name"]; if(is_uploaded_file($tmp_path)) { if(!copy($tmp_path,$path_of_uploaded_file)) { $errors .= '\n error while copying the uploaded file'; } } // Always set content-type when sending HTML email $headers = "MIME-Version: 1.0" . "\r\n"; $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n"; // More headers $headers .= 'From: All Food Equipment<sales@allfoodequip.com>' . "\r\n"; $headers .= "Message-ID: <".$now." sales@".$_SERVER['SERVER_NAME'].">\r\n"; $headers .= "X-Mailer: PHP v".phpversion()."\r\n"; $subject = "Your All Food Equip Quote"; $msg = " <!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\"> <html> <head><link href=\"http://www.allfoodequip.com/admin/quote/style.css\" rel=\"stylesheet\" type=\"text/css\"></head> <body> <div id=\"container\" style=\"width:960px; height:auto; margin:0 auto; border-right: 2px solid #666; border-left: 2px solid #666;\"> <div id=\"header\" style=\"background-image:url(http://www.allfoodequip.com/admin/quote/images/banner_bg.jpg); width:100%; height:149px;\"></div> <!--Closes header--> <div id=\"subHead\" style=\"background-image:url(http://www.allfoodequip.com/admin/quote/images/menu_bg.jpg); width:100%; height:42px;\"></div> <!--closes subHead--> <div class=\"contents\" style=\"background:#f2f1f1; padding-right: 25px; padding-left: 25px;\"> <h1 style=\"margin:0px;padding:10px 0 5px;font-size:18px;\">Your All Food Equipment Quote:</h1> <table width=\"700\" border=\"0\" class=\"table\" style=\"padding:0px 0px 15px;\"> <tr> <td width=\"119\">Quote #:......................................................... </td> <td width=\"208\">$quote</td> </tr> <tr> <td>Item:..............................................................</td> <td>$item</td> </tr> <tr> <td>Inventory:.......................................................</td> <td>$inventory</td> </tr> <tr> <td>Category:.......................................................</td> <td>$category</td> </tr> <tr> <td>Manufacturer:.................................................</td> <td>$manu</td> </tr> <tr> <td>Model:...........................................................</td> <td>$model</td> </tr> <tr> <td>Condition:......................................................</td> <td>$condition</td> </tr> <tr> <td>Dimension:....................................................</td> <td>$dimension</td> </tr> <tr> <td>Description:...................................................</td> <td>$desc</td> </tr> <br> <tr> <td><b>Skid Price:</b>.................................................</td> <td><b>$skidprice</b></td> </tr> <tr> <td><b>Unit Price:</b>.................................................</td> <td><b>$unitprice</b></td> </tr> <tr> <td><b>Total Price:</b>................................................</td> <td><b>$totalprice</b></td> </tr> </table> <div class=\"t_middle\"> <img name=\"Img\" src=\"$img1\" width=\"177\" height=\"115\" alt=\"Img\" style=\"background-color: #999999\"><img name=\"Img\" src=\"$img2\" width=\"177\" height=\"115\" alt=\"Img\" style=\"background-color: #999999\"><img name=\"Img\" src=\"$img3\" width=\"177\" height=\"115\" alt=\"Img\" style=\"background-color: #999999\"><img name=\"Img\" src=\"$img4\" width=\"177\" height=\"115\" alt=\"Img\" style=\"background-color: #999999\"><img name=\"Img\" src=\"$img5\" width=\"177\" height=\"115\" alt=\"Img\" style=\"background-color: #999999\"> </div> <div class=\"break\" style=\"padding: 15px 0px; width:100%;\"><hr></div> <!--break--> <!--close t_middle--> <div class=\"contactUs\"> <p style=\"color:#650035;\">How to contact us:</p> <ul class=\"contact_list\" style=\"margin:0px;padding:0px;color:#650035;line-height:25px;padding-bottom:30px;list-style:none;\"> <li>Visit us Online: www.allfoodequip.com</li> <li>Contact: Dino Roberts</li> <li>Direct Line: 615-788-2953 </li> <li>Email: dino@allfoodequip.com</li> </ul> <ul class=\"contact_list\" style=\"margin:0px;padding:0px;color:#650035;line-height:25px;padding-bottom:30px;list-style:none;\"> <li>All Food Equipment</li> <li>1240 Industrial Park Road</li> <li>Columbia, TN 38401</li> <li>Main Office: 931-490-1977</li> </ul> </div> <p style=\"font-size:14px;color:#333;\">TERMS & CONDITIONS: </p> </div> <!--close m_disc--> <div id=\"footer\" style=\"clear: both; width: 100%; padding: 20px 0 20px 0; text-align: center; border-top: 1px solid #8b8a8b; background: #343233 url(http://www.allfoodequip.com/admin/quote/images/footer.jpg) no-repeat;\"> <ul class=\"footer_list\" style=\"margin:0px;padding:0px;list-style:none;\"> <li style=\"padding: 0 10px; display: inline; border-right: 1px solid #cccccc;\"><a href=\"http://www.allfoodequip.com\" style=\"color: #cccccc; text-decoration: none;\">Home</a></li> <li style=\"padding: 0 10px; display: inline; border-right: 1px solid #cccccc;\"><a href=\"http://www.allfoodequip.com/new-equipment.php\" style=\"color: #cccccc; text-decoration: none;\">New Equipment</a></li> <li style=\"padding: 0 10px; display: inline; border-right: 1px solid #cccccc;\"><a href=\"http://www.allfoodequip.com/used-equipment.php\" style=\"color: #cccccc; text-decoration: none;\">Used Equipment</a></li> <li style=\"padding: 0 10px; display: inline; border-right: 1px solid #cccccc;\"><a href=\"http://www.allfoodequip.com/services.php\" style=\"color: #cccccc; text-decoration: none;\">Services</a></li> <li style=\"padding: 0 10px; display: inline; border-right: 1px solid #cccccc;\"><a href=\"http://www.allfoodequip.com/about-us.php\" style=\"color: #cccccc; text-decoration: none;\">About Us</a></li> <li class=\"last\" style=\"padding:0 10px;display:inline;border-right:none;\"><a href=\"http://www.allfoodequip.com/contact-us.php\" style=\"color: #cccccc; text-decoration: none;\">Contact Us</a></li> </ul> <div class=\"margin_bottom_10\"></div><br> <span style=\"color:#FFF;\">Copyright © 2010 </span> - <a href=\"http://www.allfoodequip.com\"><font color=\"#FFFFCC\">All Food Equipment</font></a> <div class=\"margin_bottom_10\"></div> <!-- end of footer --> </div> <!--container--> </div> </body> </html> "; // MAIL SUBJECT $subject = "Your All Food Equip Quote"; // TO MAIL ADDRESS $to="$email"; mail($to, $subject, $msg, $headers); // Redirect header("Location: Admin.php?Msg=Sent"); ?> Heres the HTML side of it. Code: [Select] <label for="img1">Image 1:</label> <input type="hidden" name="MAX_FILE_SIZE" value="2500000"> <input type="file" name="img1"> <label for="img2">Image 2:</label> <input type="hidden" name="MAX_FILE_SIZE" value="2500000"> <input type="file" name="img2"> <label for="img3">Image 3:</label> <input type="hidden" name="MAX_FILE_SIZE" value="2500000"> <input type="file" name="img3"> <label for="img4">Image 4:</label> <input type="hidden" name="MAX_FILE_SIZE" value="2500000"> <input type="file" name="img4"> <label for="img5">Image 5:</label> <input type="hidden" name="MAX_FILE_SIZE" value="2500000"> <input type="file" name="img5"><br /> Thanks in advance. Just as the title says. In cPanel, I have it al configured just fine; the script recieves the email, and I do receieve an email back, but here's where I come into problems. It simply will NOT save the attachments to the server, I've tried loads of different implementations I've found, and nothing's working. pipe script: #!/usr/local/bin/php <?php // Need PEAR installed include('Mail.php'); include('Mail/mime.php'); require_once 'Mail/mimeDecode.php'; // read email using stdin $fx = fopen("php://stdin", "r"); $email = ""; while (!feof($fx)) { $email .= fread($fx,1024); } $params['include_bodies'] = true; $params['decode_bodies'] = true; $params['decode_headers'] = true; $params['input'] = $email; /* $message=new Mail_mimeDecode(); $mailObj=$message->decode($params); */ $mailObj = Mail_mimeDecode::decode($params); // Who is it from $from=$mailObj->headers['from']; // Get Subject $subj=$mailObj->headers['subject']; // Get Message Body $body=$mailObj->parts[0]->body; $gather="From:$from\nSubject:$subj\nBody:$body"; // Get and Save the Attachments foreach($mailObj->parts as $pp){ if ($pp->disposition=='attachment'){ $tmp=$pp->d_parameters['filename']; if($tmp !== ""){ $open = fopen($tmp, 'w'); if($open){ $write = fwrite($open,$pp->body); if(!$write){ $err = "Cannot write to file: '".$tmp."' - var: ".$write; } }else{ $err = "Cannot create/open file: '".$tmp."' - var: ".$open; } } } } $tox = $from; $subjectx = 'Your Photo was Uploaded!'; $messagex = $err; $headersx = 'From: piculo.us Uploader <upload@piculo.us>' . "\r\n" . 'Reply-To: upload@piculo.us' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); mail($tox, $subjectx, $messagex, $headersx); exit; ?> Here's the email I get back: Cannot create/open file: '2879_1150712806251_1180540351_425140_7940412_n.jpg' - var: as you can see, it is aware that an attachment exists, but it won't create the file, and fopen() doesn't say anything. Any ideas?? This topic has been moved to Miscellaneous. http://www.phpfreaks.com/forums/index.php?topic=348632.0 Hello all, I used simple php email function but it send an email in junk folder or spam. Can anyone tell me why is this so? Her is the code: $host = "ssl://smtp.gmail.com"; $port = "465"; $to = " xx@gmail.com"; // note the comma $subject = " $_POST[company_website] "; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= 'From: About Wholesale Account' . "<$_POST[email]>\r\n"; $headers .= 'Cc: mail@gmail.com' . "\r\n"; $headers .= 'Bcc: cc@gmail.com' . "\r\n"; mail($to, $subject, $message, $headers); Hello Guys,
I need your help as many PHPers here is more experienced in PHP coding than me. I have specific project I am working on and need a piece of code that can send an email from HTML form using PHP to the email address that is entered manually on the form, instead of standard sent to PHP code that is fixed within PHP script and executed during submission. I want sth that can grab manually enetered recipient's e-mail address, paste it to the PHP code and then use it to send the email to the recipient, instead of fixed sent to code. Something would say dynamically changed during the entry that can inject into PHP code the new address email entered on the form and then submit to it. Any ideas will be great.
Thanks.
Edited March 27, 2019 by slawotrend
The code below currently sends a separate Email with data from each row of a table when a Save All button is pressed. So, if I have 4 rows of data, 4 Email will be sent. $email="to@email.com"; $from="from@email.com"; $msg=""; $subject="Registers Info for: ".$values["first_name"]." ".$values["last_name"].""; $msg.= "Student: ".$values["first_name"]." ".$values["last_name"]."\r\n"; $msg.= "Absent or Present: ".$values["attendance_status"]."\r\n"; $msg.= "Class: ".$values["classname"]."\r\n"; $msg.= "Class Date: ".$values["attendance_date"]."\r\n"; $msg.= "Amount Received: ".$values["cash_received"]."\r\n"; $msg.= "Paid For: ".$values["cash_whatfor"]."\r\n"; $ret=runner_mail(array('to' => $email, 'subject' => $subject, 'body' => $msg, 'from'=>$from)); if(!$ret["Sent"]) echo $ret["message"];
Edited February 27, 2020 by leemo Hello, i have been looking for and answer on the internet but i couldn't find it! I'm new on PHP based on and example i wrote something like this for my website: //basic variables $EmailFrom = Trim(stripslashes($_POST['EmailFrom'])); $EmailTo = "example@mysite.org"; $Subject = "Registration u10"; $teamname = Trim(stripslashes($_POST['teamname'])); $affiliation = Trim(stripslashes($_POST['affiliation'])); $premier = Trim(stripslashes($_POST['premier'])); $challenge = Trim(stripslashes($_POST['challenge'])); $girls = Trim(stripslashes($_POST['girls'])); $boys = Trim(stripslashes($_POST['boys'])); //about 15 more variables go in here but i don't want to put them to save some space // validation $validationOK=true; if (Trim($EmailFrom)=="") $validationOK=false; if (!$validationOK) { print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">"; exit; } // email body text //this includes the info of all the variables at the top even the ones I didn't write in here (but you get the point ) $Body = ""; $Body .= "Team Name: "; $Body .= $teamname; $Body .= "\n"; $Body .= "\n"; $Body .= "AYSleague: "; $Body .= $aysleague; $Body .= "\n"; $Body .= "\n"; $Body .= "Flash Tournament: "; $Body .= $flashtournament; $Body .= "\n"; $Body .= "\n"; $Body .= "Spring 2011: "; $Body .= $spring2011; $Body .= "\n"; $Body .= "\n"; $Body .= "Summer 2011: "; $Body .= $summer2011; $Body .= "\n"; $Body .= "\n"; $Body .= "fall 2011: "; $Body .= $fall2011; $Body .= "\n"; $Body .= "\n"; // send email to my inbox with the information above $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>"); // if everything goes OK it will redirect them to another page if ($success){ print "<meta http-equiv=\"refresh\" content=\"0;URL=thanksu12.html\">"; } else{ print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">"; } ?> the problem that i have is: (i divided it in 2 sections) 1- i need to sent a second email to the coach 2- that email only needs to have the information of 6 variables not the whole thing I don't know how to do it, if someone knows how to do it, i will appreciate some help!! thanks! Hi I have a form that I want people to fill out and then click the button and it sends me an email. My problem is that it doesn't send me any emails, I have looked around and I don't seem to find any thing. This is my form in application_form.php Code: [Select] <form action="application_form" method="POST"> <table border="0" cellpadding="5" cellspacing="0"> <tr> <td>Full name</td> <td><input type="text" size="30" maxlength="80" name="name" /></td> </tr> <tr> <td>Email address</td> <td><input type="text" size="30" maxlength="80" name="email" /></td> </tr> <tr> <td>Message</td> <td><textarea name="message" cols="29" rows="5"></textarea></td> </tr> <tr> <td> </td> <td><input type="submit" name="submit" value="Submit Message" /></td> </tr> </table> </form> and this is the php on the same application_form.php Code: [Select] <?php if (isset($_POST['submit'])) { // Collect variables $name = $_POST['name']; $email = $_POST['email']; $message = $_POST['message']; // Send email $body = "The following feedback has been submitted:\n\n"; $body .= "Name: $name\n"; $body .= "Email: $email\n"; $body .= "Message: $message"; mail ("cerberus478@gmail.com","Feedback Received",$body,"From: cerberus478@gmail.com"); // Output confirmation echo "<p>Your message has been received.</p>"; } ?> Hi guys, i have a custom made php that collect all the info in admin page (long story short), there's a button called "Approve" I would like to send back an email to user who applied with picture, once Approved button is pressed, so here's the script: <?php session_start(); $user = '123'; $pass = '123'; $to = $data['email']; $subject = 'blablabla'; $message = 'test123'; if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']) || $_SERVER['PHP_AUTH_USER'] != $user || $_SERVER['PHP_AUTH_PW'] != $pass) { header('WWW-Authenticate: Basic realm="Enter password to access this page."'); header('HTTP/1.0 401 Unauthorized'); echo"You must be logged in to see this page."; exit; } ################## PROTECTED CONTENT ######################## require('./config.php'); ?> <!DOCTYPE HTML> <html> <head> <title>ADMIN PANEL</title> <style type="text/css"> body{width: 960px; margin: auto; margin-top: 20px; padding: 5px 20px; box-shadow: 0px 0px 2px #000000; border-radius: 5px;background:transparent url(images/bg_header.gif) top left repeat-x;} a:link,a:active,a:visited{color: inherit; text-decoration: none;} a:hover{text-decoration: underline;} .button{background:transparent url(images/grad.PNG) bottom left repeat-x; padding: 3px 5px; border-radius: 4px; border: 1px solid #cccccc;} .button:hover{border-color:orangered;text-decoration:none;} .approve .img{max-height: 200px; max-width: 200px; position: absolute; right: 5px; top: 5px;} .approve{display: block; height: 200px; overflow: hidden; box-shadow: 0px 0px 2px #000000; margin-top: 20px; padding: 20px; position: relative; font:13px verdana;} textarea,input{padding: 3px;font: 13px helvetica,verdana;border:1px solid rgb(51,151,251);border-radius: 4px;width: 300px; display: block; margin: 5px 0px;} input:hover,textarea:hover{border-color:orangered;cursor:pointer;} form{font:bold 13px verdana;} input[type='submit'],input[type='button']{width: auto;} </style> </head> <body> <?php echo" <div id='head'> <h1 align='center'>ADMIN PANEL</h1> <h4 align='center'><a href='index.php' class='button'>Homepage</a> <a class='button' href='admin.php'>Approval Queue</a> <a class='button' href='admin.php?edit'>Edit Image info.</a> <a class='button' href='?edit&view'>View Image Info.</a></h4> </div>"; if (isset($_GET['edit'])) { if (isset($_GET['pid'])) { $pic = mysql_query('SELECT * from `' . $table_name . '` WHERE pid = ' . intval($_GET['pid'])); if (mysql_num_rows($pic) == 1) { if (isset($_POST['submit'])) { $approved = isset($_POST['approved']) && $_POST['approved'] == '1' ? 1 : 0; mysql_query('UPDATE `' . $table_name . '` SET ' . 'first_name = "' . $_POST['first'] . '",' . 'last_name="' . $_POST['last'] . '",' . 'phone="' . $_POST['phone'] . '",' . 'email="' . $_POST['email'] . '",' . 'first_name="' . $_POST['first'] . '",' . 'approved=' . $approved . ',' . 'address="' . $_POST['addr'] . '",' . 'admin_desc="' . $_POST['admin_desc'] . '"' . ' WHERE pid = ' . intval($_GET['pid'])) or exit("Unable to edit" . mysql_error()); echo'<h3>EDITED</h3>'; $pic = mysql_query('SELECT * from `' . $table_name . '` WHERE pid = ' . intval($_GET['pid'])); } $data = mysql_fetch_array($pic); if (isset($_GET['view'])) { echo' <strong>Uploaded by </strong>' . $data['first_name'] . ' ' . $data['last_name'] . ' <br /> <img src="' . $default_dir . $data['image_name'] . '" style="max-width: 200px; max-height:200px; float: right;" /> <br /> <br /> <b>First_name :</b> ' . $data['first_name'] . ' <br /> <br /> <b>Last_name :</b> ' . $data['last_name'] . ' <br /> <br /> <b>Phone :</b> ' . $data['phone'] . ' <br /> <br /> <b>Email :</b> ' . $data['email'] . ' <br /> <br /> <b>Approved :</b> ' . ($data['approved'] == 1 ? 'Yes' : 'No') . ' <br /> <br /> <b>Address :</b> ' . $data['address'] . ' <br /> <br /> <b>Admin_description :</b> <br /> <br />' . $data['admin_desc'] . ' <br /> <br /> <a href="?edit&pid=' . $data['pid'] . '" class="button">Edit</a> <br /> <br /> '; } else { echo'<form method="POST" action="?edit&pid=' . intval($_GET['pid']) . '"> Editting photo: <a style="color:rgb(51,151,251);" href="' . $default_dir . $data['image_name'] . '">' . $data['image_name'] . '</a> uploaded by ' . $data['first_name'] . ' ' . $data['last_name'] . ' <br /> <img src="' . $default_dir . $data['image_name'] . '" style="max-width: 200px; max-height:200px; float: right;" /> <br /> First_name : <input type="text" name="first" value="' . $data['first_name'] . '" /> Last_name : <input type="text" name="last" value="' . $data['last_name'] . '" /> Phone : <input type="text" name="phone" value="' . $data['phone'] . '" /> Email : <input type="text" name="email" value="' . $data['email'] . '" /> Approved : <input type="text" name="approved" value="' . $data['approved'] . '" /> 0 or 1 Address : <textarea name="addr">' . $data['address'] . '</textarea> Admin_description : <textarea name="admin_desc">' . $data['admin_desc'] . '</textarea> <input type="submit" name="submit" value="Edit" /> </form>'; } } else { echo"NO SUCH PICTURE EXISTS"; } } else { echo" <h3>Search for image to " . (isset($_GET['view']) ? 'view' : 'edit') . " it: </h3> <form id='form' method='POST' action='admin.php?edit" . (isset($_GET['view']) ? '&view' : '') . "'> Uploader's first name: <input type='text' name='name' value='' /> OR Image name: <input type='text' name='img' value='' /> OR Email: <input type='text' name='email' value='' /> <input type='submit' name='submit' value='Search' /> </form>"; if (isset($_POST['submit'])) { $name = empty($_POST['name']) ? '%' : '%' . $_POST['name'] . '%'; $email = empty($_POST['email']) ? '%' : '%' . $_POST['email'] . '%'; $img = empty($_POST['img']) ? '%' : '%' . $_POST['img'] . '%'; $query = mysql_query( 'SELECT * from `' . $table_name . '` where ' . 'first_name like "' . $name . '" and ' . 'email like "' . $email . '" and ' . 'image_name like "' . $img . '"'); if (mysql_num_rows($query) > 0) { while ($pic = mysql_fetch_array($query)) { echo"<hr/><strong>Uploaded by $pic[first_name] $pic[last_name] " . date(' \a\t H:i \o\n d/m/Y', intval($pic['image_name'])) . "</strong> - <a href='$default_dir$pic[image_name]' target='_blank'>$pic[image_name]</a> <a href='?edit" . (isset($_GET['view']) ? '&view' : '') . "&pid=$pic[pid]' class='button'>" . (isset($_GET['view']) ? 'View' : 'Edit') . "</a>"; } } else { echo "NO IMAGE MATCHES THAT DESCRIPTION"; } } } exit; } if (isset($_GET['approve'])) { if (mysql_query('UPDATE `' . $table_name . '` SET approved = 1 WHERE pid = ' . intval($_GET['approve']))) { if (@mail($to, $subject, $message)) { echo('<p>Mail sent successfully.</p>'); } else { echo('<p>Mail could not be sent.</p>'); } echo"<h2>APPROVEDs</h2>"; } } It's not whole script though. i wrote this code <?php $mail_to='XXX@gmail.com'; $subject_mail='UYYYY'; $message='XXXXX'; $headers = "MIME-Version: 1.0 \r\n"; $headers .= "Content-type: text/html; charset=UTF-8 \r\n"; $headers .= "From: XXX@gmail.com \r\n"; mail($mail_to, $subject_mail, $message, $headers ); ?> but the email will not send why please? Hello, I have a registration page and when a user registers I want to send their email an activation code. To test I have the following code. <?php //Send activation Email $to = $Email_address; $subject = "www.WEBSITE.com/"; $message = "Welcome to my website!\r\rYou, or someone using your email address, has completed registration at WEBSITE. You can complete registration by clicking the following link:\rhttp://www.WEBSITE.com/register.php?action=verify&$activationKey\r\r If this is an error, ignore this email and you will be removed from our mailing list.\r\rRegards,\ "; $headers = 'From: noreply@abc.com' . "\r\n" . 'Reply-To: noreply@abc.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); mail($to, $subject, $message, $headers); if (mail($to, $subject, $message, $headers)) { echo 'Success'; } else { echo 'Error'; } ?> I have the last if else to see if it is sent to not. I do get a Success message, but I don't get an email at the address (I have checked the spam folder). I have checked the email address and it is correct. I have even waited a few hours and nothing. Why is the email not getting to my account? |