Emails con PHP
Hola:
Me gustaría saber como poder enviar un email con un fichero adjunto con php. He mirado en varios sitios y no consigo hacerlo funcionar. Envía el correo pero no aparecen los archivos adjuntos.
Gracias
Me gustaría saber como poder enviar un email con un fichero adjunto con php. He mirado en varios sitios y no consigo hacerlo funcionar. Envía el correo pero no aparecen los archivos adjuntos.
Gracias
Aqui esta este codigo, yo ya lo probe si funciona.
function sendmail ($from_name, $from_email, $to_name, $to_email, $subject, $text_message="", $html_message, $attachment="")
{
$from = "$from_name <$from_email>";
$to = "$to_name <$to_email>";
$main_boundary = "----=_NextPart_".md5(rand());
$text_boundary = "----=_NextPart_".md5(rand());
$html_boundary = "----=_NextPart_".md5(rand());
$headers = "From: $fromn";
$headers .= "Reply-To: $fromn";
$headers .= "MIME-Version: 1.0n";
$headers .= "Content-Type: multipart/mixed; boundary=".$main_boundary."n";
$message .= "n--$main_boundaryn";
$message .= "Content-Type: multipart/alternative; boundary=".$text_boundary."n";
$message .= "n--$text_boundaryn";
$message .= "Content-Type: text/plain; charset="ISO-8859-1"n";
$message .= "Content-Transfer-Encoding: 7bitnn";
$message .= ($text_message!="")?"$text_message":"Text portion of HTML Email";
$message .= "n--$text_boundaryn";
$message .= "Content-Type: multipart/related; boundary=".$html_boundary."n";
$message .= "n--$html_boundaryn";
$message .= "Content-Type: text/html; charset="ISO-8859-1"n";
$message .= "Content-Transfer-Encoding: quoted-printablenn";
$message .= str_replace ("=", "=3D", $html_message)."n";
if (isset ($attachment) && $attachment != "" && count ($attachment) >= 1)
{
for ($i=0; $i<count ($attachment); $i++)
{
$attfile = $attachment[$i];
$file_name = basename ($attfile);
$fp = fopen ($attfile, "r");
$fcontent = "";
while (!feof ($fp))
{
$fcontent .= fgets ($fp, 1024);
}
$fcontent = chunk_split (base64_encode($fcontent));
@fclose ($fp);
$message .= "n--$html_boundaryn";
//$message .= "Content-Type: application/octetstreamn";
$message .= "Content-Type: image/jpegn";
$message .= "Content-Transfer-Encoding: base64n";
$message .= "Content-Disposition: inline; filename=".$file_name."n";
$message .= "Content-ID: <$file_name>nn";
$message .= $fcontent;
}
}
$message .= "n--$html_boundary--n";
$message .= "n--$text_boundary--n";
$message .= "n--$main_boundary--n";
$enviado=mail ($to, $subject, $message, $headers);
}
/**/
$from_name = "From";
# Sender Email
$from_email = "[email protected]";
# Recipient Name
$to_name = "To";
# Recipient Email
$to_email = "[email protected]";
# Email Subject
$subject = "Subject";
# Text Portion
$text_message = "This is HTML email and your email client softawre ain't support HTML email.";
# HTML Portion
$html_message="";
$html_message = "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">n";
$html_message.= "<html><head><title></title>n";
$html_message.= "<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">n";
$html_message.= "<style type="text/css">n";
$html_message.= "body, td {nfont-family: Trebuchet MS;nfont-size: 12px;n}n";
$html_message.= "</style>n";
$html_message.= "</head>n";
$html_message.= "<body>";
$html_message.= "<img src="chiva.jpg"><br>n";
$html_message.= "</body></html>";
# Attachment Location
$attachment = array("file.jpg", "file2.doc");
# Execute SendMail Function
sendmail ($from_name, $from_email, $to_name, $to_email, $subject, $text_message, $html_message, $attachment);
function sendmail ($from_name, $from_email, $to_name, $to_email, $subject, $text_message="", $html_message, $attachment="")
{
$from = "$from_name <$from_email>";
$to = "$to_name <$to_email>";
$main_boundary = "----=_NextPart_".md5(rand());
$text_boundary = "----=_NextPart_".md5(rand());
$html_boundary = "----=_NextPart_".md5(rand());
$headers = "From: $fromn";
$headers .= "Reply-To: $fromn";
$headers .= "MIME-Version: 1.0n";
$headers .= "Content-Type: multipart/mixed; boundary=".$main_boundary."n";
$message .= "n--$main_boundaryn";
$message .= "Content-Type: multipart/alternative; boundary=".$text_boundary."n";
$message .= "n--$text_boundaryn";
$message .= "Content-Type: text/plain; charset="ISO-8859-1"n";
$message .= "Content-Transfer-Encoding: 7bitnn";
$message .= ($text_message!="")?"$text_message":"Text portion of HTML Email";
$message .= "n--$text_boundaryn";
$message .= "Content-Type: multipart/related; boundary=".$html_boundary."n";
$message .= "n--$html_boundaryn";
$message .= "Content-Type: text/html; charset="ISO-8859-1"n";
$message .= "Content-Transfer-Encoding: quoted-printablenn";
$message .= str_replace ("=", "=3D", $html_message)."n";
if (isset ($attachment) && $attachment != "" && count ($attachment) >= 1)
{
for ($i=0; $i<count ($attachment); $i++)
{
$attfile = $attachment[$i];
$file_name = basename ($attfile);
$fp = fopen ($attfile, "r");
$fcontent = "";
while (!feof ($fp))
{
$fcontent .= fgets ($fp, 1024);
}
$fcontent = chunk_split (base64_encode($fcontent));
@fclose ($fp);
$message .= "n--$html_boundaryn";
//$message .= "Content-Type: application/octetstreamn";
$message .= "Content-Type: image/jpegn";
$message .= "Content-Transfer-Encoding: base64n";
$message .= "Content-Disposition: inline; filename=".$file_name."n";
$message .= "Content-ID: <$file_name>nn";
$message .= $fcontent;
}
}
$message .= "n--$html_boundary--n";
$message .= "n--$text_boundary--n";
$message .= "n--$main_boundary--n";
$enviado=mail ($to, $subject, $message, $headers);
}
/**/
$from_name = "From";
# Sender Email
$from_email = "[email protected]";
# Recipient Name
$to_name = "To";
# Recipient Email
$to_email = "[email protected]";
# Email Subject
$subject = "Subject";
# Text Portion
$text_message = "This is HTML email and your email client softawre ain't support HTML email.";
# HTML Portion
$html_message="";
$html_message = "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">n";
$html_message.= "<html><head><title></title>n";
$html_message.= "<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">n";
$html_message.= "<style type="text/css">n";
$html_message.= "body, td {nfont-family: Trebuchet MS;nfont-size: 12px;n}n";
$html_message.= "</style>n";
$html_message.= "</head>n";
$html_message.= "<body>";
$html_message.= "<img src="chiva.jpg"><br>n";
$html_message.= "</body></html>";
# Attachment Location
$attachment = array("file.jpg", "file2.doc");
# Execute SendMail Function
sendmail ($from_name, $from_email, $to_name, $to_email, $subject, $text_message, $html_message, $attachment);