phpMailer Attachment not working - php

this is my first post and I hope that I can get some help regarding adding an attachment field in my phpMailer contact form. I have already added an uploader [browse] bar in the html website but I don't know how to link it with the phpmailer. do I have to declare it at the package which is phpmailer.php or do something with it?
I would really appreciate some help. Below is my desperate attempt.
Snippet from the code:
<?
$body = ob_get_contents();
$to = 'xxx#xxxx.com>';
$email = $email;
$subject = $subject;
$fromaddress = "xxx#xxxx.com";
$fromname = "Online Contact";
require("phpmailer.php"); //<<this is the original pack
$mail = new PHPMailer();
$mail->From = "xxx#xxxx.com";
$mail->FromName = "My Name";
$mail->AddBCC("xxx#xxxx.com","Name 1");
$mail->AddAddress( $email,"Name 2");
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AltBody = "This is the text-only body";
// attachment
$mail->AddAttachment('uploadbar', 'new_file.pdf'); // the name of my html uploader is uploadbar, clicking browse to locate a file
if(!$mail->Send()) {
$recipient = 'xxx#xxxx.com';
$subject = 'Contact form failed';
$content = $body;
mail($recipient, $subject, $content, "From: xxx#xxxx.com\r\nReply-To: $email\r\nX-Mailer: DT_formmail");
exit;
}
?>

Had the same problem, try this
$mail->AddAttachment($_SERVER['DOCUMENT_ROOT'].'/file-name.pdf');

First, make sure your upload form has enctype='multipart/form-data' as in <form method=post action=file.php enctype='multipart/form-data'>.
then, use this code in your phpmailer handler file
foreach(array_keys($_FILES['files']['name']) as $key) {
$source = $_FILES['files']['tmp_name'][$key];
$filename = $_FILES['files']['name'][$key];
$mail->AddAttachment($source, $filename);
}

Make sure attachment file name should not contain any special character and check the file path also correct.
eg: [file name.pdf] Should be like [filename.pdf]

Related

PHP - Send copy of form input to user [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
I'm trying to send a copy of a submitted form to the user of the client-side. I tried implementing some code I already found around here but can not get it to work successfully. Would you guys mind taking a look? ...All input appreciated, as always!
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "nonedesigns#gmail.com";
$email_subject = "SuitUP | Support";
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
if ($success)
mail($email_from, $Subject, $Body, "From: <$email_From>");
// validation expected data exists
$error_message = "";
//get fields, and check if they are filled
$project_name = $_POST['first_name'];
required_field($project_name,"Project Name");
$last_name = $_POST['last_name'];
required_field($last_name,"Last Name");
$email_from = $_POST['email'];
required_field($email_from,"Email");
$contact_reason = $_REQUEST['contact_reason'];
$other_info = $_POST['other_info'];
required_field($other_info,"Additional Info");
//phone number manip
$phone_number_clean = clean_phone_number($phone_number);
//email manip
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
First you need to declare the variables, and then send out the email:
// validation expected data exists
$error_message = "";
//get fields, and check if they are filled
$project_name = $_POST['first_name'];
required_field($project_name,"Project Name");
$last_name = $_POST['last_name'];
required_field($last_name,"Last Name");
$email_from = $_POST['email'];
required_field($email_from,"Email");
$contact_reason = $_REQUEST['contact_reason'];
$other_info = $_POST['other_info'];
required_field($other_info,"Additional Info");
//phone number manip
$phone_number_clean = clean_phone_number($phone_number);
//email manip
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "nonedesigns#gmail.com";
$email_subject = "SuitUP | Support";
$success = mail($email_to, $Subject, $Body, "From: <$email_from>");
if ($success)
mail($email_from, $Subject, $Body, "From: <$email_from>");
Try to be consistent in variable naming conventions (not mixing CamelCase and underscores). That will make it easier for you to stay on top of your code.
Try to send mail using PHPMailer. Try the below standard php code but first download phpmailer.php and save it.
// include the PHPMailer
require_once('PHPMailer.php');
$mail = new PHPMailer(); // defaults to using php "mail()"
$mail->IsSendmail(); // telling the class to use SendMail transport
$body = "hello";
$mail->AddReplyTo("name#yourdomain.com","First Last");
$mail->SetFrom('name#yourdomain.com', 'First Last');
$mail->AddReplyTo("name#yourdomain.com","First Last");
$address = "name#yourdomain.com";
$mail->AddAddress($address, "John Doe");
$mail->Subject = "PHPMailer Test Subject via Sendmail, basic";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$mail->IsHTML(true);
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}

How to add form validation to phpmailer?

I am a newbie to PHP, and I am currently using phpmailer to let my clients send emails to me, but a bot is apparently taking advantage of it and sends 3 emails at exactly the same time everyday. So I've been trying to add validation to the form using PHP as javascript can be stopped to avoid validation.. Could you guys please help me add validation to this? I just need to validate name and email. Thank you so much in advance.
PS. I removed some parts of the code for privacy.
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$address = $_POST['address'];
$proptypes = $_POST['proptypes'];
$units = $_POST['units'];
$purchaseOrRefi = $_POST['purchase'];
$loans = $_POST['loans'];
$income = $_POST['income'];
$apt = $_POST['apartment'];
$message = $_POST['message'];
$body = "<b>[From]</b><br>$name<br><br> <b>[E-Mail]</b><br>$email<br><br> <b>[Phone #]</b><br>$phone<br><br> <b>[Address]</b><br>$address<br><br> <b>[Type of Property]</b><br>$proptypes<br><br> <b>[# of Units]</b><br>$units<br><br> <b>[Purchase or Refi?]</b><br>$purchaseOrRefi<br><br> <b>[Amnt of Loans Requested]</b><br>$loans<br><br> <b>[Total Income]</b><br>$income<br><br> <b>[Total Apt Expense]</b><br>$apt<br><br> <b>[Message]</b><br>$message<br><br>";
$mail->Subject = 'Someone wants to hear more about your mortgage programs!';
$mail->Body = $body;
$mail->From = '';
$mail->FromName = '';
// Add a recipient
$mail->addAddress('');
$mail->addAddress('');
$mail->addAddress('');
$mail->addAttachment($_FILES['attachment']['tmp_name'], $_FILES['attachment']['name'], 'base64', $_FILES['attachment']['type']); // Add attachments
$mail->isHTML(true);
if(!$mail->send()) {
echo 'Message could not be sent. Please try again.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Your message has been sent! We will get back to you soon!';
}
?>

