PHP Mailer Attachments Not Uploading? - php

I am trying to set up PHP Mailer to allow me to send attachments, mainly just images, from a form. I have the following code working except I am not receiving the attachments, they are not being uploaded. Can anybody help me or point me in the right direction as to what I am missing?
PHP
<?php
require 'class.phpmailer.php';
date_default_timezone_set('Europe/London');
$mail = new PHPMailer;
$current=date('l F dS, Y, H:i a');
$mail->From = 'me#example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('karina#live.co.uk', 'Karina McG');
$mail->addAddress('me#live.com');
$mail->addReplyTo('noreply#tempur.com');
$mail->addCC('');
$mail->addBCC('');
$mail->addAttachment('/var/tmp/file.tar.gz');
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');
$mail->isHTML(true);
$name=$_POST['name'];
$subject=$_POST['subject'];
$system=$_POST['system'];
$country=$_POST['country'];
$description=$_POST['description'];
$mail->Subject = "TEMPUR Web Support Request - $system";
$mail->Body = "Dear $name,<br>
<br>
We have received your TEMPUR request form! - $current<br>
<br>
We will be in contact shortly regarding your issue/s.<br>
<br>
Name : $name <br>
Country : $country <br>
Subject : $subject <br>
System : $system <br>
Description : $description";
$mail->AltBody = 'Hehehe';
//$to="$email";//Will change to Digital Users email!
//Send email
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
echo 'Message has been sent';
?>
HTML
Upload File:<input type="file" name="upload" id="upload"><br>

add enctype="multipart/form-data" to the form as attribute if it is not their.
also try $mail->addAttachment($_FILES["upload"]["tmp_name"]);
It will work.

Related

PHPMailer variable in addadress

I have a PHP mailer function set up to send a mail to a customer that purchases a file. IT works perfect if I use a static emailaddress in the AddAdress method. But if I use a variable it doesn't get sent.
function sendEmailToClient($name, $address, $postcode, $city, $email, $which, $orderid, $downloadlink){
$mail = new PHPMailer;
// $mail->isSMTP();
// $mail->Host = 'localhost';
$mail->setFrom('email#email.com', 'My Store');
$mail->addAddress($email); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Thanks for your order';
$mail->Body = '<body>
<p>Order:</p>
<p>Order ID: '.$orderid.'</p>
<p>Book: '.$which.'<br />
Name: '.$name.'<br />
Address: '.$address.'<br />
Postal code: '.$postcode.'<br />
City: '.$city.'<br/>
Email: '.$email.'</p>
<p>Download: '.$downloadlink.'</p>
<p>Have fun!!</p>
</body>';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
}
I don't understand why the variable doesn't work. I checked the input from the function to see if the email is actually a valid email, and that is the case. But it's not working with the variable.
Anyone know a solution?

Unable to send email from godaddy server to godaddy or zoho mail

I am trying to send email from my website to my godaddy mail and zoho mail but its not working.
I tried it on my gmail account and its working fine.
I am using phpmailer.
MY CODE-
require_once "PHPMailerAutoload.php";
//PHPMailer Object
$mail = new PHPMailer;
//From email address and name
$mail->From = "test#deltware.com";
$mail->FromName = "Himanshu Mishra";
$mail->addAddress("my godaddy webmail"); //Recipient name is optional
//Send HTML or Plain Text email
$mail->isHTML(true);
$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body in HTML</i>";
$mail->AltBody = "This is the plain text version of the email content";
if(!$mail->send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent successfully";
}
Please help!!!!!
Your From syntax is wrong
Instead of
$mail->From = "test#deltware.com";
$mail->FromName = "Himanshu Mishra";
It should be
$mail->setFrom('test#deltware.com', 'Himanshu Mishra');
Check this link https://github.com/PHPMailer/PHPMailer

PHP Mailer Function not working

