phpmailer received email but not attachment - phpmailer

I am using the below script to allow user to upload images and email it to me. I did received the email but the attachment is not included in the email. It also show the from email as root user instead of the user's email address. please help.
<?php
ob_start();
require("class.phpmailer.php");
$salutation = $_POST['salutation'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$photo = $_FILES['photo'];
isset($_POST['submit']);
$active_keys = array();
foreach($_FILES[$photo]['name'] as $key => $filename)
{
if(!empty($filename))
{
$active_keys[] = $key;
} }
foreach($active_keys as $key)
{ switch(exif_imagetype($_FILES[$photo]['tmp_name'][$key])) {
case IMAGETYPE_JPEG:
case IMAGETYPE_PNG:
break;
default:
echo "{";
echo "error: 'This is no photo..'\n";
echo "}";
exit(0);
} }
$message = "Photo submitted by: $salutation $firstname $lastname. <br>Comments: $comments.<br>terms:$terms.";
$mail = new PHPMailer();
$mail->From = ($email);
$mail->AddAddress('info#domain.com');
$mail->Subject = "Submitted Photos";
$mail->Body = $message;
$mail->WordWrap = 50;
foreach($FILES['photo']['tmp_name'] as $photo) {
if(!empty($photo)) {
$mail->AddAttachment($photo);
}}
$mail->Send();
header("Location: thankyou.php");
exit();
?>

change
foreach($FILES['photo']['tmp_name'] as $photo) {
to
foreach($_FILES['photo']['tmp_name'] as $photo)

Related

Google_Service_Gmail mail not send

I am using PHPMailer to send mails with Google_Service_Gmail,but mails are not getting delivered although status is showing SENT.I have created a valid OAth Credentails for Gmail api,which did deliver 5 mails,but then stopped sending mails,although status is showing as SENT
Using Google_Service_Gmail with PHPMailer
try {
$mail = new PHPMailer();
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
$mail->CharSet = "UTF-8";
$mail->From = $sender;
$mail->FromName = $alias;
$mail->AddAddress($to);
$mail->Subject = $subject;
$mail->Body = $messageText;
$mail->IsHTML(true);
$mail->preSend();
$mime = $mail->getSentMIMEMessage();
$mime = rtrim(strtr(base64_encode($mime), '+/', '-_'), '=');
// $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$message = new Google_Service_Gmail_Message();
$message->setRaw($mime);
$client = getClient();
$service = new Google_Service_Gmail($client);
$message = $service->users_messages->send($sender, $message);
if ($message->labelIds[0] == "SENT") echo 'Message with ID: ' .$message->getId()."-".$message->labelIds[0];
else echo "Something went wrong...";
return $message;
} catch (Exception $e) {
print "Error" . $e->getMessage();
}
// return null;
}
Returns
Message with ID: 1845b21052f00d8e-SENT
but the e-mail is not delivered.

how to send email to multiple users in php mailer

I want to send email to two users when the count is not zero. When I am making
$semail = $row_seller['seller_email']; its working but when I make it
$semail = $row_seller['seller_email'].",example#gmail.com"; its not, I want to send it to both. How can i do it.
<?php
$query1 = "select * from `seller` where seller_pincode = $pincode";
$run_query1 = mysqli_query($con, $query1);
$count_seller = mysqli_num_rows($run_query1);
if ($count_seller == 0)
{
$semail = "example#gmail.com";
$sname = $saddress = $spincode = $scity = $sstate = $sphone = $sgst = $span = "Not Available";
}
else
{
while ($row_seller = mysqli_fetch_array($run_query1))
{
$semail = $row_seller['seller_email'] . ",example#gmail.com";
}
}
$email = $semail;
$subject = "Order Details";
$message = 'You got a new Order!!! ";
require_once('mailer / class . phpmailer . php);
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
$mail->Subject = $subject;
$mail->MsgHTML($message);
$mail->Send();
You need to use addAddress method or addCC for add copy, like so
$mail->addAddress('mail#domain.com', 'NAME'); //add address
$mail->addCC('mail#domain.com', 'NAME'); / add copy
Github

SMTP ERROR: Failed to connect to server: With PHPMAILER in Dreamhost

I can not send mail using the PHPMailer lib. My site is hosted on dreamhost,
with SMTP gmail I can send more properly to the configuration provided by the hosting the only return is:
SMTP ERROR: Failed to connect to server: php_network_getaddresses: getaddrinfo failed: Name or service not known (0)
SMTP connect() failed.
My code:
<?php
date_default_timezone_set('Etc/UTC');
require 'vendor/phpmailer/phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$name = trim(stripslashes($_POST['name']));
$from = trim(stripslashes($_POST['email']));
$subject = trim(stripslashes($_POST['subject']));
$message = trim(stripslashes($_POST['message']));
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Host = 'mail.example.com.br';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = 'e-mail#example.com';
$mail->Password = 'password';
$mail->SMTPSecure = 'tls';
$mail->Debugoutput = 'html';
$mail->isHTML(true);
$mail->setFrom($from, $name);
$mail->addReplyTo($from, $name);
$mail->addAddress('email#example.com');
$mail->Subject = $subject;
$mail->Body = $message;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
`
I managed to solve the problem, first replaces the e-mail sent to an e-mail sender#example.com own client, then tried some documentation of dreamhost, made the necessary changes, the code:
date_default_timezone_set('Etc/UTC');
require 'vendor/phpmailer/phpmailer/PHPMailerAutoload.php';
$subject = trim(stripslashes($_POST['subject']));
$name = trim(stripslashes($_POST['name']));
$from = trim(stripslashes($_POST['email']));
$body = trim(stripslashes($_POST['message']));
$mail = new PHPMailer;
$mail->isSMTP();
$mail->CharSet = 'UTF-8';
$mail->SMTPDebug = 1;
$mail->Host = 'mail.example.com.br';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = 'sender#example.com.br';
$mail->Password = 'password';
$mail->Debugoutput = 'html';
$mail->isHTML(true);
$mail->setFrom('sender#dexample.com.br', 'DontAnswer');
$mail->addReplyTo($from, $name);
$mail->addAddress('contact#example.com.br');
$mail->Subject = $subject;
$mail->Body = $body;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
In dreamhost documentation says it should be repeated sending e-mail in setFrom () method; after these changes the form worked perfectly.

php mailer will send mail but not include content

This mailer works with go daddy but I cant get it too work with a home server. It will send the mail and shows up in the inbox but not the content. I think it has to do with using $body tag but I am not sure any suggestions?
<?php
// PHPmailer settings
$mail = new PHPMailer(); // create a new object
$mail->Issmtp(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Username = "email"; // SMTP username
$mail->Password = "password"; // SMTP password
$mail->From = $email;
$mail->FromName = $contact_name;
$mail->SetFrom($email, $contact_name);
$mail->AddAddress('email');
$mail->Priority = 1;
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = "Gray Gables maintenance request";
$mail->Body = $body;
$mail->AltBody = "This is the text-only body";
// gets info from form
$name = $_POST['name'] ;
$email = $_POST['email'] ;
$phone = $_POST['phone'] ;
$message = $_POST['message'] ;
$ip = $_SERVER['REMOTE_ADDR'];
// defines how message looks in email
$mail->Body="
Name: $name<br>
Telephone: $phone<br>
Email: $email<br>
IP address: $ip <br>
-------------------<br>
Message:<br>
$message";
// looks good in your inbox
$mail->From = "$email";
$mail->FromName = "$name";
$mail->AddReplyTo("$email","$name");
// gives success or error
if(!$mail->Send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
echo '
<meta http-equiv="refresh" content="3;url=http/">
<center><font size="5px">
Message has been sent!!<br>
you will be redirected to our home page in 3 seconds...<br>
click here to return now.
</font>
</center>
';
?>
<?php
echo ' Client IP: ';
if ( isset($_SERVER["REMOTE_ADDR"]) ) {
echo '' . $_SERVER["REMOTE_ADDR"] . ' ';
} else if ( isset($_SERVER["HTTP_X_FORWARDED_FOR"]) ) {
echo '' . $_SERVER["HTTP_X_FORWARDED_FOR"] . ' ';
} else if ( isset($_SERVER["HTTP_CLIENT_IP"]) ) {
echo '' . $_SERVER["HTTP_CLIENT_IP"] . ' ';
}
?>
I think you are looking for this...
$body = file_get_contents('yourfile.html');
You can attach any HTML content in 'yourfile.html', which displays in your body of the mail.

why php mailer not send attachement?

I m using php mailer to send mail with attachment but i don't know why attachment not send
else msg in body send properly and no error show plz give best solution for this
$to = 'ok123#gmail.com';
$msg = "This a body of a mail";
require_once("functions/class.phpmailer.php");
$mailer = new PHPMailer();
$mailer->From = "noreply#gmail.com";
$mailer->Subject = "attachment file";
$mailer->AddAddress($to);
$mailer->ContentType = 'text/html';
$mailer->CharSet = "UTF-8";
$mailer->Body = $msg;
$mailer->IsHTML(true);
$mailer->AddAttachment("images/20130319182911.zip","20130319182911.zip");
$mailer->Send();
echo "Message Sent OK<p></p>\n";
This is my favorite mail attachment class. Use it if you like it.
class AttachmentEmail {
private $from = 'yours#email.com';
private $from_name = 'Your Name';
private $reply_to = 'yours#email.com';
private $to = '';
private $subject = '';
private $message = '';
private $attachment = '';
private $attachment_filename = '';
public function __construct($to, $subject, $message, $attachment = '', $attachment_filename = '') {
$this -> to = $to;
$this -> subject = $subject;
$this -> message = $message;
$this -> attachment = $attachment;
$this -> attachment_filename = $attachment_filename;
}
public function mail() {
if (!empty($this -> attachment)) {
$filename = empty($this -> attachment_filename) ? basename($this -> attachment) : $this -> attachment_filename ;
$path = dirname($this -> attachment);
$mailto = $this -> to;
$from_mail = $this -> from;
$from_name = $this -> from_name;
$replyto = $this -> reply_to;
$subject = $this -> subject;
$message = $this -> message;
$file = $path.'/'.$filename;
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$name = basename($file);
$header = "From: ".$from_name." <".$from_mail.">\r\n";
$header .= "Reply-To: ".$replyto."\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message."\r\n\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use diff. tyoes here
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
$header .= "--".$uid."--";
if (mail($mailto, $subject, "", $header)) {
return true;
} else {
return false;
}
} else {
$header = "From: ".($this -> from_name)." <".($this -> from).">\r\n";
$header .= "Reply-To: ".($this -> reply_to)."\r\n";
if (mail($this -> to, $this -> subject, $this -> message, $header)) {
return true;
} else {
return false;
}
}
}
}
And use it like
$sendit = new AttachmentEmail('test#example.com', 'Testing attachment!', 'Hi', '/home/test/test.jpg');
$sendit -> mail();
Gmail removes .zip attachment when it detects particular files inside, like EXEs. Add zip attachment as 20130319182911.piz and it should arrive.
It's not a phpmailer problem, but a Gmail policy.

Resources