ALP - Aquileia Linux Project

Gruppo Utenti GNU/Linux – Free Software Bassa Friulana
Oggi è 28/05/2017, 22:16

Tutti gli orari sono UTC




Apri un nuovo argomento Rispondi all’argomento  [ 8 messaggi ] 
Autore Messaggio
 Oggetto del messaggio: mail con allegato
MessaggioInviato: 26/05/2008, 18:16 
Non connesso
pinguino_imperatore
pinguino_imperatore

Iscritto il: 10/07/2006, 9:57
Messaggi: 114
Ciao a tutti, sono tornato per l'ennesimo quesito!

Il mio attuale problema è inviare una mail con allegato attraverso i5. Ho detto con allegato perchè non ho riscontrato nessun problema ad inviarla senza allegato.
Vi posto il codice, aiutatemi a trovare l'errore

Codice:
<?php
//define the receiver of the email
$to = gigi@yahoo.it';
//define the subject of the email
$subject = 'Test email with attachment';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: webmaster@masterphp.it\r\nReply-To: we@mast.it";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$attachment = chunk_split(base64_encode(file_get_contents('/www/zendcore/htdocs/mail/a.zip')));
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-mixed-<?php echo $random_hash; ?> 
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"

--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Hello World!!!
This is simple text email message.

--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>

--PHP-alt-<?php echo $random_hash; ?>--

--PHP-mixed-<?php echo $random_hash; ?> 
Content-Type: application/zip; name="a.zip" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 

<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>--

<?php
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
?>


Top
 Profilo  
 
 Oggetto del messaggio: Re: mail con allegato
MessaggioInviato: 27/05/2008, 6:53 
Non connesso
slacker
slacker
Avatar utente

Iscritto il: 07/10/2004, 12:41
Messaggi: 1296
Località: Terra - Italia - Friûl - Udine
se togli la @ a mail vedi se hai dei warnings , credo che sia una soluzione per capire come mai non funziona.

Cmq ti consiglio di usare un framework , così non devi fare tutto quel casino

_________________
Devo Proprio? pare di si :)


Top
 Profilo  
 
 Oggetto del messaggio: Re: mail con allegato
MessaggioInviato: 27/05/2008, 7:35 
Non connesso
pinguino_imperatore
pinguino_imperatore

Iscritto il: 10/07/2006, 9:57
Messaggi: 114
non hai da postare un esempio facile di mail con allegato? non dirmi di prenderlo dalla rete perchè ne ho già presi a migliaia ma con pessimi risultati. ne vorrei uno semplice e testato al 100% e di te mi fido. non mi serve un form di scelta per l'allegato mi basta anche un allegato fisso residente su disco.
Grazie


Top
 Profilo  
 
 Oggetto del messaggio: Re: mail con allegato
MessaggioInviato: 27/05/2008, 9:02 
Non connesso
slacker
slacker
Avatar utente

Iscritto il: 07/10/2004, 12:41
Messaggi: 1296
Località: Terra - Italia - Friûl - Udine
dovrei avere qualcosa in giro .. te lo cerco , ma non ti prometto nulla di urgente

_________________
Devo Proprio? pare di si :)


Top
 Profilo  
 
 Oggetto del messaggio: Re: mail con allegato
MessaggioInviato: 28/05/2008, 10:20 
Non connesso
pinguino_imperatore
pinguino_imperatore

Iscritto il: 10/07/2006, 9:57
Messaggi: 114
... aspetto con ansia...


Top
 Profilo  
 
 Oggetto del messaggio: Re: mail con allegato
MessaggioInviato: 28/05/2008, 19:30 
Non connesso
pinguino_imperatore
pinguino_imperatore

Iscritto il: 10/07/2006, 9:57
Messaggi: 114
Questo è header al momento dell'invio della mail, dov'è l'errore???

From: Olaf Lederer <my@mail.com>
Reply-To: my_reply_to@mail.net
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="e8728f07aa2f1a214315f0a1c28d6e17"

This is a multi-part message in MIME format.
--e8728f07aa2f1a214315f0a1c28d6e17
Content-type:text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 7bit

Hallo,
do you like this script? I hope it will help.

gr. Olaf

--e8728f07aa2f1a214315f0a1c28d6e17
Content-Type: application/octet-stream; name="form.zip"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="form.zip"