I am new to PHP and have a form that sends mails. The following is the PHP code and i get the message
"Message body empty message could not be sent.
mailer error:
"
I have been searching on google whole day of what the issue might be but have not been able to figure it out.
I am not understanding the part "Message body empty". I checked the $mail.body and it has the value.
<?php
require_once("class.PHPMailer.php");
$mail = new phpmailer();
$mail->issmtp(); // set mailer to use smtp
$mail->host = "smtp.office365.com"; // specify main and backup server
$mail->smtpauth = true; // turn on smtp authentication
$mail->username = "noreply#test.com"; // smtp username
$mail->password = "test123"; // smtp password
$mail->from = "noreply#test.com";
$mail->fromname = "no reply";
$mail->addaddress("johndoe#test.com", "John Doe");
$mail->wordwrap = 50; // set word wrap to 50 characters
//$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";
echo $mail->subject;
try{
if(!$mail->send())
{
echo "message could not be sent. <p>";
echo "mailer error: " . $mail->errorinfo;
exit;
}
echo 'after mail send';
}
catch(exception $e) {
echo 'caught exception: ', $e->getmessage(), "\n";
}
echo "message has been sent";
?>
Also I have deployed in windows server.
Thanks in advance.
<h2>Feedback Form</h2>
<?php
// display form if user has not clicked submit
if (!isset($_POST["submit"])) {
?>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
From: <input type="text" name="from"><br>
Subject: <input type="text" name="subject"><br>
Message: <textarea rows="10" cols="40" name="message"></textarea><br>
<input type="submit" name="submit" value="Submit Feedback">
</form>
<?php
} else { // the user has submitted the form
// Check if the "from" input field is filled out
if (isset($_POST["from"])) {
$from = $_POST["from"]; // sender
$subject = $_POST["subject"];
$message = $_POST["message"];
// message lines should not exceed 70 characters (PHP rule), so wrap it
$message = wordwrap($message, 70);
// send mail
mail("webmaster#example.com",$subject,$message,"From: $from\n");
echo "Thank you for sending us feedback";
}
}
?>

sending attachment fails with php mailer function codeigniter

Here I am trying to send email with an attachment, all codes are running fine but I didn't find any attachment along with the mail, but the same file is getting printed in hyper link that I had given with success message. I have used php mailer class in codeigniter.
public function sendmailto()
{
$this->load->library('phpmail');
$mail = new PHPMailer();
$body = "hello";
$mail->AddReplyTo("reply#mymail.com","First Last");
$mail->SetFrom('noname#mymail.com', 'First Last');
$mail->AddReplyTo("mail#mymail.com","First Last");
$address = "abcd#mymail.com";
$mail->AddAddress($address, "John Doe");
$mail->Subject = "PHPMailer Test Subject via mail(), basic";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$mail->AddAttachment("../../uploads/a.pdf"); // attachment
//$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
}
else {
echo "Message sent!<a href='../../uploads/a.pdf' >click</a>" ;
}
}
I think its path issue you can try this
$attachment = base_url().'/uploads/a.pdf';
$mail->AddAttachment($attachment); // attachment
I think you should change
$mail->MsgHTML();
to
$mail->Body;

How to do email form with multiple recipients and different body?

i have one contact form, when user submit all value will send(email) to admin.But now i want to do when user submit admin will receive the email and user also will receive an email but with different body.
here my previous code :
<?php
if(md5($verif_box).'a4xn' == $_COOKIE['tntcon']){
$name= $_POST["name"];
$email= $_POST["email"];
$phone= $_POST["phone"];
$company= $_POST["company"];
$message= $_POST["message"];
require_once('lib/class.phpmailer.php');
$mail = new PHPMailer(); // defaults to using php "mail()"
$mail->AddReplyTo("admin#gmail.com","I Concept");
$mail->SetFrom('admin#gmail.com', 'I Concept');
$mail->AddReplyTo("admin#gmail.com","I Concept");
$address = "admin#gmail.com";
$mail->AddAddress($address, "I Concept");
$mail->Subject = "MY - Request a Quote";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->Body = "<strong>Request a Quote from I Concept Malaysia Website</strong><br><br>
Name : $name<br>
Email : $email<br>
Phone : $phone<br>
Company : $company<br>
Enquiry : $message<br> <br>
Thank You!<br>
";
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!<br>";
}
}
?>
Try the following. Didn't test but you basically need to get another PHPMailer object going and set the body and to information separately.
$address = "admin#gmail.com";
$mail->Subject = "MY - Request a Quote";
// keeps the current $mail settings and creates new object
$mail2 = clone $mail;
// mail to admin
$mail->AddAddress($address, "I Concept");
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->Body = "<strong>Request a Quote from I Concept Malaysia Website</strong><br><br>
Name : $name<br>
Email : $email<br>
Phone : $phone<br>
Company : $company<br>
Enquiry : $message<br> <br>
Thank You!<br>";
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!<br>";
}
// now send to user.
$mail2->AddAddress($email, $name);
$mail2->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail2->Body = "Separate email body for user filling form out.";
if(!$mail2->Send()) {
echo "Mailer Error: " . $mail2->ErrorInfo;
} else {
echo "Message sent!<br>";
}
Cloning the PHPmailer object is not necessary. Just use the ClearAllRecipients method that is built into PHPmailer before changing the body and sending the second email.
I'm sure you can't send different bodies in one SMTP call. However you can just send the first email and initiate a new PHPMailer.

Categories