phpmailer attachment code example
Example 1: send attachment in mail php
$filename = 'myfile';
$path = 'your path goes here';
$file = $path . "/" . $filename;
$mailto = '[email protected]';
$subject = 'Subject';
$message = 'My message';
$content = file_get_contents($file);
$content = chunk_split(base64_encode($content));
$separator = md5(time());
$eol = "\r\n";
$headers = "From: name <[email protected]>" . $eol;
$headers .= "MIME-Version: 1.0" . $eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol;
$headers .= "Content-Transfer-Encoding: 7bit" . $eol;
$headers .= "This is a MIME encoded message." . $eol;
$body = "--" . $separator . $eol;
$body .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
$body .= "Content-Transfer-Encoding: 8bit" . $eol;
$body .= $message . $eol;
$body .= "--" . $separator . $eol;
$body .= "Content-Type: application/octet-stream; name=\"" . $filename . "\"" . $eol;
$body .= "Content-Transfer-Encoding: base64" . $eol;
$body .= "Content-Disposition: attachment" . $eol;
$body .= $content . $eol;
$body .= "--" . $separator . "--";
if (mail($mailto, $subject, $body, $headers)) {
echo "mail send ... OK";
} else {
echo "mail send ... ERROR!";
print_r( error_get_last() );
}
Example 2: phpmailer send attachment
$mail->AddAttachment($_FILES['uploaded_file']['tmp_name'], $_FILES['uploaded_file']['name']);
Example 3: phpmailer example php
<?php
require_once('class.phpmailer.php');
$mailer = new PHPMailer();
$mailer->IsSMTP();
$mailer->SMTPDebug = 1;
$mailer->Port = 587;
$mailer->Host = 'smtplw.com.br';
$mailer->SMTPAuth = true;
$mailer->Username = 'smtplocaweb';
$mailer->Password = 'Gwb9etA323';
$mailer->FromName = 'Bart S. Locaweb';
$mailer->From = '[email protected]';
a mesma caixa postal configurada no remetente do SMTP
$mailer->AddAddress('[email protected]','Nome do
destinatário');
$mailer->Subject = 'Teste enviado através do PHP Mailer
SMTPLW';
$mailer->Body = 'Este é um teste realizado com o PHP Mailer
SMTPLW';
if(!$mailer->Send())
{
echo "Message was not sent";
echo "Mailer Error: " . $mailer->ErrorInfo; exit; }
print "E-mail enviado!"
?>