9 Replies - 3564 Views - Last Post: 07 November 2011 - 12:36 PM Rate Topic: -----

#1 kurazi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 27-March 11

Include image in PHP email form

Posted 06 November 2011 - 01:11 PM

Hi there,

I have created a simple php email form, which works just fine, but now I have been asked to include an image of the company logoat the top of the email the user receives. I am new to php, so my first thought was that I could just add a line that had the path to the image on the server like so: $mailimg = 'http://domain.ca/images/image.gif'; and then call it in the $message:
$headers = "From: $email\r\n\r\n";
$subject = "Cancellation Request Form from Website";
$message .= "$mailimg";
$message = "Notice Date: $ndate\r
Brokerage: $brokerage\r

but that doesnt seem to be working for me. I get the email message, but with no image attached. Any ideas on how I can simply do this. Again I am new to php, so the easier the better :)

Here is my code
 <?php
if ($_SERVER['REQUEST_METHOD'] != 'POST'){
      $me = $_SERVER['PHP_SELF'];

?>
          <form name="form1" method="post"
         action="<?php echo $me;?>">
            <table width="97%" border="0" align="center" cellpadding="0" cellspacing="0">
    <tr>
      <td><img src="../images/dot_trans.gif" width="2" height="2" /><br />
        <table width="97%" border="0" align="center" cellpadding="0" cellspacing="0">
        <tr>
          <td><img src="../images/dot_trans.gif" width="10" height="10" /><br />
          <table width="100%" height="183" border="0" align="center" cellpadding="3" cellspacing="3">
                <tr>
                  <td width="147"><strong>Notice Date</strong></td>
                  <td width="263"><span id="sprytextfield1">
                    <label>
                      <input name="ndate" type="text" id="ndate" size="40" />
                      </label>
                    <span class="textfieldRequiredMsg">Please Enter Notice Date.</span></span></td>
                  <td width="127"><strong>Broker Name</strong></td>
                  <td width="271"><span id="sprytextfield7">
                    <input name="brokername" type="text" id="brokername" size="40" />
                    <span class="textfieldRequiredMsg">Please Enter Broker Name.</span></span></td>
                </tr>
                <tr>
                  <td><strong>Brokerage</strong></td>
                  <td><span id="sprytextfield2">
                    <label>
                      <input name="brokerage" type="text" id="brokerage" size="40" />
                    </label>
                    <span class="textfieldRequiredMsg">Please Enter Brokerage</span></span></td>
                  <td><strong>Phone Number</strong></td>
                  <td><span id="sprytextfield8">
                    <input name="pnum" type="text" id="pnum" size="40" />
                    <span class="textfieldRequiredMsg">Please Enter A Phone Number.</span></span></td>
                  </tr>
                <tr>
                  <td><strong>RJFC Contract #</strong></td>
                  <td><span id="sprytextfield3">
                    <input name="rjfccon" type="text" id="rjfccon" size="40" />
                    <span class="textfieldRequiredMsg">Please Enter A Contact Number</span></span></td>
                  <td><strong>E Mail</strong></td>
                  <td><span id="sprytextfield9">
                  <input name="email" type="text" id="email" size="40" />
                  <span class="textfieldRequiredMsg">Please Enter A E Mail Address.</span><span class="textfieldInvalidFormatMsg">Invalid format.</span></span></td>
                  </tr>
                <tr>
                  <td height="28"><strong>Insured</strong></td>
                  <td><span id="sprytextfield4">
                    <input name="insured" type="text" id="insured" size="40" />
                    <span class="textfieldRequiredMsg">Please Enter Name of Insured</span></span></td>
                  <td rowspan="3" valign="top"><strong>Reason for cancellation</strong></td>
                  <td rowspan="3"><label for="cancel"></label>
                    <span id="sprytextarea1">
                    <textarea name="cancel" id="cancel" cols="30" rows="5"></textarea>
                    <span class="textareaRequiredMsg">Please enter a reason for cancellation.</span></span></td>
                  </tr>
                <tr>
                  <td height="28"><strong>Policy #</strong></td>
                  <td><span id="sprytextfield5">
                    <input name="polnum" type="text" id="polnum" size="40" />
                    <span class="textfieldRequiredMsg">Please Enter A Policy Number.</span></span></td>
                  </tr>
                <tr>
                  <td height="22">&nbsp;</td>
                  <td>&nbsp;</td>
                </tr>
                </table>
          <br />
          <br />
          <table width="822" align="center" cellpadding="0" cellspacing="0">
          
            <tr>
              <td width="569"><div align="left">RJFC will process the Cancellation Request    upon receipt.&nbsp; All documents will be    sent to the    Insured by registered mail.&nbsp; A fax or    e-mail copy of the documents will be sent to the Broker and Insurer.                </div>                <p>&nbsp;</p></td>
            </tr>
        </table>
          <table width="97%" border="0" align="center" cellpadding="3" cellspacing="3">
            <tr>
                    <td width="801"><div align="center">
                      <label>
                        <input name="Submit" type="submit" value="Submit" />
                        </label>
                      <label>
                        <input name="reset" type="reset" id="reset" value="Reset" />
                        </label>
                      </div></td>
                  </tr>
        </table>
           
            <img src="../images/dot_trans.gif" width="10" height="10" /></td>
        </tr>
      </table>
      
      <img src="../images/dot_trans.gif" width="2" height="2" /></td>
    </tr>
  </table>
</form>
<?php
} else {
error_reporting(0);
$recipient = 'contact@domain.ca';
$ndate = stripslashes($_POST['ndate']);
$brokerage = stripslashes($_POST['brokerage']);
$rjfccon = stripslashes($_POST['rjfccon']);
$insured = stripslashes($_POST['insured']);
$polnum = stripslashes($_POST['polnum']);
$brokername = stripslashes($_POST['brokername']);
$pnum = stripslashes($_POST['pnum']);
$email = stripslashes($_POST['email']);
$cancel = stripslashes($_POST['cancel']);
$mailimg = 'http://domain.ca/images/image.gif';


$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: multipart/mixed;charset=us-ascii\r\n";
$headers .= "From: contact@domain.ca";



$headers = "From: $email\r\n\r\n";
$subject = "Cancellation Request Form from Website";
$message .= "$mailimg";
$message = "Notice Date: $ndate\r
Brokerage: $brokerage\r
RJFC Contact Number: $rjfccon\r
Insured: $insured\r
Policy Number: $polnum\r
Broker Name: $brokername\r
Phone Number: $pnum\r
Email: $email\r
Reason for cancellation: $cancel\r


      "; 
      mail($recipient, $subject, $message, $headers);
      if (!mail) {
          echo "Message failed to send";
      } else {
          echo nl2br ("<center><br><br><br><br><br><br><br><br><br><br>Thank you.  Your request has been sent.  A representative from RJFC will contact you shortly.<br><br><br><br><br><br><br><br><br><br></center>");
      }
}
?>