PHPMail - can I send two unique emails

I'm using PHPMailer to successfully send an email upon a webform submission (so just using basic post data, no mysql databases or any lookups).
What I need to do is send two seperate emails, one version for the customer and the other for a customer service agent - so that when a user completes a webform, they will receive a 'marketing' version of the email, whilst the customer service agent will receive an email just containing the details submitted by the user.
See below what im using at the moment, but not sure how to best implement to send the secoind email? It presently fails and the script exits after sending only one email.
$body = ob_get_contents();
$to = 'removed';
$email = 'removed';
$fromaddress = "removed";
$fromname = "removed";
require("phpmailer.php");
$mail = new PHPMailer();
$mail->From = $fromaddress;
$mail->FromName = $fromname ;
$mail->AddAddress("email#example.com");
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = "Subject";
$mail->Body = $body;
$mail->AltBody = "This is the text-only body";
if(!$mail->Send()) {
$recipient = 'email#example.com';
$subject = 'Contact form failed';
$content = $body;
mail($recipient, $subject, $content,
"From: mail#yourdomain.com\r\nReply-To: $email\r\nX-Mailer: DT_formmail");
exit;
}
//Send the customer version
$mail=new PHPMailer();
$mail->SetFrom('email', 'FULLNAME');
$mail->AddAddress($mail_vars[2], 'sss');
$mail->Subject = "Customers email";
$mail->MsgHTML("email body here");
//$mail->Send();
In the customer version you are not setting the email's properties the same way you are in the first. For example you use $mail->From = $fromaddress; in the first and $mail->SetFrom('email', 'FULLNAME'); in the second.
Because you instantiated a new $mail=new PHPMailer(); you need to set the properties again.
Instead of just bailing out with a useless "something didn't work" message, why not have PHPMailer TELL you what the problem is?
if (!$mail->send()) {
$error = $mail->ErrorInfo;
echo "Mail failed with error: $error";
}

Files will corrupt when send by php mailer

I use this php mailer
every things are O.k. but when I attach a file, corrupted file receive in destination email. for example when I send a pdf file receiver can't open it.
and this is my code in using phpmailer:
$target_path = "upload_files/";
$target_path = $target_path . basename( $_FILES['attach']['name']);
if(move_uploaded_file($_FILES['attach']['tmp_name'], $target_path)) {
} else{
}
//eupload file end
require_once 'phpmailer/phpmailer.inc.php';
$mail = new PHPMailer();
$body = $message;
$body = eregi_replace("[\]",'',$body);
$mail->AddReplyTo($email, $fname." ".$lname);
$mail->FromName = $fname." ".$lname;
$mail->From = $email;
$mail->AddAddress("email address", "some one");
$mail->Subject = "something";
$mail->body = $body;
$mail->AddAttachment($target_path); // attachment
if(!$mail->Send()) {
} else {
}
Your PHPMailer have critical errors try it
Must be your Header include file's are wrong
becouse this is the basic mistake in attachments
$headers .= "\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"_1_$boundary\"";
:P

PHPMailer wont send to email with Hyphen IE-#blah-blah.com

I have a simple contact form that uses PHPMailer to send it's information to the admin. Everything works fine when the results are sent to an e-mail with out a hypen such as blah#blah.com. As soon as I change the address to an e-mail with a hypen like blah#blah-blah.com, I get this error.
"Could not instantiate mail function. Mailer Error: Could not instantiate mail function."
Is this a bug with PHPMailer?
Here's my code
<?php
require_once('class.phpmailer.php');
$question_for = $_POST['question_for'];
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$company = $_POST['company'];
$comment = $_POST['comment'];
$mail = new PHPMailer(); // defaults to using php "mail()"
$body = eregi_replace("[\]",'',$body);
$mail->AddReplyTo("donotreply#blah-blah.com","DoNotReply");
$mail->SetFrom('donotreply#blah-blah.com', 'DoNotReply');
switch ($question_for) {
case "Sales":
$address = "Sales#blah-blah.com";
$mail->AddAddress($address, "Blah");
$mail->Subject = "Message from Sales";
break;
case "Service":
$address = "service#blah-blah.com";
$mail->AddAddress($address, "Blah");
$mail->Subject = "Message from Service";
break;
case "Career":
$address = "career#blah-blah.com";
$mail->AddAddress($address, "Blah");
$mail->Subject = "Message from Career";
break;
}
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML("
Question For:".$question_for."<br />
Name:".$name."<br />
Phone:".$phone."<br />
Email:".$email."<br />
Company:".$company."<br />
Comment:".$comment."<br />");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
header('Location: http://blah.com/contact/?sent');
}
?>
Checking even more, I see that this works
<?php
mail("test#blah.com", "Test Email", "Testing"); ?>
but this does not
<?php
mail("test#blah-blah.com", "Test Email", "Testing"); ?>

Categories