上传文件并用PHP发送电子邮件?

时间:2012-05-07 21:32:59

标签: php html email-attachments

上传文件的最简单方法是:<input type="file" name="clanlogo">并通过电子邮件发送至“12345@email.com”我已经研究过,但发现了超级复杂的代码和内容。我需要它作为附件发送。在PHP中。这是我到目前为止的代码:

<?php

## CONFIG ##

# LIST EMAIL ADDRESS
$recipient = "my email Here";

# SUBJECT (Contact Form/Remove)
$subject = "Volunteer Form";

# RESULT PAGE
$location = "thank_you.html";

## FORM VALUES ##

# SENDER
$email = $_REQUEST['email'] ;

# MAIL BODY
$body .= "First Name: ".$_REQUEST['first_name']." \n";

$body .= "Last Name: ".$_REQUEST['last_name']." \n";

$body .= "Email: ".$_REQUEST['email']." \n";

$body .= "Volunteer Choice: ".$_REQUEST['dropdown']." \n";

$body .= "Questions: ".$_REQUEST['comments']." \n";
# add more fields here if required

## SEND MESSGAE ##

我只需要添加一些代码即可上传文件。

2 个答案:

答案 0 :(得分:1)

使用html表单并添加标记<input type="file" name="uploaded">,然后您可以像这样处理PHP文件:

<?php

$file = $_FILES['uploaded'];
if (move_uploaded_file($file['tmp_name'], $destination)) {
// do something
}
// do something

?>

我不能写一个巨大的代码,这应该是一个推动你开始!这是一个快速guide。现在要附加它,您可以将此作为对服务器的ajax请求,存储文件的路径,然后在发送电子邮件时再次从数据库中提取路径。

答案 1 :(得分:0)

下载PHPMailer课程并尝试此操作...

<html>
<head>
<title>PHPMailer - Mail() basic test</title>
</head>
<body>

<?php

require_once('class.phpmailer.php');

$mail = new PHPMailer();     
$mail->SetFrom('name@yourdomain.com', 'First Last');
$mail->AddReplyTo("name@yourdomain.com","First Last");

$address = "12345@email.com";
$mail->AddAddress($address, "John Doe");

$mail->Subject    = "PHPMailer Test Subject via mail()";

$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional

$mail->MsgHTML($body);

move_uploaded_file($_FILES["file"]["tmp_name"],$destination);

$mail->AddAttachment("$destination");  //Your attachment
//$mail->AddAttachment("images/phpmailer.gif");      // attachment
//$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment

if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {
  echo "Message sent!";
}

?>

</body>
</html>
  • 这是来自PHPMailer Class
  • 的示例