2015-04-08 42 views
-1

我想用電子郵件發送附件,有可能嗎?我的客戶已經通過谷歌註冊了郵件。這是我到目前爲止有:如何用郵件發送附件()

$to = "[email protected]"; 
$subject = "My subject"; 
$txt = "Hello world!"; 
$headers = "From: [email protected]" . "\r\n" . 
"CC: [email protected]"; 

mail($to,$subject,$txt,$headers); 
?> 
+0

最簡單的解決方案是使用PHPMailer。請參閱http://stackoverflow.com/questions/12301358/send-attachments-with-php-mail – christophetd

+0

是的..誰說這是不可能的? – starkeen

回答

0

使用的PHPMailer類,

include 'PHPMailer.php'; 
    $email = new PHPMailer(); 
    $email->From  = '[email protected]'; 
    $email->FromName = 'Your Name'; 
    $email->Subject = 'Message Subject'; 
    $email->Body  = $bodytext; 
    $email->AddAddress('[email protected]'); 

$file_to_attach = 'PATH_OF_YOUR_FILE_HERE'; 

$email->AddAttachment($file_to_attach , 'NameOfFile.pdf'); 

return $email->Send(); 

下載的PHPMailer類,

https://github.com/PHPMailer/PHPMailer 
+0

如果我使用上面的代碼,它會工作嗎? –

+0

是的,當然!!!! –

+0

好吧,讓我看看它 –

0

您可以使用PHPMailer的PHP庫發送電子郵件..
http://phpmailer.worxware.com/

 <?php 
     require "[YOUR PATH TO PHPMAILER LIB]/class.phpmailer.php"; 
     require "[YOUR PATH TO PHPMAILER LIB]/class.smtp.php"; 
    $mail = new PHPMailer; 

      $mail->isSMTP(); // Set mailer to use SMTP 

      $mail->Host = 'mtp.host.com'; // Specify main SMTP servers 
      $mail->SMTPAuth = true; // Enable SMTP authentication 
      $mail->Username = '[email address to send email]'; // SMTP username 
      $mail->Password = '[above email password]'; // SMTP password 
      $mail->SMTPSecure = 'encryption type'; // Enable TLS encryption, `ssl` also accepted 
      $mail->Port = [SMTP PORT]; // TCP port to connect to 

      $mail->From = '[FROM EMAIL]'; 
      $mail->FromName = '[FROM NAME]'; 
      $mail->addAddress([recipient email address], [recipient name]); // Add a recipient 
      $mail->addReplyTo([email protected], reply); 
      $mail->addAttachment([attachment path], [attachment name]);// name 
      $mail->isHTML(true); // Set email format to HTML 
      $mail->Subject = $subject; 
      $mail->Body = $body; 
      $mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; 
      $mail->send(); 
?> 
+0

$ mail-> Host ='mtp.host.com'; //指定主要的SMTP服務器 –