Re: problem sending attachement with the mail() function [message #178980] |
Fri, 31 August 2012 12:10 |
The Natural Philosoph
Messages: 993 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
anomit wrote:
> Hi all,
>
> I'm trying to send an email from a php script with an attachment. This
> is how I am constructing the message body. The mail is sent to the
> right address but the attachment doesn't appear and instead I get to
> see the entire contents of the $output string in the mail. I'd guess
> I'm going wrong with the message boundaries but some help would be
> appreciated
>
> $random_hash = md5(date('r', time()));
> $subject = "your free pdf is here";
> $headers = "From: no-reply(at)my-domain(dot)com\r\nReply-To: no-
> reply(at)my-domain(dot)com";
> $headers .= "\r\nContent-type: multipart/mixed; boundary=
> \"mailer-boundary-".$random_hash."\"";
> $attachment =
> chunk_split(base64_encode(file_get_contents('file.pdf')));
> //the body of the message with the message boundaries and
> attachment as multipart mime
> $output = "
> --mailer-boundary-$random_hash
> Content-Type: multipart/alternative; boundary='mailer-alt-
> $random_hash'
> --mailer-alt-$random_hash
> Content-Type: text/plain; charset='iso-8859-1'
> Content-Transfer-Encoding: 7bit
>
> Hi $fname!
> Your free pdf is attached with this mail.
>
> --mailer-alt-$random_hash
> Content-Type: text/html; charset='iso-8859-1'
> Content-Transfer-Encoding: 7bit
>
> <b>Hi $fname!</b>
> Your free pdf is attached with this mail.
> --mailer-alt-$random_hash--
>
> --mailer-boundary-$random_hash
> Content-Type: application/pdf; name=\"file.pdf\"
> Content-Transfer-Encoding: base64
> Content-Disposition: attachment; filename=\"file.pdf\"
>
> $attachment
> --mailer-boundary-$random_hash--";
> mail($email, $subject, $output, $headers) ;
>
> Thanks,
> Anomit
here is a function that works for me. The image gets attached Some of
the variables are globals like $return_path
function mail_html_file($email, $subject,$from,$return_path,$body)
{
$attach=
chunk_split(base64_encode(file_get_contents('../Images/Logo2.gif')));
$boundary = md5( time() );
$headers = sprintf("From: %s\nMIME-Version: 1.0\nContent-Type:
multipart/related; boundary=\"%s\"",
$from, $boundary);
$message = sprintf("--%s\nContent-Type: text/html;
charset=\"iso-9959-1\"\nContent-Transfer-Encoding:
quoted-printable\n\n%s\n\n--%s\nContent-Type: Image/gif;
name=\"Logo.gif\"\nContent-ID: <Logo2.gif>\nContent-Transfer-Encoding:
base64\n\n%s\n\n--%s--\n",
$boundary,$body,$boundary,$attach,$boundary);
mail($email, $subject, $message, $headers, "-f ".$return_path );
}
--
Ineptocracy
(in-ep-toc’-ra-cy) – a system of government where the least capable to
lead are elected by the least capable of producing, and where the
members of society least likely to sustain themselves or succeed, are
rewarded with goods and services paid for by the confiscated wealth of a
diminishing number of producers.
|
|
|