我正在尝试使用PHP mail()从HTML表单发送带有附件的电子邮件。
我在网上找到了一个有效的脚本-但有一个问题:
如果电子邮件中附加了文件,则脚本可以正常工作。但是,如果没有附件,则发送电子邮件,但不包含任何文本内容(空白电子邮件正文)。
我希望添加附件的功能是可选的。
我的问题是,无论有没有附件,如何成功发送电子邮件?
谢谢!
我正在使用的PHP脚本如下:
<?php
$email = trim($_POST['email']);
$email_san = filter_var($email, FILTER_SANITIZE_EMAIL);
$fname = $_POST['first_name'];
$fname_san = filter_var($fname,FILTER_SANITIZE_STRING);
$lname = $_POST['last_name'];
$lname_san = filter_var($lname,FILTER_SANITIZE_STRING);
$org = trim($_POST['organisation']);
$org_san = filter_var($org,FILTER_SANITIZE_STRING);
$user_phone = $_POST['phone'];
$trim_phone = trim($user_phone);
$replace_phone = preg_replace('/[^0-9+-]/', '', $trim_phone);
$phone_san = filter_var($replace_phone,FILTER_SANITIZE_NUMBER_INT);
$message = $_POST['message'];
$fromemail = $email_san;
$subject="Inquiry";
$email_message = '<p><b>First Name:</b> '.$fname_san.'</p>
<p><b>Organisation:</b> '.$org_san.'</p>
<p><b>Email:</b> '.$email_san.'</p>
<p><b>Phone:</b> '.$phone_san.'</p>
<p><b>Message:</b><br/>'.$message.'</p>';
$semi_rand = md5(uniqid(time()));
$headers = "From: ".$fromemail;
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
if($_FILES["file"]["name"]!= ""){
$strFilesName = $_FILES["file"]["name"];
$strContent = chunk_split(base64_encode(file_get_contents($_FILES["file"]["tmp_name"])));
$email_message .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$email_message .= "\n\n";
$email_message .= "--{$mime_boundary}\n" .
"Content-Type: application/octet-stream;\n" .
" name=\"{$strFilesName}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$strContent .= "\n\n" .
"--{$mime_boundary}--\n";
}
$toemail="email@somedomain.com";
if(mail($toemail, $subject, $email_message, $headers)){
echo "Email sent.";
}else{
echo "Email NOT sent.";
}
?>
/ ***更新*** /
好吧,在得知PHPMailer是发送电子邮件的更好方法的建议之后,我尝试了另一种设置PHPMailer的尝试。
我没有Composer,所以我手动下载并安装了PHPMailer。我没有Composer,所以我手动下载并安装了PHPMailer。我的服务器文件夹层次结构显示在this image中。
我在网上找到了一个PHPMailer教程,该教程提供了一个简单的脚本,我将其保存为“ mailer-test.php”,并上传并链接到我的PHPMailer安装:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require_once "php-mailer/src/Exception.php";
//PHPMailer Object
$mail = new PHPMailer(true); //Argument 'true' in enables exceptions
//From email address and name
$mail->From = "me@mydomain.com";
$mail->FromName = "Full Name";
//To address and name
//$mail->addAddress("recipient@somedomain.com", "Recepient Name");
$mail->addAddress("recipient@somedomain.com"); //Recipient name is optional
//Address to which recipient will reply
$mail->addReplyTo("me@mydomain.com", "Reply");
//Send HTML or Plain Text email
$mail->isHTML(true);
$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body in HTML</i>";
$mail->AltBody = "This is the plain text version of the email content";
try {
$mail->send();
echo "Message has been sent successfully";
} catch (Exception $e) {
echo "Mailer Error: " . $mail->ErrorInfo;
}
?>
但是,当我在浏览器中查看“ mailer-test.php”时,出现以下服务器错误:
此页面无效。 yourdomain.com当前无法处理此请求。 HTTP错误
是什么原因造成的?
答案 0 :(得分:1)
您的满足感看起来有些奇怪(他错过了第39行的$) 但是要变得更加简单,请使用PhpMailer类
通过发送邮件使您的生活变得轻松
答案 1 :(得分:0)
确保将enctype='multipart/form-data'
添加到表单标签:
<form action='index.php' method='POST' enctype='multipart/form-data'>
...
</form>