Jump to content

Recommended Posts

I am having trouble learning how to a send an attachment using a PHP script. Any help is greatly appreciated. Below is my script followed by a snippet of my HTML:

 

<?php

$to = "jsmith@yahoo.com";

$subject = "Purchase Received!";

$email = $_REQUEST['email'];

$mar_stat = $_REQUEST['mar_stat'];

$client_name = $_REQUEST['client_name'];

$spouse_name = $_REQUEST['spouse_name'];

$client_dob_month = $_REQUEST['client_dob_month'];

$client_dob_day = $_REQUEST['client_dob_day'];

$client_dob_year = $_REQUEST['client_dob_year'];

$spouse_dob_month = $_REQUEST['spouse_dob_month'];

$spouse_dob_day = $_REQUEST['spouse_dob_day'];

$spouse_dob_year = $_REQUEST['spouse_dob_year'];

$client_income = $_REQUEST['client_income'];

$spouse_income = $_REQUEST['spouse_income'];

$address = $_REQUEST['address'];

$city = $_REQUEST['city'];

$state = $_REQUEST['state'];

$zip = $_REQUEST['zip'];

$email = $_REQUEST['email'];

$area = $_REQUEST['area'];

$prefix = $_REQUEST['prefix'];

$phone = $_REQUEST['phone'];

$contact_method = $_REQUEST['contact_method'];

 

$client_ret_age = $_REQUEST['client_ret_age'];

$spouse_ret_age = $_REQUEST['spouse_ret_age'];

$desired_gross_inc = $_REQUEST['desired_gross_inc'];

$apprx_ret_invest = $_REQUEST['apprx_ret_invest'];

$apprx_nonret_invest = $_REQUEST['apprx_nonret_invest'];

$pension = $_REQUEST['pension'];

$pension_comments = $_REQUEST['pension_comments'];

$life_expect = $_REQUEST['life_expect'];

$spouse_life_expect = $_REQUEST['spouse_life_expect'];

$life_expect_comments = $_REQUEST['life_expect_comments'];

$work_ret = $_REQUEST['work_ret'];

$spouse_work_ret = $_REQUEST['spouse_work_ret'];

$work_ret_comments = $_REQUEST['work_ret_comments'];

$inheritance = $_REQUEST['inheritance'];

$inheritance_comments = $_REQUEST['inheritance_comments'];

 

$headers = "Content-type: text/html; charset=iso-8859-1\r\n";

$headers .= "From: $email";

 

$message = "<b>Purchase Received! Customer Infromation Form</b><br><br><b>Married?:</b> $mar_stat<br><b>Client Name:</b> $client_name<br><b>Spouse_Name:</b> $spouse_name<br><b>Client DOB:</b> $client_dob_month-$client_dob_day-$client_dob_day<br><b>Spouse DOB:</b> $spouse_dob_month-$spouse_dob_day-spouse_dob_day<br><b>Client Income:</b> $client_income<br><b>Spouse Income:</b> $spouse_income<br><br><b>Address:</b> $address<br><b>City:</b> $city<br><b>State:</b> $state<br><b>Zip:</b> $zip<br><b>Email:</b> $email<br><b>Phone:</b> $area-$prefix-$phone<br><b>Best Contact Method:</b> $contact_method<br><br><b>Desired Client Retirement Age:</b> $client_ret_age<br><b>Desired Spouse Retirement Age:</b> $spouse_ret_age<br><b>Desired Gross Income:</b> $desired_gross_inc<br><b>Approximate Retirement Investments:</b> $apprx_ret_invest<br><b>Approximate Non-Retirement Investments:</b> $apprx_nonret_invest<br><br><b>Pension:</b> $pension<br><b>Pension Comments:</b> $pension_comments<br><br><b>Client Life Expectancy:</b> $life_expect<br><b>Spouse Life Expectancy:</b> $spouse_life_expect<br><b>Life Expectancy Comments:</b> $life_expect_comments<br><br><b>Client Working in Retirement:</b> $work_ret<br><b>Spouse Working in Retirement:</b> $spouse_work_ret<br><b>Working in Retirement Comments:</b> $work_ret_comments<br><br><b>Inheritance Expected:</b> $inheritance<br><b>Inheritance Comments:</b> $inheritance_comments";

 

$sent = mail($to, $subject, $message, $headers);

if($sent)

header( "Location: http://www.sspla.com/thankyou_purchase.html" );

else

header( "Location: http://www.sspla.com/sorry_purchase.html" );

?>

 

Here is the HTML snippet:

 

<!-- ATTACH DOCUMENTATION -->

<fieldset> <legend> <b>Attach Statement</b> </legend>

<table border="0" cellpadding="2" cellspacing="3">

<tr>

<td align="left" valign="top">

<input type="file" name='statement' maxlength="275" /> <span style="font-size: .95em;"><i>NOTE: Failure to Attach Your Statement May Result in a Delay Proccessing Your Purchase</i></span>

</td>

</tr>

</table>

</fieldset>

 

THANKS!

Link to comment
https://forums.phpfreaks.com/topic/269336-php-sendmail-with-attachment/
Share on other sites

You have to format the message as MIME Message

 

I actually have a ready made script for it but what would be the fun in sharing that right away, you're here to learn right?

 

There are actual tools to do this but mine is just PHP and pushes the message out with the mail() function. Not too far from what you have now, but far more elaborate and what I consider portable (I don't have to rewrite the code just write configuration files.)

OK I will look into MIME format. This has to be done just when sending an email with an attachment? My script worked fine until I had to add the attcahment code. I thought it would be pretty straight forward but have obviously been struggling with it. Thanks

OK I will look into MIME format. This has to be done just when sending an email with an attachment? My script worked fine until I had to add the attcahment code. I thought it would be pretty straight forward but have obviously been struggling with it. Thanks

Yes if you want a successful file attached to an email message it should be done in MIME format.

 

An email message is a singular document, if there something such as a file attached it must be encoded into the message, and you very well should not expect the end user to translate that jumbled information into a readable format.

 

That is what MIME does.

 

To be honest I wish I could be more help at the moment, but I would have to remind myself of a few things too as I had got used to my ready made code. (all I had to do was run something like $emailer->attachment($filename); and my code did everything for me)

Alas had recently got off my windows server and setup my linux, so my old windows hard drive is locked away in my linux server and I have yet to implement means of remote access and I am away from it ATM, and the file is contained therein.

 

Some Key things:

  • Boundaries (What they are and how to use them)
  • Content-type
  • Content-disposition
  • Content-Transfer-Encoding
    (attachments would normally be base64 encoded so look into that too)
    So when placing the attachment into the message I would expect something similar to:
    base64_encode(file_get_contents($file));


Edited by DarkerAngel
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.