Jump to content

a little help with PHP mail


IamSuchANoob

Recommended Posts

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 = '[email protected]';
$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 = "[email protected]";//<== 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;
}   
?>
Link to comment
https://forums.phpfreaks.com/topic/294749-a-little-help-with-php-mail/
Share on other sites

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  = '[email protected]';// 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 <[email protected]>' . "\r\n";
$headers .= 'From: pic example <[email protected]>' . "\r\n";


// Mail it
mail($to, $subject, $body, $headers);
?>

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.