UEsDBBQAAAAIAA4xxjI+KOL4swIAAKAKAAAIAAAAZm9ybS5odG3dVk1P3DAQvSPxH0au2gNSm0BV
idJsKoSKxKFwQOoVObbZjJrYkeOFsr+2P6XjOF6STUCAUFU1K63HX8/znsf27O5kpaurfJdKxaUv
k2gURt5RSZbjRaWgMFYqu2Apg1uUrlywTynZQlVV23CBetn1+XrDpezrhACQOduV3pJx9n6avmV5
JvEGeIVLvWBCaacstQUjf3DhMDUgDtHjCmM8uOnrzjQMrLkld6l2sIH7TGhQLIWpDC3z5jT1PzbE
vDa2Bs1rtWC1kavKMKiVK41csMa0jgEXDg2Bor5BXnOsPjRlwzr5SMAZjvfgwektplv92yTvWyWQ
25FQnrXOGr3MyWHtYEPoNE39Vl1zQQR+KCu55n5wzasq38uSYGSJn/Xo3GOLvGLQ4poq+1Mv/Se5
M2CKgviSZdFE3CQ6lzg55ZfMEHyYdK+mxWXpNkyexX5vRHfcuZmbdt88z3NTKzgxS03l0ZRj1HSG
a0chz1A3KwfurqGVnfpFURQizANGjQ8O2f+i17f33+lcvKpSyp+0vyPVU1hHPc+0RIvrtTkasIys
X0wWI+q/R/gEnfv9qmQFIfKXEKUmf5U+836dE2f6kGwdlF6FB67VrJiB7AW6bPgaDTTKgkVRomod
B9T+lfEdGgdazsF4pbhVfHNhOBUetgX72O2SNw7D80OC9KNz2L7pp5rOKfo0bd7pom2+zGxzM8uh
e1SnEdZvOKlY5MdCKOcQaOOEoeDvlPl6BGGl8A+XZzAKIsslmhhFznIKo5o8NFf0LGGXCqyog3fQ
NE6USvxUMu9BYXfn/OIlgNroqwhKuhbPkXguZLOEcoactAt+eQUfC77wLhPCyPV2VdToNj6e+dyE
URRsbcekHl1vBs2+0dajhgGzAacxmyyJuVwgFCf1E+4H95lS0mWdlISGrPQPUEsBAhQAFAAAAAgA
DjHGMj4o4vizAgAAoAoAAAgAAAAAAAAAAQAgAAAAAAAAAGZvcm0uaHRtUEsFBgAAAAABAAEANgAA
ANkCAAAAAA==


--e8728f07aa2f1a214315f0a1c28d6e17--


Top
 Profilo  
 
 Oggetto del messaggio: Re: mail con allegato
MessaggioInviato: 29/05/2008, 6:17 
Non connesso
slacker
slacker
Avatar utente

Iscritto il: 07/10/2004, 12:41
Messaggi: 1296
Località: Terra - Italia - Friûl - Udine
hai 2 boundaries , se vedi bene inizi 2 volte l'head

_________________
Devo Proprio? pare di si :)


Top
 Profilo  
 
 Oggetto del messaggio: Re: mail con allegato
MessaggioInviato: 30/05/2008, 16:36 
Non connesso
pinguino_imperatore
pinguino_imperatore

Iscritto il: 10/07/2006, 9:57
Messaggi: 114
Per la comunità...... invio mail con allegato!


class.Email.php
Codice:
<? 
//** ©William Fowler (wmfwlr@cogeco.ca)
//** MAY 13/2004, Version 1.1
//** - added support for CC and BCC fields.
//** - added support for multipart/alternative messages.
//** - added ability to create attachments manually using literal content.
//** DECEMBER 15/2003, Version 1.0

  if(isset($GLOBALS["emailmsgclass_php"])) { return; }  //** onlyinclude once.
  $GLOBALS["emailmsgclass_php"] = 1;                    //** filewas included.

//** the newline character(s) to be used when generating an email message.

  define("EmailNewLine", "\r\n");

//** the unique X-Mailer identifier for emails sent with this tool.

  define("EmailXMailer", "PHP-EMAIL,v1.1 (William Fowler)");

//** the default charset values for both text and HTML emails.

  define("DefaultCharset", "iso-8859-1");