This post has been edited by kurazi: 06 November 2011 - 01:12 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Include image in PHP email form

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,738
  • Joined: 08-August 08

Re: Include image in PHP email form

Posted 06 November 2011 - 01:25 PM

View Postkurazi, on 06 November 2011 - 04:11 PM, said:

I have created a simple php email form, which works just fine, but now I have been asked to include an image of the company logoat the top of the email the user receives.

Ugh.

If it works just fine then it should be easy to make changes. If you could read/edit/debug it then it would be working fine. Functioning is not the same as working fine. Your problem is that you've got display code mixed with logic code and so you can't read either.
Was This Post Helpful? 0
  • +
  • -

#3 kurazi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 27-March 11

Re: Include image in PHP email form

Posted 06 November 2011 - 01:30 PM

View PostCTphpnwb, on 06 November 2011 - 01:25 PM, said:

View Postkurazi, on 06 November 2011 - 04:11 PM, said:

I have created a simple php email form, which works just fine, but now I have been asked to include an image of the company logoat the top of the email the user receives.

Ugh.

If it works just fine then it should be easy to make changes. If you could read/edit/debug it then it would be working fine. Functioning is not the same as working fine. Your problem is that you've got display code mixed with logic code and so you can't read either.


HENCE my "new to php". Thanks for your help.. but next time check your attitude at the door.

This post has been edited by kurazi: 06 November 2011 - 01:30 PM

Was This Post Helpful? -1
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,738
  • Joined: 08-August 08

Re: Include image in PHP email form

Posted 06 November 2011 - 01:38 PM

Read the link. Refactor your code. Then ask specific questions about the parts you don't understand.

If you don't want attitude then don't use phrases like "that doesnt seem to be working". Be descriptive and show that you're making an effort to solve your own problem.
Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,738
  • Joined: 08-August 08

Re: Include image in PHP email form

Posted 06 November 2011 - 02:12 PM

