如何通过电子邮件发送上传图片

时间:2017-02-01 21:44:19

标签: php email

基本上,我需要能够在发送电子邮件时发送上传到表单的图像。

正在发生的问题是它发送带有文字的电子邮件但照片没有通过..我的做法有何不同?



$email_to = "email@email.com";
$email_subject = "Subject";
$email_from = "People";
		
$b_frontPhoto = $_FILES['before1']; // required
$b_backPhoto = $_FILES['before2']; // required 
$b_sidePhoto = $_FILES['before3']; // required
$s_Weight = $_POST['startWeight']; // not required
$s_pantSize = $_POST['startPants']; // required
$s_dressSize = $_POST['startDress']; // required
// $why_start = $_POST['bwell_code']; // required

$email_message = "Form details below.<br><br>";

$email_message .= "Name: ".$b_frontPhoto."<br>";
$email_message .= "Address: ".$b_backPhoto."<br>";
$email_message .= "City: ".$b_sidePhoto."<br>";
$email_message .= "State: ".$s_Weight."<br>";
$email_message .= "Zip: ".$s_pantSize."<br>";
$email_message .= "Email: ".$s_dressSize."<br>";
// $email_message .= "Redemption_Code: ".$b_redemption."<br>";
	
sendEmail($email_to,$email_subject,$email_message,'email@email.com');
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

我认为你应该花一些时间研究PHPMailer,因为它将使你的生活从现在开始变得更加容易。它修复了PHP mail()所带来的大量问题,例如添加了轻松添加附件的功能,而且使用起来非常简单。

PHPMailer

在以下示例中,您将通过调用addAttachment并提供图片网址来附加图片,该网址可能是您刚上传的文件:

<?php
require 'PHPMailerAutoload.php';

$mail = new PHPMailer;

$mail->setFrom('from@example.com', 'Mailer');
$mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
$mail->addAddress('ellen@example.com');               // Name is optional
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');

$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

编辑:

基于您正在使用PHPMailer的注释已经如此改变sendEmail函数:

function sendEmail($email,$subject,$content,$sent_from_email_address,$b_frontPhot‌​o,$b_backPhoto,$b_si‌​dePhoto) {
///////////////////////////////// Passed the photos to the function ^^^^^^^^^^^^^^^^^^^^^

        $b_frontPhot‌​o = $b_frontPhot‌​o['tmp_name'];
        $b_backPhot‌​o = $b_backPhot‌​o['tmp_name'];
        $b_sidePhot‌​o = $b_sidePhot‌​o['tmp_name'];
//^^^^^^ Try adding these in ^^^^^^^^^^^^^^^^^^
        $mail = new PHPMailer();  
        $mail->IsSMTP();  
        $mail->Host = "stuff";  
        $mail->SMTPAuth = true;      
        $mail->Username = "stuuff";
        $mail->Password = "stuff";
        $mail->Port = "stuff";       
        $mail->setFrom($sent_from_email_address, "stuff");
        $mail->Encoding = "stuff";  
        $mail->Subject = $subject;
        $mail->msgHTML($content); 
        $mail->AddAddress($email); 

        // Attach the files to the email
        $mail->addAttachment($b_frontPhoto);
        $mail->addAttachment($b_backPhoto);
        $mail->addAttachment($b_sidePhoto);


        if (!$mail->Send()) 
            return 0;
        else
            return 1;
}///// close function /////