//**!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//**EMAIL_MESSAGE_CLASS_DEFINITION********************************************
//** The Email class wrappers PHP's mail function into a class capable of
//** sending attachments and HTML email messages. Custom headers can also be
//** included as if using the mail function.

class Email
{
//** (String) the recipiant email address, or comma separated addresses.

  var $To = null;

//** (String) the recipiant addresses to receive a copy. Can be a comma
//** separated addresses.

  var $Cc = null;

//** (String) the recipiant addresses to receive a hidden copy. Can be a
//** comma separated addresses.

  var $Bcc = null;

//** (String) the email address of the message sender.

  var $From = null;

//** (String) the subject of the email message.

  var $Subject = null;

//** (String) body content for the message. Must be plain text or HTML based
//** on the 'TextOnly' field. This field is ignored if
//** SetMultipartAlternative() is called with valid content.

  var $Content = null;

//** an array of EmailAttachment instances to be sent with this message.

  var $Attachments;

//** any custom header information that must be used when sending email.

  var $Headers = null;

//** whether email to be sent is a text email or a HTML email.

  var $TextOnly = true;

//** the charset of the email to be sent (initially none, let type decide).

  var $Charset = null;

//** Create a new email message with the parameters provided.

  function Email($to=null, $from=null, $subject=null, $headers=null)
  {
    $this->To = $to;
    $this->From = $from;
    $this->Subject = $subject;
    $this->Headers = $headers;   

//** create an empty array for attachments. NULL out attachments used for
//** multipart/alternative messages initially.
   
    $this->Attachments = Array();   
    $this->Attachments["text"] = null;
    $this->Attachments["html"] = null;
  }
//** Returns: Boolean
//** Set this email message to contain both text and HTML content.
//** If successful all attachments and content are ignored.

  function SetMultipartAlternative($text=null, $html=null)
  {
//** non-empty content for the text and HTML version is required.

    if(strlen(trim(strval($html))) == 0 || strlen(trim(strval($text))) == 0)
      return false;
    else
    {
//** create the text email attachment based on the text given and the standard
//** plain text MIME type.

      $this->Attachments["text"] = new EmailAttachment(null, "text/plain");
      $this->Attachments["text"]->LiteralContent = strval($text);

//** create the html email attachment based on the HTML given and the standard
//** html text MIME type.

      $this->Attachments["html"] = new EmailAttachment(null, "text/html");
      $this->Attachments["html"]->LiteralContent = strval($html);

      return true;  //** operation was successful.
    }
  }
//** Returns: Boolean
//** Create a new file attachment for the file (and optionally MIME type)
//** given. If the file cannot be located no attachment is created and
//** FALSE is returned.

  function Attach($pathtofile, $mimetype=null)
  {
//** create the appropriate email attachment. If the attachment does not
//** exist the attachment is not created and FALSE is returned.

    $attachment = new EmailAttachment($pathtofile, $mimetype);
    if(!$attachment->Exists())
      return false;
    else
    {
      $this->Attachments[] = $attachment;  //** add the attachment to list.
      return true;                         //** attachment successfully added.
    }
  }
//** Returns: Boolean
//** Determine whether or not the email message is ready to be sent. A TO and
//** FROM address are required.

  function IsComplete()
  {
    return (strlen(trim($this->To)) > 0 && strlen(trim($this->From)) > 0);
  }
//** Returns: Boolean
//** Attempt to send the email message. Attach all files that are currently
//** valid. Send the appropriate text/html message. If not complete FALSE is
//** returned and no message is sent.

