'How to send upload image through email
I need to be able to send image that is uploaded into form when hit submit it sends email.
The problem that is happening is it sends email with text but photos are not going through. What do I do differently?
$email_to = "[email protected]";
$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 protected]');
Solution 1:[1]
I think you should really invest some time looking into PHPMailer as it will make your life much easier from now on. It fixes tons of problems that PHP mail() has, such as adding the ability to easily add an attachment, and it's extremely simple to use.
In the following example, you'd attach your image by calling the addAttachment and supplying your image URL, which can be the file you just uploaded:
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->setFrom('[email protected]', 'Mailer');
$mail->addAddress('[email protected]', 'Joe User'); // Add a recipient
$mail->addAddress('[email protected]'); // Name is optional
$mail->addReplyTo('[email protected]', 'Information');
$mail->addCC('[email protected]');
$mail->addBCC('[email protected]');
$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';
}
EDIT:
Based off the comments you are using PHPMailer already so alter the sendEmail function like so:
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 /////
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |