2016-04-12 32 views
0

我用下面的SMTP郵件代碼發送音頻附件:PHP附加音頻文件通過電子郵件

<?php 

session_start(); 

$title = $_POST['title']; 
$first_name = $_POST['name']; 
$last_name = $_POST['lastname']; 
$email_from = $_POST['email']; 
$scaptcha = strtolower($_POST['scaptcha']); 
if ($scaptcha != $_SESSION['captcha']) { 
    echo 'You have entered wrong captcha'; 
    exit(0); 
} 

require('./class.phpmailer.php'); 

function clean_string($string) { 
    $bad = array("content-type", "bcc:", "to:", "cc:", "href"); 
    return str_replace($bad, "", $string); 
} 

$email_message = ""; 
$email_message .= "Title: "  . clean_string($title)  . "\n"; 
$email_message .= "First Name: " . clean_string($first_name) . "\n"; 
$email_message .= "Last Name: " . clean_string($last_name) . "\n"; 
$email_message .= "Email: "  . clean_string($email_from) . "\n"; 

$allowedExts = array("mp3","wav","dss"); 
$temp = explode(".", $_FILES["file"]["name"]); 
$extension = end($temp); 
if ((($_FILES["file"]["type"] == "audio/mpeg")) && in_array($extension, $allowedExts)) { 
    if ($_FILES["file"]["error"] > 0) { 
     echo "<script>alert('Error: " . $_FILES["file"]["error"] . "')</script>"; 
    } else { 
     $d = 'Audio/Uploads/'; 
     $de = $d . basename($_FILES['file']['name']); 
     move_uploaded_file($_FILES["file"]["tmp_name"], $de); 
     $fileName = $_FILES['file']['name']; 
     $filePath = $_FILES['file']['tmp_name']; 
    } 
} else { 
    echo "<script>alert('Invalid file')</script>"; 
} 

$headers = 'From: ' . $email_from . "\r\n" . 
      'Reply-To: ' . $email_from . "\r\n" . 
      'X-Mailer: PHP/' . phpversion(); 
$mail = new PHPMailer(); 
$mail->IsSMTP(); 
$mail->SMTPDebug = 0; 
$mail->Debugoutput = 'html'; 
$mail->Host = "smtp.gmail.com"; 
$mail->Port = 25; 
$mail->SMTPAuth = true; 
$mail->Username = "[email protected]"; 
$mail->Password = "*****"; 
$mail->SetFrom($email_from, $first_name . ' ' . $last_name); 
//$mail->AddReplyTo('[email protected]','First Last'); 
$mail->AddAddress('[email protected]', 'Saravana'); 
$mail->Subject = 'New audio file received'; 
$mail->MsgHTML($email_message); 
$mail->AltBody = 'This is a plain-text message body'; 
$mail->AddAttachment($_FILES['file']['tmp_name'], $_FILES['file']['name']); 

if (!$mail->Send()) { 
    echo "<script>alert('Mailer Error: " . $mail->ErrorInfo . "')</script>"; 
} else { 
    echo "<script>alert('Your request has been submitted. We will contact you soon.')</script>"; 
    Header('Location: contact.php'); 
} 

?> 

請幫我解決這個問題。我一直在嘗試這一個多星期。我仍然沒有明白。我也嘗試過PHP郵件程序。這也不起作用。

更新:我收到以下錯誤:

Mailer Error: The following From address failed: [email protected] : Called MAIL FROM without being connected,

+0

問題出現在這一行:'$ mail-> AddAttachment($ _ FILES ['file'] ['tmp_name'],$ _FILES ['file'] ['name']);' – Jer

+0

呃..使用'PHPMailer'是一個非常好的練習點擊這裏鏈接:https://github.com/PHPMailer/PHPMailer –

+0

它正在被使用 - $ mail = new PHPMailer(); – weaveoftheride

回答

0

發送音頻文件鏈接的消息,而不是內嵌附件的。

$ mail-> AddAttachment方法用於內聯附件。

由於文件加密和文件大小,最大服務器不允許發送音頻,視頻或zip文件的內聯附件。

0

Well..It的真的很不容易,而使用PHPMailer。這裏是代碼添加任何東西:

PHPMailer的鏈接:https://github.com/PHPMailer/PHPMailer

您可以添加附件,如:

$mail->addAttachment('/var/tmp/file.tar.gz');   // Add attachments 

雖然這裏是完整的代碼:

<?php 
require 'PHPMailerAutoload.php'; 

$mail = new PHPMailer; 

//$mail->SMTPDebug = 3;        // Enable verbose debug output 

$mail->isSMTP();          // Set mailer to use SMTP 
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers 
$mail->SMTPAuth = true;        // Enable SMTP authentication 
$mail->Username = '[email protected]';     // SMTP username 
$mail->Password = 'secret';       // SMTP password 
$mail->SMTPSecure = 'tls';       // Enable TLS encryption, `ssl` also accepted 
$mail->Port = 587;         // TCP port to connect to 

$mail->setFrom('[email protected]', 'Mailer'); 
$mail->addAddress('[email protected]', 'Joe User');  // Add a recipient 
$mail->addAddress('[email protected]');    // Name is optional 
$mail->addReplyTo('[email protected]', 'Information'); 
$mail->addCC('[email protected]'); 
$mail->addBCC('[email protected]'); 

$mail->addAttachment('/var/tmp/file.tar.gz');   // Add attachments 
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name 
$mail->isHTML(true);         // Set email format to HTML 

$mail->Subject = 'Here is the subject'; 
$mail->Body = 'This is the HTML message body <b>in bold!</b>'; 
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; 

if(!$mail->send()) { 
    echo 'Message could not be sent.'; 
    echo 'Mailer Error: ' . $mail->ErrorInfo; 
} else { 
    echo 'Message has been sent'; 
} 
+0

我試過這個code.Getting這個錯誤---消息可能未發送。郵件錯誤:以下發件人地址失敗:[email protected]:郵件發送失敗,需要身份驗證。有關詳情,請訪問https://support.google.com/mail/answer/14257 a22sm42385348pfj.2 - gsmtp,530,5.5.1 –

+0

@saravananam:好像您的代碼可能正常工作,但您登錄時遇到問題你的Gmail帳戶..! –

+0

是的。我收到一封電子郵件,告訴我登錄嘗試無效。請告訴我如何解決此問題? –

相關問題