Jump to content

a little help with PHP mail


IamSuchANoob
Go to solution Solved by 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 = '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;
}   
?>
Link to comment
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  = '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);
?>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.