Ok, after taking a second and closer look, your code isn't as mangled as I usually see when there's so much html mixed with PHP. I believe you could get it working by changing this line:
$mailimg = 'http://domain.ca/images/image.gif';

to this:
$mailimg = '<img src="http://domain.ca/images/image.gif" alt="Image missing!" />';

I still strongly urge you to treat HTML and PHP as distinct languages that run on different computers. Because they are and they do!
Was This Post Helpful? 0
  • +
  • -

#6 kurazi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 27-March 11

Re: Include image in PHP email form

Posted 06 November 2011 - 03:09 PM

thanks for your reply. I tried to add that code and I also separated the file as per your advice. When i receive the email back it comes back as a link to the image like this:
<img src="http://domain.ca/new/images/hand.gif" alt="Image missing!" />

I was hoping to get the actual image to load. Here is my code

form.html
<form name="form1" method="post" action="sent.php">
            <table width="97%" border="0" align="center" cellpadding="0" cellspacing="0">
    <tr>
      <td><img src="../images/dot_trans.gif" width="2" height="2" /><br />
        <table width="97%" border="0" align="center" cellpadding="0" cellspacing="0">
        <tr>
          <td><img src="../images/dot_trans.gif" width="10" height="10" /><br />
          <table width="100%" height="183" border="0" align="center" cellpadding="3" cellspacing="3">
                <tr>
                  <td width="147"><strong>Notice Date</strong></td>
                  <td width="263"><span id="sprytextfield1">
                    <label>
                      <input name="ndate" type="text" id="ndate" size="40" />
                      </label>
                    <span class="textfieldRequiredMsg">Please Enter Notice Date.</span></span></td>
                  <td width="127"><strong>Broker Name</strong></td>
                  <td width="271"><span id="sprytextfield7">
                    <input name="brokername" type="text" id="brokername" size="40" />
                    <span class="textfieldRequiredMsg">Please Enter Broker Name.</span></span></td>
                </tr>
                <tr>
                  <td><strong>Brokerage</strong></td>
                  <td><span id="sprytextfield2">
                    <label>
                      <input name="brokerage" type="text" id="brokerage" size="40" />
                    </label>
                    <span class="textfieldRequiredMsg">Please Enter Brokerage</span></span></td>
                  <td><strong>Phone Number</strong></td>
                  <td><span id="sprytextfield8">
                    <input name="pnum" type="text" id="pnum" size="40" />
                    <span class="textfieldRequiredMsg">Please Enter A Phone Number.</span></span></td>
                  </tr>
                <tr>
                  <td><strong>RJFC Contract #</strong></td>
                  <td><span id="sprytextfield3">
                    <input name="rjfccon" type="text" id="rjfccon" size="40" />
                    <span class="textfieldRequiredMsg">Please Enter A Contact Number</span></span></td>
                  <td><strong>E Mail</strong></td>
                  <td><span id="sprytextfield9">
                  <input name="email" type="text" id="email" size="40" />
                  <span class="textfieldRequiredMsg">Please Enter A E Mail Address.</span><span class="textfieldInvalidFormatMsg">Invalid format.</span></span></td>
                  </tr>
                <tr>
                  <td height="28"><strong>Insured</strong></td>
                  <td><span id="sprytextfield4">
                    <input name="insured" type="text" id="insured" size="40" />
                    <span class="textfieldRequiredMsg">Please Enter Name of Insured</span></span></td>
                  <td rowspan="3" valign="top"><strong>Reason for cancellation</strong></td>
                  <td rowspan="3"><label for="cancel"></label>
                    <span id="sprytextarea1">
                    <textarea name="cancel" id="cancel" cols="30" rows="5"></textarea>
                    <span class="textareaRequiredMsg">Please enter a reason for cancellation.</span></span></td>
                  </tr>
                <tr>
                  <td height="28"><strong>Policy #</strong></td>
                  <td><span id="sprytextfield5">
                    <input name="polnum" type="text" id="polnum" size="40" />
                    <span class="textfieldRequiredMsg">Please Enter A Policy Number.</span></span></td>
                  </tr>
                <tr>
                  <td height="22">&nbsp;</td>
                  <td>&nbsp;</td>
                </tr>
                </table>
          <br />
          <br />
          <table width="822" align="center" cellpadding="0" cellspacing="0">
          
            <tr>
              <td width="569"><div align="left">RJFC will process the Cancellation Request    upon receipt.&nbsp; All documents will be    sent to the    Insured by registered mail.&nbsp; A fax or    e-mail copy of the documents will be sent to the Broker and Insurer.                </div>                <p>&nbsp;</p></td>
            </tr>
        </table>
          <table width="97%" border="0" align="center" cellpadding="3" cellspacing="3">
            <tr>
                    <td width="801"><div align="center">
                      <label>
                        <input name="Submit" type="submit" value="Submit" />
                        </label>
                      <label>
                        <input name="reset" type="reset" id="reset" value="Reset" />
                        </label>
                      </div></td>
                  </tr>
        </table>
           
            <img src="../images/dot_trans.gif" width="10" height="10" /></td>
        </tr>
      </table>
      
      <img src="../images/dot_trans.gif" width="2" height="2" /></td>
    </tr>
  </table>