  function Send()
  {
    if(!$this->IsComplete())  //** message is not ready to send.
      return false;           //** no message will be sent.

//** generate a unique boundry identifier to separate attachments.

    $theboundary = "-----" . md5(uniqid("EMAIL"));

//** the from email address and the current date of sending.

    $headers = "Date: " . date("r", time()) . EmailNewLine .
               "From: $this->From" . EmailNewLine;

//** if a non-empty CC field is provided add it to the headers here.

    if(strlen(trim(strval($this->Cc))) > 0)
      $headers .= "CC: $this->Cc" . EmailNewLine;
   
//** if a non-empty BCC field is provided add it to the headers here.

    if(strlen(trim(strval($this->Bcc))) > 0)
      $headers .= "BCC: $this->Bcc" . EmailNewLine;

//** add the custom headers here, before important headers so that none are
//** overwritten by custom values.

    if($this->Headers != null && strlen(trim($this->Headers)) > 0)
      $headers .= $this->Headers . EmailNewLine;

//** determine whether or not this email is mixed HTML and text or both.

    $isMultipartAlternative = ($this->Attachments["text"] != null &&
                               $this->Attachments["html"] != null);

//** determine the correct MIME type for this message.

    $baseContentType = "multipart/" . ($isMultipartAlternative ?
                                       "alternative" : "mixed");

//** add the custom headers, the MIME encoding version and MIME typr for the
//** email message, the boundry for attachments, the error message if MIME is
//** not suppported.

    $headers .= "X-Mailer: " . EmailXMailer . EmailNewLine .
                "MIME-Version: 1.0" . EmailNewLine .
                "Content-Type: $baseContentType; " .
                "boundary=\"$theboundary\"" . EmailNewLine . EmailNewLine;

//** if a multipart message add the text and html versions of the content.

    if($isMultipartAlternative)
    {
//** add the text and html versions of the email content.

      $thebody = "--$theboundary" . EmailNewLine .
                  $this->Attachments["text"]->ToHeader() . EmailNewLine .
                 "--$theboundary" . EmailNewLine .
                  $this->Attachments["html"]->ToHeader() . EmailNewLine;
    }
//** if either only html or text email add the content to the email body.

    else
    {
//** determine the proper encoding type and charset for the message body.

      $theemailtype = "text/" . ($this->TextOnly ? "plain" : "html");
      if($this->Charset == null)
        $this->Charset = DefaultCharset;

//** add the encoding header information for the body to the content.

      $thebody = "--$theboundary" . EmailNewLine .
                 "Content-Type: $theemailtype; charset=$this->Charset" .
                  EmailNewLine . "Content-Transfer-Encoding: 8bit" .
                  EmailNewLine . EmailNewLine . $this->Content .
                  EmailNewLine . EmailNewLine;

//** loop over the attachments for this email message and attach the files
//** to the email message body. Only if not multipart alternative.

      foreach($this->Attachments as $attachment)
      {
 //** check for NULL attachments used by multipart alternative emails. Do not
 //** attach these.

        if($attachment != null)
        {
          $thebody .= "--$theboundary" . EmailNewLine .
                       $attachment->ToHeader() . EmailNewLine;
        }
      }
    }
//** end boundry marker is required.

    $thebody .= "--$theboundary--";

//** attempt to send the email message. Return the operation success.

    return mail($this->To, $this->Subject, $thebody, $headers);
  }
}
//******************************************END_EMAIL_MESSAGE_CLASS_DEFINITION
//**!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

//**!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//**EMAIL_ATTACHMENT_CLASS_DEFINITION*****************************************
//** The EmailAttachment class links a file in the file system to the
//** appropriate header to be included in an email message. if the file does
//** not exist the attachment will not be sent in any email messages. It can
//** also be used to generate an attachment from literal content provided.

class EmailAttachment
{
//** (String) the full path to the file to be attached.

  var $FilePath = null;

//** (String) the MIME type for the file data of this attachment.

  var $ContentType = null;

//** binary content to be used instead the contents of a file.

  var $LiteralContent = null;

//** Creates a new email attachment ffrom the file path given. If no content
//** type is given the default 'application/octet-stream' is used.

  function EmailAttachment($pathtofile=null, $mimetype=null)
  {
//** if no MIME type is provided use the default value specifying binary data.
//** Otherwise use the MIME type provided.

    if($mimetype == null || strlen(trim($mimetype)) == 0)
      $this->ContentType = "application/octet-stream";
    else
      $this->ContentType = $mimetype;

    $this->FilePath = $pathtofile;  //** save the path to the file attachment.
  }
//** Returns: Boolean
//** Determine whether literal content is provided and should be used as the
//** attachment rather than a file.

  function HasLiteralContent()
  {
    return (strlen(strval($this->LiteralContent)) > 0);
  }
//** Returns: String
//** Get the binary string data to be used as this attachment. If literal
//** content is provided is is used, otherwise the contents of the file path
//** for this attachment is used. If no content is available NULL is returned.

