PHP to email (with optional attachements) expert required

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dave Smithz

    PHP to email (with optional attachements) expert required

    Hi there,

    Previously in my PHP code I was using Sendmail to send out emails to people
    in the MySQL DB. The requirement cam along to be able to send attachments
    and so I dug around for an easy bit of code that did not need classes and I
    could plug easily into my PHP code.

    I thought I found one (see bottom of email), however I have since discovered
    that as it is, this script has the following disadvantages when compared to
    using Sendmail as I used to:

    1. Sendmail sent out plaintext emails however if there was text that was
    like www.example.com then the code would still appear as a link in many
    plain text readers. This does not seem to happen when sent using the below
    script. Anyone understand why?
    (Note: I use the $Text parameter rather then the $HTML one because I need to
    assume customers will only have plain text email readers. I also did try
    putting the body of the email into both Text and HTML but I ended up
    receiving the email twice with the HTML version losing many carriage returns
    etc.).

    2. With send mail if the delivery address did not exist, we would get an
    email bounce back. This does not seem to happen with below despite my
    tweaking attempts. Anyone know why this would be?

    I may have some more but issues but wont confuse for now any comments
    appreciated and please see the code below.

    Thanks




    *******Custom - insertable sendmail code - very handy if I could fully get
    it to work the way I want it to*****

    function
    SendMail($From, $FromName,$To,$ ToName,$Subject ,$Text,$Html,$A ttmFiles){
    $OB="----=_OuterBoundary _000";
    $IB="----=_InnerBoundery _001";
    $Html=$Html?$Ht ml:preg_replace ("/\n/","<br>",$T ext)
    or die("neither text nor html part present.");
    $Text=$Text?$Te xt:"Sorry, but you need an html mailer to read this mail.";
    $From or die("sender address missing");
    $To or die("recipient address missing");

    $headers ="MIME-Version: 1.0\r\n";
    $headers.="From : ".$FromName ." <".$From.">\n ";
    $headers.="To: ".$ToName." <".$To.">\n" ;
    #$headers.="To: ".$To."\n";
    #$headers.="Rep ly-To: ".$FromName ." <".$From.">\n ";
    #Prioriry 3 = normal 1 = high - apparently s- shouldlook more into thi
    #$headers.="X-Priority: 1\n";
    #$headers.="X-MSMail-Priority: High\n";
    #$headers.="X-MSMail-Priority: Normal\n";
    $headers.="X-Mailer: My PHP Mailer\n";
    $headers.="Cont ent-Type: multipart/mixed;\n\tbound ary=\"".$OB."\" \n";

    //Messages start with text/html alternatives in OB
    $Msg ="This is a multi-part message in MIME format.\n";
    $Msg.="\n--".$OB."\n";
    $Msg.="Content-Type: multipart/alternative;\n\ tboundary=\"".$ IB."\"\n\n";

    //plaintext section
    $Msg.="\n--".$IB."\n";
    $Msg.="Content-Type: text/plain;\n\tchars et=\"iso-8859-1\"\n";
    $Msg.="Content-Transfer-Encoding: quoted-printable\n\n";
    // plaintext goes here
    $Msg.=$Text."\n \n";

    // html section
    $Msg.="\n--".$IB."\n";
    $Msg.="Content-Type: text/html;\n\tcharse t=\"iso-8859-1\"\n";
    $Msg.="Content-Transfer-Encoding: base64\n\n";
    // html goes here
    $Msg.=chunk_spl it(base64_encod e($Html))."\n\n ";

    // end of IB
    $Msg.="\n--".$IB."--\n";

    // attachments
    if($AttmFiles){
    foreach($AttmFi les as $AttmFile){
    $patharray = explode ("/", $AttmFile);
    $FileName=$path array[count($patharra y)-1];
    $Msg.= "\n--".$OB."\n";
    $Msg.="Content-Type:
    application/octetstream;\n\ tname=\"".$File Name."\"\n";
    $Msg.="Content-Transfer-Encoding: base64\n";
    $Msg.="Content-Disposition:
    attachment;\n\t filename=\"".$F ileName."\"\n\n ";

    //file goes here
    $fd=fopen ($AttmFile, "r");
    $FileContent=fr ead($fd,filesiz e($AttmFile));
    fclose ($fd);
    $FileContent=ch unk_split(base6 4_encode($FileC ontent));
    $Msg.=$FileCont ent;
    $Msg.="\n\n";
    }
    }

    //message ends
    $Msg.="\n--".$OB."--\n";
    #mail($To,$Subj ect,$Msg,$heade rs);
    mail("",$Subjec t,$Msg,$headers );
    //syslog(LOG_INFO ,"Mail: Message sent to $ToName <$To>");
    }


  • Sadara

    #2
    Re: PHP to email (with optional attachements) expert required

    Dave Smithz wrote:[color=blue]
    > Hi there,
    >
    > Previously in my PHP code I was using Sendmail to send out emails to people
    > in the MySQL DB. The requirement cam along to be able to send attachments
    > and so I dug around for an easy bit of code that did not need classes and I
    > could plug easily into my PHP code.
    >
    > I thought I found one (see bottom of email), however I have since discovered
    > that as it is, this script has the following disadvantages when compared to
    > using Sendmail as I used to:
    >
    > 1. Sendmail sent out plaintext emails however if there was text that was
    > like www.example.com then the code would still appear as a link in many
    > plain text readers. This does not seem to happen when sent using the below
    > script. Anyone understand why?[/color]
    whether a string such as 'www.example.co m' is displayed as an HTML link
    depends on the email client ('reader') not on the email.
    [color=blue]
    > (Note: I use the $Text parameter rather then the $HTML one because I need to
    > assume customers will only have plain text email readers. I also did try
    > putting the body of the email into both Text and HTML but I ended up
    > receiving the email twice with the HTML version losing many carriage returns
    > etc.).
    >
    > 2. With send mail if the delivery address did not exist, we would get an
    > email bounce back. This does not seem to happen with below despite my
    > tweaking attempts. Anyone know why this would be?
    >
    > I may have some more but issues but wont confuse for now any comments
    > appreciated and please see the code below.
    >
    > Thanks[/color]

    i'd recommend using phpMailer, available from
    http://phpmailer.sourceforge.net/. does the job. simple to use. what
    more could you want?

    sadara

    Comment

    • Good Man

      #3
      Re: PHP to email (with optional attachements) expert required

      "Dave Smithz" <SPAM FREE WORLD> wrote in
      news:4276af4e$1 @news1.homechoi ce.co.uk:

      [color=blue]
      > I may have some more but issues but wont confuse for now any comments
      > appreciated and please see the code below.[/color]

      take five minutes and tweak this to your exact liking:



      it's just amazing.

      Comment

      • Dave Smithz

        #4
        Re: PHP to email (with optional attachements) expert required


        "Sadara" <sadaraNOWAY@NO WAYdds.nl> wrote in message
        news:4276ba31$0 $169$e4fe514c@n ews.xs4all.nl.. .[color=blue]
        > i'd recommend using phpMailer, available from
        > http://phpmailer.sourceforge.net/. does the job. simple to use. what
        > more could you want?[/color]


        What can I say. So far, so d*rn amazing!!!. I was scared off normally by
        the fact that you download lots of files and was not familiar with PHP
        classes.

        Easy when you try (well seems so, so far).

        Thanks


        Comment

        Working...