Attaching file in the php mail code
This code is working fine in my unix shared hosting account but how do I attach file with it? I've removed the hostname, login, passwd etc.
<?php
require_once "Mail.php";
$from = "contactus@site.com";
$to = "to email id";
$subject = "this is the message from your domain";
$body = "give your message body here";
$host = "mail.site.com";
$username = "user";
$password = "pass123";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password))开发者_StackOverflow中文版;
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) { echo("" . $mail->getMessage() . "");
} else {
echo("Message Sent successfully ");
}
?>
You have to include both PEAR::Mail and PEAR::Mail_Mime, as it takes both classes to get the full email sent. Example...
include_once('Mail.php');
include_once('Mail_Mime/mime.php');
// include_once('Mail/mime.php');
// The code below composes and sends the email
$message = new Mail_mime();
$message->setTXTBody($text);
$message->addAttachment($path_of_attachment_file);
$body = $message->get();
$extraheaders = array("From"=>$from, "Subject"=>$subject,"Reply-To"=>$visitor_email);
$headers = $message->headers($extraheaders);
$mail = Mail::factory("mail");
$mail->send($to, $headers, $body);
精彩评论