  function GetContent()
  {
//** non-empty literal content is available. Use that as the attachment.
//** Assume the user has used correct MIME type.

    if($this->HasLiteralContent())
      return $this->LiteralContent;

//** no literal content available. Try to get file data.

    else
    {
      if(!$this->Exists())  //** file does not exist.
        return null;        //** no content is available.
      else
      {
//** open the file attachment in binary mode and read the contents.

        $thefile = fopen($this->FilePath, "rb");
        $data = fread($thefile, filesize($this->FilePath));
        fclose($thefile);
        return $data;
      }
    }
  }
//** Returns: Boolean
//** Determine whether or not the email attachment has a valid, existing file
//** associated with it.

  function Exists()
  {
    if($this->FilePath == null || strlen(trim($this->FilePath)) == 0)
      return false;
    else
      return file_exists($this->FilePath);
  }
//** Returns: String
//** Generate the appropriate header string for this email attachment. If the
//** the attachment content does not exist NULL is returned.

  function ToHeader()
  {
    $attachmentData = $this->GetContent();  //** get content for the header.
    if($attachmentData == null)             //** no valid attachment content.
      return null;                          //** no header can be generted.

//** add the content type and file name of the attachment.

    $header = "Content-Type: $this->ContentType;";

//** if an attachment then add the appropriate disposition and file name(s).
 
    if(!$this->HasLiteralContent())
    {
      $header .= " name=\"" . basename($this->FilePath) . "\"" . EmailNewLine .
                 "Content-Disposition: attachment; filename=\"" .
                  basename($this->FilePath) . "\"";
    }
    $header .= EmailNewLine;

//** add the key for the content encoding of the attachment body to follow.

    $header .= "Content-Transfer-Encoding: base64" . EmailNewLine .
                EmailNewLine;

//** add the attachment data to the header. encode the binary data in BASE64
//** and break the encoded data into the appropriate chunks.

    $header .= chunk_split(base64_encode($attachmentData), 76, EmailNewLine) .
               EmailNewLine;

    return $header;  //** return the headers generated by file.
  }
}
//***************************************END_EMAIL_ATTACHMENT_CLASS_DEFINITION
//**!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
?>


index2.php

Codice:
<?
//** load email class definition.

  include('class.Email.php'); 

//** establish to,from, and any other recipiants.

  $Sender = "stefano.sacco@masteredp.it";
  $Recipiant = "stefano_sacco@yahoo.it";
  $Cc = "them@there.com";
  $Bcc = "";

//** create the HTML versions of the body content here.

 $htmlVersion = "<font face='verdana' color='blue'><b>In allegato la fattura num. del. <br> Distinti
 saluti <br> Masteredp srl</b></font>";

  unset($msg);

//** !!!! SEND AN HTML EMAIL w/ATTACHMENT !!!!
//** create the new message using the to, from, and email subject.

  $msg = new Email($Recipiant, $Sender, "fatt. del. - master edp");
  $msg->Cc = $Cc;
  $msg->Bcc = $Bcc;
//** set the message to be text only and set the email content.

  $msg->TextOnly = false;
  $msg->Content = $htmlVersion;
 
//** attach this scipt itself to the message.

  //$msg->Attach(__FILE__, "text/plain");
   $msg->Attach('/BOLLE/NOV-009000-169-2006-01-23.pdf', "pdf/plain");
 
//** send the email message.

  $SendSuccess = $msg->Send();
 
  echo "HTML email w/attachment was ",
       ($SendSuccess ? "sent" : "not sent"), "<br>";

  unset($msg);

?>



Top
 Profilo  
 
Visualizza ultimi messaggi:  Ordina per  
Apri un nuovo argomento Rispondi all’argomento  [ 8 messaggi ] 

Tutti gli orari sono UTC


Chi c’è in linea

Visitano il forum: Nessuno e 1 ospite


Non puoi aprire nuovi argomenti
Non puoi rispondere negli argomenti
Non puoi modificare i tuoi messaggi
Non puoi cancellare i tuoi messaggi
Non puoi inviare allegati

Cerca per:
Vai a:  
cron
Powered by phpBB® Forum Software © phpBB Group
Traduzione Italiana phpBBItalia.net basata su phpBB.it 2010
[ Time : 0.084s | 13 Queries | GZIP : Off ]