</form>




sent.php
<?php
      error_reporting(0);
      $recipient = 'contact@domain.ca';
	  $ndate = stripslashes($_POST['ndate']);
      $brokerage = stripslashes($_POST['brokerage']);
      $rjfccon = stripslashes($_POST['rjfccon']);
	  $insured = stripslashes($_POST['insured']);
	  $polnum = stripslashes($_POST['polnum']);
      $brokername = stripslashes($_POST['brokername']);
	  $pnum = stripslashes($_POST['pnum']);
      $email = stripslashes($_POST['email']);
	  $cancel = stripslashes($_POST['cancel']);
	  $mailimg = '<img src="http://domain.ca/new/images/hand.gif" alt="Image missing!" />';
	  

$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain;charset=us-ascii\r\n";
$headers .= "From: contact@domain.ca";



$headers = "From: $email\r\n\r\n";
$subject = "Cancellation Request Form from Website";
$message = "Notice Date: $ndate\r
$mailimg \r
Brokerage: $brokerage\r
RJFC Contact Number: $rjfccon\r
Insured: $insured\r
Policy Number: $polnum\r
Broker Name: $brokername\r
Phone Number: $pnum\r
Email: $email\r
Reason for cancellation: $cancel\r


      "; 
      mail($recipient, $subject, $message, $headers);
      if (!mail) {
          echo "Message failed to send";
      } else {
          echo nl2br ("<center><br><br><br><br><br><br><br><br><br><br>Thank you.  Your request has been sent.  A representative from RJFC will contact you shortly.<br><br><br><br><br><br><br><br><br><br></center>");
      }
?>


This post has been edited by kurazi: 06 November 2011 - 03:11 PM

Was This Post Helpful? 1
  • +
  • -

#7 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,738
  • Joined: 08-August 08

Re: Include image in PHP email form

Posted 06 November 2011 - 03:29 PM

Now that they're separated it's easier to look through the relevant code and see that your header is specifying plain text. Try using text/html:
$headers .= "Content-type: text/plain;charset=us-ascii\r\n";

Was This Post Helpful? 0
  • +
  • -

#8 kurazi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 27-March 11

Re: Include image in PHP email form

Posted 06 November 2011 - 04:08 PM

Still shows the link. I wonder if it has anything to do with the charset. I have seen a few different examples, and tried the $headers .= "Content-type: text/html;charset=UTF-8 \r\n"; which produced the link as well. I will keep looking to see if I can find any other solutions. Any further ideas as welcome :)
Was This Post Helpful? 0
  • +
  • -

#9 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,738
  • Joined: 08-August 08

Re: Include image in PHP email form

Posted 06 November 2011 - 04:41 PM

It's always a good idea to post the current code along with the current problem.

