使用此代码使用PHP在电子邮件中发送附件。
因此,我只收到了我的电子邮件,但我无法通过附件收到该邮件。
这是我的PHP代码:
<?php
if($_POST && isset($_FILES['attachment'])){
$from_email = 'someone@gmail.com'; //sender email
$recipient_email = 'me@gmail.com'; //recipient email
$subject = 'Test mail'; //subject of email
$message = 'This is body of the message'; //message body
$file_name = $_FILES['attachment']['name'];
$temp_name = $_FILES['attachment']['temp_name'];
$file_type = $_FILES['attachment']['type'];
$file_size = $_FILES['attachment']['size'];
//get base ext of file
$base = basename($file_name);
$extention = substr($base, strlen($base)-4, strlen($base));
//only these files type will allowed
$allowed_extentions = array(".doc", "docx", ".pdf", ".zip", ".png", ".xlsx");
/* check that file type is allowed */
if(in_array($extention, $allowed_extentions )) {
$user_email = filter_var($_POST["email"], FILTER_SANITIZE_EMAIL);
if($file_error>0)
{
die('upload error');
}
/* things you need */
$file = $temp_name ;
$content = chunk_split(base64_encode( file_get_contents($file) )) ;
$uid = md5(uniqid(time()));
//header
$header .= "MIME-Version: 1.0\r\n";
$header .= "From:".$from_email."\r\n";
$header .= "Reply-To: ".$user_email."" . "\r\n";
$header .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n";
/* declaring we have multiple kinds of email */
$header .= "Content-Type: multipart/mixed; boundary=\"" .$uid."\"\r\n\r\n";
$header .= "This is multipart message in MIME formate.\r\n";
/* plain text part */
$header .= "--".$uid."\r\n";
$header .= "Content-Type:text/plain; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding : 7bit\r\n\r\n";
$header .= $message."\r\n\r\n";
/* file attachment */
$header .= "--".$uid."\r\n";
$header .= "Content-Type: ".$file_type."; name=\"".$file_name."\"\r\n";
$header .= "ContentTransfer-Encoding:base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$file_name."\"\r\n";
$header .= $content."\r\n\r\n";
require_once "Mail.php";
/* mail setup recipients, subject etc */
$name = 'sanju'; //$_POST['name'];
$email = $from_email ; // $_POST['email'];
$subject = 'Test mail'; //$_POST['subject'];
//$comments = 'This is body of the message'; //$_POST['message'];
$recipients = 'me@gmail.com';
$header = array (
'From' => "someone@gmail.com",
'Content-type' => "text/html;charset=UTF-8",
'Reply-To' => "someone@gmail.com",
'To' => "contact@domain.com",
);
$mailmsg="Name : $name \n\n Email :$email \n\n Message : $message ";
/* SMTP server name, port, user/passwd */
$smtpinfo["host"] = "ssl://lnxind3.cloudhostdns.net";
$smtpinfo["port"] = "465";
$smtpinfo["auth"] = true;
$smtpinfo["username"] = "contact@net-lytics.com";
$smtpinfo["password"] = "sanjeev12@";
/* Create the mail object using the Mail::factory method */
$mail_object =& Mail::factory("smtp", $smtpinfo);
/* Ok send mail */
echo $mail_object->send($recipients, $header, $mailmsg);
$msg = " Thank you for contacting us. .";;
} else {
echo " File type not allowed";
}
}
?>
HTML表单:
<form name="form1" enctype="multipart/form-data" method="post" action="">
<label>Your Name
<input type="text" name="name" />
</label>
<label>Your Email
<input type="email" name="email" />
</label>
<label>Attachment
<input type="file" name="attachment" />
</label>
<label>
<input type="submit" name="button" value="Submit" />
</label>
</form>