glitchbox
glitchbox

Reputation: 133

Sending an email with an attachment via PHPMailer

I'm having trouble getting PHPMailer to send out an email with an image attachment that a user submits. I just keep getting "Mail Error" everytime it tries to send. I just can't see what is going wrong. There's no errors in the error_log, it just fails everytime.

HTML Input dealing with image attachment:

<input type="file" name="file" placeholder="Choose an image." id="imageupload" accept="image/jpeg" />

PHP Code:

<?php

// Make sure logged in
include('../php_scripts/login-required.php');
// Make sure posted
if (!$_POST) {
    header('location: ./404.php');
}
// Include PHPmailer
require 'site_includes/PHPMailer/class.phpmailer.php';

// Get variables
$offertype = $_POST['ot'];
if ($offertype == 1) {
    $offertype = 'Discount';
} elseif ($offertype == 2) {
    $offertype = 'Free';
} else {
    $offertype = 'Not set';
}
$offerdetail = $_POST['od'];
// Check for errors
if (!isset($offertype) || empty($offertype)) {
    $errors = 1;
    header('location: ./create-advert.php?errors=' . $errors . '&ot=' . $offertype . '&od=' . $offerdetail);
} elseif (!isset($offerdetail) || empty($offerdetail)) {
    $errors = 2;
    header('location: ./create-advert.php?errors=' . $errors . '&ot=' . $offertype . '&od=' . $offerdetail);
}

// Get variables from database
include('../php_scripts/db-connect.php');
include('../php_scripts/get-userinfo.php');

// Set mail variables
$reply      = $email;
$replyname  = $fullname;
$to         = '[email protected]';
$toname     = 'John Doe';
$from       = '[email protected]';
$subject    = 'New Advert Request';
$message    = 'Name: ' . $fullname . "\r\n" . "Company: " . $company . "\r\n" . "Phone: " . $phone . "\r\n" . "Email: " . $email . "\r\n\r\n" . $offertype . "\r\n" . "--------------------------------------------------" . "\r\n\r\n" . $offerdetail;

if (isset($_FILES['file'])) {

    // Get attachment and upload
    $tmp_name = $_FILES['file']['tmp_name'];
    $name = $_FILES['file']['name'];
    move_uploaded_file($tmp_name, "uploads/$name");

    // Send email with attachment
    $mail = new PHPMailer;

    $mail->From = $from;
    $mail->addAddress($to, $toname);
    $mail->addReplyTo($reply, $replyname);
    $mail->WordWrap = 50;
    $mail->addAttachment(
        '/uploads/' . $name,
        $name,
        'base64',
        'mime/type'
    );
    $mail->Subject = $subject;
    $mail->AltBody = $message;

    if (!$mail->send()) {
        echo 'Mail Error!';
        exit;
    } else {
        echo 'Message has been sent!';
    }
}

?>

I also know I should be creating an array for acceptable file extensions (only .jpg or .jpeg in this case), but am also unsure of exactly how and where to implement these.

Also, I only changed the $to and $from emails. Why would this always be failing to send? All I'm looking to do is send a plain text email either with an email attachment or without one.

Upvotes: 0

Views: 1007

Answers (1)

Val Petruchek
Val Petruchek

Reputation: 176

Looks like you are trying to send file from the root /uploads directory:

$mail->addAttachment(
    '/uploads/' . $name,

It will be easier to send email before moving the file, passing $tmp_name as a first parameter to addAttachment() method. Or, optionally, make sure to specify correct path to the file after you have moved it to uploads dir. (wild guess: "./uploads/$name" - dot means current dir).

Also, you can try to check $mail->ErrorInfo for details of your error.

Upvotes: 1

Related Questions