Without seeing the new code the only thing that stands out is that these two lines look funny:
      mail($recipient, $subject, $message, $headers);
      if (!mail) {


I'd do this:
      if (!mail($recipient, $subject, $message, $headers)) {

Was This Post Helpful? 0
  • +
  • -

#10 kurazi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 27-March 11

Re: Include image in PHP email form

Posted 07 November 2011 - 12:36 PM

ok, so good news! i found a script that sends an email from a page onload, and I was able to get it to send me an email with a picture from my server with no problem. From there, I tried to incorporate it into my form, but the image fails to come with the email message. I do still get the email, with the text, but i also get a few other lines of code, and then a page full of rambled text like this:

Content-Type: image/jpg; name="hand.jpg"
Content-Transfer-Encoding: base64
Content-disposition: attachment; file="hand.jpg"

/9j/4AAQSkZJRgABAgAAZABkAAD/7AARRHVja3kAAQAEAAAAUAAA/+4ADkFkb2JlAGTAAAAAAf/b
AIQAAgICAgICAgICAgMCAgIDBAMCAgMEBQQEBAQEBQYFBQUFBQUGBgcHCAcHBgkJCgoJCQwMDAwM
DAwMDAwMDAwMDAEDAwMFBAUJBgYJDQsJCw0PDg4ODg8PDAwMDAwPDwwMDAwMDA8MDAwMDAwMDAwM
DAwMDAwMDAwMDAwMDAwMDAwM/8AAEQgAuwFSAwERAAIRAQMRAf/EALwAAAEEAwEBAQAAAAAAAAAA

Maybe someone can spot something I am missing. Here is the code for the onload php page that is working by sending an email to my email address with a image from my server


<body>
<?PHP 	 
$to = 	'contact@domain.ca';
$subject = 	'PHP Mail Attachment Test';
$bound_text = 	"jimmyP123";
$bound = 	"--".$bound_text."\r\n";
$bound_last = 	"--".$bound_text."--\r\n";
  	 
$headers = 	"From: admin@server.com\r\n";
$headers .= 	"MIME-Version: 1.0\r\n"
  	."Content-Type: multipart/mixed; boundary=\"$bound_text\"";
  	 
$message .= 	"If you can see this MIME than your client doesn't accept MIME types!\r\n"
  	.$bound;
  	 
$message .= 	"Content-Type: text/html; charset=\"iso-8859-1\"\r\n"
  	."Content-Transfer-Encoding: 7bit\r\n\r\n"
  	.$bound;
  	 
$file = 	file_get_contents("http://www.domain.ca/new/images/hand.jpg");
  	 
$message .= 	"Content-Type: image/jpg; name=\"hand.jpg\"\r\n"
  	."Content-Transfer-Encoding: base64\r\n"
  	."Content-disposition: attachment; file=\"hand.jpg\"\r\n"
  	."\r\n"
  	.chunk_split(base64_encode($file))
  	.$bound_last;

if(mail($to, $subject, $message, $headers))
{
     echo 'MAIL SENT';
} else {
     echo 'MAIL FAILED';
}

?>




And here is my page that I tried to incorporate with it.
<?php
      error_reporting(0);
      $recipient = 'contact@domain.ca';
	  $subject = "Cancellation Request Form from Website";
	  $ndate = stripslashes($_POST['ndate']);
      $brokerage = stripslashes($_POST['brokerage']);
      $rjfccon = stripslashes($_POST['rjfccon']);
	  $insured = stripslashes($_POST['insured']);
	  $polnum = stripslashes($_POST['polnum']);
      $brokername = stripslashes($_POST['brokername']);
	  $pnum = stripslashes($_POST['pnum']);
      $email = stripslashes($_POST['email']);
	  $cancel = stripslashes($_POST['cancel']);
	  $bound_text = 	"jimmyP123";
		$bound = 	"--".$bound_text."\r\n";
		$bound_last = 	"--".$bound_text."--\r\n";

$headers .= 	"MIME-Version: 1.0\r\n"
  	."Content-Type: multipart/mixed; boundary=\"$bound_text\"";
$headers = "From: $email\r\n\r\n";

 	 
$message .= 	"If you can see this MIME than your client doesn't accept MIME types!\r\n"
  	.$bound;
  	 
$message .= 	"Content-Type: text/html; charset=\"iso-8859-1\"\r\n"
  	."Content-Transfer-Encoding: 7bit\r\n\r\n"
  	.$bound;
	
$message = "Notice Date: $ndate\r
Brokerage: $brokerage\r
RJFC Contact Number: $rjfccon\r
Insured: $insured\r
Policy Number: $polnum\r
Broker Name: $brokername\r
Phone Number: $pnum\r
Email: $email\r
Reason for cancellation: $cancel\r"; 

  	 
$file = 	file_get_contents("http://www.domain.ca/new/images/hand.jpg");
  	 
$message .= 	"Content-Type: image/jpg; name=\"hand.jpg\"\r\n"
  	."Content-Transfer-Encoding: base64\r\n"
  	."Content-disposition: attachment; file=\"hand.jpg\"\r\n"
  	."\r\n"
  	.chunk_split(base64_encode($file))
  	.$bound_last;

if (!mail($recipient, $subject, $message, $headers)) { 

  echo "Message failed to send";
} else {
  echo nl2br ("<center><br><br><br><br><br><br><br><br><br><br>Thank you.  Your request has been sent.  A representative from RJFC will contact you shortly.<br><br><br><br><br><br><br><br><br><br></center>");
}
?>


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1