如何在邮件中将图像作为附件发送

时间:2015-05-28 13:12:53

标签: php html image email send

我需要帮助html表单通过PHP发送邮件与图像附件,我尝试简单的代码和消息发送,有所有文本甚至图像名称但没有实际图像,我不完全确定如何做它正确地尝试了许多代码,但没有一个有效。我很可惜,如果这已经得到了解答,但我搜索了几个小时,却找不到任何东西。

这是我的HTML

<div class="uk-alert uk-alert-large uk-alert-success" style="float: right; width: 25%; padding: 30px;">Registration for competitors
<form action=".../registration.php" method="POST" enctype="multipart/form-data">
<br />Full Name:<br /> <input type="text" name="name" required/> 
<br /> Country: <br />*At least one team member has to have the citizenship of the chosen country.
<br /><input type="text" name="krajina" required/> <br /> Full name of your team mate:<br />
<input type="text" name="tym" required/> 
<br /> Email:<br /> 
<input type="text" name="email" required/> At least 3 pictures of your previous work: 
<input id="fileToUpload" placeholder="Select an image to Upload" type="file" name="fileToUpload" required/> 

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$tym = $_POST['tym'];
$krajina = $_POST['krajina'];
$target_dir = "images/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
$formcontent=" Uzivatel: $name \ email: $email \ jeho kolega: $tym \ z krajiny: $krajina \ sa hlasi na event Tatry ice master chalenge 2016 \ $target_file" . "\r\n";
$recipient = "test@mail.com";
$subject = "Registracia na Specialny Event";
$mailheader =  "From: $email\nMIME-Version: 1.0\nContent-Type: text/html; charset=utf-8\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo header("Location: http://www.iceart.club/special-events");
die();
?>
谢谢大家的帮助

2 个答案:

答案 0 :(得分:0)

尝试此链接here

使用PHPMailer:

  • 从此处下载PHPMailer脚本:http://github.com/PHPMailer/PHPMailer

  • 提取存档并将脚本的文件夹复制到项目中方便的位置。

  • 包含主脚本文件 - require_once('path / to / file / class.phpmailer.php');

现在,发送附带电子邮件的过程非常困难,非常简单:

$email = new PHPMailer();
$email->From      = 'you@example.com';
$email->FromName  = 'Your Name';
$email->Subject   = 'Message Subject';
$email->Body      = $bodytext;
$email->AddAddress( 'destinationaddress@example.com' );

$file_to_attach = 'PATH_OF_YOUR_FILE_HERE';

$email->AddAttachment( $file_to_attach , 'NameOfFile.pdf' );

return $email->Send();

这只是一行$ email-&gt; AddAttachment(); - 你不能要求更容易。

如果你使用PHP的mail()函数,你将编写代码堆栈,你可能会有很多很难找到错误。

答案 1 :(得分:0)

$mail->AddEmbeddedImage('img/2u_cs_mini.jpg', 'logo_2u');

并在标签上输入src ='cid:logo_2u'