我使用以下SMTP邮件代码发送音频附件:
<?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 = "saro17.ams@gmail.com";
$mail->Password = "*****";
$mail->SetFrom($email_from, $first_name . ' ' . $last_name);
//$mail->AddReplyTo('replyto@example.com','First Last');
$mail->AddAddress('saro17.ams@gmail.com', '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错误:以下发件人地址失败:saro17.ams@gmail.com:在未连接的情况下调用MAIL FROM,
在消息中发送音频文件链接,而不是内联附件。
$mail->AddAttachment方法用于内联附件。
由于文件加密和文件大小的原因,最大服务器不允许发送音频、视频或zip文件的内联附件。
嗯。。在使用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 = 'user@example.com'; // 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('from@example.com', 'Mailer');
$mail->addAddress('joe@example.net', 'Joe User'); // Add a recipient
$mail->addAddress('ellen@example.com'); // Name is optional
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');
$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';
}