【问题标题】:PHP email form WITH ATTACHMENT (with or without PHPmailer)带附件的 PHP 电子邮件表单(带或不带 PHPmailer)
【发布时间】:2015-05-05 02:56:23
【问题描述】:

我正在使用以下 PHP 代码,该代码从 HTML 表单中获取值并以 HTML 格式发送电子邮件。这对我来说很好。

现在我需要添加一个附件字段,它将附件与电子邮件一起发送(无论是否将文件存储在服务器上)。有人可以建议如何使用或不使用 PHPmailer 吗?

谢谢!

<?php

if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['address'])) {
    die('Error: Missing variables');
}

$name=$_POST['name'];
$mobile=$_POST['mobile'];
$position=$_POST['position'];
$email=$_POST['email'];
$message=$_POST['message'];

$ip=$_SERVER['REMOTE_ADDR'];

$to="email@server.com";

$headers = "MIME-Version: 1.0\r\n";  
$headers .= "Content-type: text/plain; charset=utf-8\r\n";  
$headers = 'From: '.$_POST['name'].' <'.$_POST['email'].'>';

    'X-Mailer: PHP/' . phpversion();

$subject='Job Application from '.$name."\n\n\n";
$body.='Name: <b>'.$name."</b><br>\n";
$body.='Mobile No: <b>'.$mobile."</b><br>\n";
$body.='Position: <b>'.$position."</b><br>\n";
$body.='Email: <b>'.$email."</b><br>\n";
$body.='Message: <b>'.$message."</b><br>\n";

$body.='IP address of the submitter: '."\n".$ip."\n";

$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: '.$email."\r\n";

if(mail($to, $subject, $body, $headers)) {
    header("Location: thank-you.html"); 
} else {
    echo "Something has gone wrong! Please try again!"; 
}

?>

【问题讨论】:

  • 这段代码有漏洞,容易受到攻击。您应该从随 PHPMailer 提供的this example 开始,它完全符合您的要求。
  • 感谢 Synchro,我试过了,但没有用。它说——邮件程序错误:无法实例化邮件功能。有什么想法吗?
  • 阅读the troubleshooting guide。这意味着您没有本地邮件服务器或您的 PHP 配置错误。您可以切换到 SMTP,请参阅其他示例。
  • 感谢 Synchro... 但是我已经放弃了 phpmailer,它对于一个附加该死文件的小功能来说太复杂了。他们不是上传和发送电子邮件附件的东方方式吗?
  • 这确实是极简、琐碎的代码,也很清晰、安全且经过测试,但以这样的态度你不太可能得到太多帮助或同情。

标签: php forms phpmailer


【解决方案1】:

使用 PHPMailer 更容易.. 您可以获取文档和教程here。 这是基本邮件和附加文件的代码:

<?php
require_once('../class.phpmailer.php');

$mail             = new PHPMailer(); // defaults to using php "mail()"

$body             = file_get_contents('contents.html');
$body             = eregi_replace("[\]",'',$body);

$mail->AddReplyTo("name@yourdomain.com","First Last");

$mail->SetFrom('name@yourdomain.com', 'First Last');

$mail->AddReplyTo("name@yourdomain.com","First Last");

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

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

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

$mail->MsgHTML($body);

$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!";
}

希望这会有所帮助。

【讨论】:

  • 这是附加文件的行 - $mail->AddAttachment("images/phpmailer.gif");
  • 该文件必须在您的服务器上。
  • 谢谢阿伦。但是如何从 获取值,因为表单位于 HTML 页面中。
  • 这不能回答问题,并且该链接(和代码)已经过时了。 PHPMailer 现在生活在here
  • 嘿,Synchro 哥们...你能帮忙提供一个更准确的答案吗?
【解决方案2】:
$filePath = 'foldername/attachment.zip';

$file = fopen($filePath,'rb'); 
$data = fread($file,filesize($filePath)); 
fclose($file);
$data = chunk_split(base64_encode($data));

Content-Type: application/zip; name="attachment.zip"  
Content-Transfer-Encoding: base64  
Content-Disposition: attachment 

$data

在“if(mail($to, $subject, $body, $headers)) {”这一行之前添加此代码。

【讨论】:

  • 这没有回答所提出的问题。
  • 应该是这种格式吗? – $headers = 'MIME 版本:1.0'。 "\r\n"; $headers .= '内容类型:文本/html;字符集=iso-8859-1' 。 "\r\n"; $headers .= '发件人:'.$email."\r\n";否则会在 Dreamweaver 中给我一个错误。
  • $headers = 'MIME-Version: 1.0' 。 "\r\n"; $headers .= '内容类型:文本/html;字符集=iso-8859-1' 。 "\r\n"; $headers .= '发件人:'.$email."\r\n";将提到此代码,并且我的代码将在下面的标题中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多