IamSuchANoob Posted February 20, 2015 Share Posted February 20, 2015 I am trying to figure out how to include a picture in the mail I send in PHP, any help is welcome! Code: <?php if(!isset($_POST['submit'])) { //This page should not be accessed directly. Need to submit the form. echo "error; you need to submit the form!"; } $name = $_POST['cformname']; $email = $_POST['cformemail']; $message = $_POST['cformmessage']; //Validate first if(empty($name)||empty($email)) { echo "Name and email are mandatory!"; exit; } if(IsInjected($email)) { echo "Bad email value!"; exit; } $email_from = 'info@test.com'; $email_subject = "Message from homepage!"; $email_body = "New message from user: $name.\n". "name:$name\n". "email:$email\n". "message:$message\n". " '<img src="http://css-tricks.com/examples/WebsiteChangeRequestForm/images/wcrf-header.png" alt="Website Change Request" />';n". $to = "info@test.com";//<== update the email address $headers = "From: $email_from \r\n"; $headers .= "Reply-To: $email \r\n"; //Send the email! mail($to,$email_subject,$email_body,$headers); //done. redirect to thank-you page. header('Location: pages/thankyou.php'); // Function to validate against any email injection attempts function IsInjected($str) { $injections = array('(\n+)', '(\r+)', '(\t+)', '(%0A+)', '(%0D+)', '(%08+)', '(%09+)' ); $inject = join('|', $injections); $inject = "/$inject/i"; if(preg_match($inject,$str)) { return true; } else { return false; } } function filter($data) { $data = trim(htmlentities(strip_tags($data))); if (get_magic_quotes_gpc()) { $data = stripslashes($data); } return $data; } ?> Quote Link to comment Share on other sites More sharing options...
tryingtolearn Posted February 20, 2015 Share Posted February 20, 2015 You have set the content type header to text/html otherwise it will be treated as plain text. Here is an example you can adapt. Ref - http://php.net/manual/en/function.mail.php Example #4 <?php $to = 'to@example.net';// to email address // subject $subject = 'Send a pic'; // message $body = ' <html> <head> <title>My Pic</title> </head> <body> <p>Name:'.$name.'</p> <p>Email:'.$email.'</p> <p>Message:'.$message.'</p> <img src="YOUR_IMAGE_URL.jpg"> </body> </html> '; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; // Additional headers $headers .= 'To: name <name@example.com>' . "\r\n"; $headers .= 'From: pic example <pic@example.com>' . "\r\n"; // Mail it mail($to, $subject, $body, $headers); ?> Quote Link to comment Share on other sites More sharing options...
rwhite35 Posted February 20, 2015 Share Posted February 20, 2015 Also you may want to consider a library like PHPMailer. https://github.com/Synchro/PHPMailer . Personally I user Pear Mail, but I think PHPMailer is better supported. Good luck. Quote Link to comment Share on other sites More sharing options...
Solution IamSuchANoob Posted February 20, 2015 Author Solution Share Posted February 20, 2015 Thanks, that worked out great! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.