PHPMailer with attachment - php

So, i'm having problem with this code.
It's a form where it get the data and this is the phpmailer fragment, where i get the error Could not acess file
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$message= $_REQUEST['message'];
$message= "--$boundary" . PHP_EOL;
$message= "Content-Type: text/html; charset='utf-8'" . PHP_EOL;
$message= "--$boundary" . PHP_EOL;
$tapete=$_REQUEST['tapete'];
$medidas=$_REQUEST['medidas'];
$cliente=$_REQUEST['cliente'];
$mail = new PHPMailer();
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "mucapapipa.br#gmail.com";
$mail->Password = "----";
$mail->Subject = 'Formulário FacilityCom';
$mail->setFrom = ('mucapapipa.br#gmail.com');
$mail->Body = 'Tipo e marca: $tapete \nMedidas: $medidas \nCliente: $cliente\n $from';
$mail->IsHTML(true);
$mail->AddAttachment($file['tmp_name'], $file['name']);
$mail->Send();`

There's a lot wrong here.
Don't mess with boundaries - PHPMailer does all that for you.
$mail->setFrom = ('mucapapipa.br#gmail.com') should be $mail->setFrom('mucapapipa.br#gmail.com').
It would help if you based your code on the file upload example provided with PHPMailer as that shows how to handle file uploads correctly.

Related

Could Not Access File using phpMailer

I'm Using phpmailer to send mail.
Recieving mail fine but the attachment not attached in it.
When I debug it. it gives me the error.
"Could not access file: upload_images/images.jpg"
i have a folder upload_images in which i have an image name images.
here is my code...
if (isset($_POST['btnsubmit'])) {
require "phpmailer/PHPMailerAutoload.php";
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = "mail.domain.com";
$mail->port = 465;
$mail->SMTPAuth=true;
$mail->SMTPDebug = 4;
$mail->SMTPSecure = 'tls';
$mail->Username = "info#example.com";
$mail->Password = '******';
$file_name = $_FILES["attc"]["name"];
$tmp_name = $_FILES["attc"]["tmp_name"];
$path = '/upload_images/';
//move_uploaded_file($tmp_name, $path.$file_name);
$mail->setFrom('abc#gmail.com','From_name');
$mail->addAddress('xyz#gmail.com','To_name');
$mail->addReplyTo('abc#gmail.com','From_name');
$mail->isHTML(true);
$mail->AddAttachment("upload_images/images.jpg","images.jpg");
$mail->Subject = "Testing well";
$mail->Body = 'This is Body Part';
if ($mail->send()) {
echo "<script>alert('Email Sent Success!')</script>";
}
else{
echo "<script>alert('".$mail->ErrorInfo."')</script>";
}
}
You should use an absolute path. For example, if upload_images is inside the document root, you may use:
$mail->AddAttachment($_SERVER['DOCUMENT_ROOT']."/upload_images/images.jpg","images.jpg");
Use the absolute path.
if (isset($_POST['btnsubmit'])) {
require "phpmailer/PHPMailerAutoload.php";
$my_path ="upload_images/images.jpg";
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = "mail.domain.com";
$mail->port = 465;
$mail->SMTPAuth=true;
$mail->SMTPDebug = 4;
$mail->SMTPSecure = 'tls';
$mail->Username = "info#example.com";
$mail->Password = '******';
$file_name = $_FILES["attc"]["name"];
$tmp_name = $_FILES["attc"]["tmp_name"];
$path = '/upload_images/';
//move_uploaded_file($tmp_name, $path.$file_name);
$mail->setFrom('abc#gmail.com','From_name');
$mail->addAddress('xyz#gmail.com','To_name');
$mail->addReplyTo('abc#gmail.com','From_name');
$mail->isHTML(true);
$mail->AddAttachment($my_path);
$mail->Subject = "Testing well";
$mail->Body = 'This is Body Part';
if ($mail->send()) {
echo "<script>alert('Email Sent Success!')</script>";
}
else{
echo "<script>alert('".$mail->ErrorInfo."')</script>";
}
}
Use the following and path for file in the variable $my_path
if (isset($_POST['btnsubmit'])) {
//use the path
$my_path = "/upload_images/images.jpg";
require "phpmailer/PHPMailerAutoload.php";
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = "mail.domain.com";
$mail->port = 465;
$mail->SMTPAuth=true;
$mail->SMTPDebug = 4;
$mail->SMTPSecure = 'tls';
$mail->Username = "info#example.com";
$mail->Password = '******';
$file_name = $_FILES["attc"]["name"];
$tmp_name = $_FILES["attc"]["tmp_name"];
$path = '/upload_images/';
//move_uploaded_file($tmp_name, $path.$file_name);
$mail->setFrom('abc#gmail.com','From_name');
$mail->addAddress('xyz#gmail.com','To_name');
$mail->addReplyTo('abc#gmail.com','From_name');
$mail->isHTML(true);
$mail->AddAttachment($my_path,'images.jpg');
$mail->Subject = "Testing well";
$mail->Body = 'This is Body Part';
if ($mail->send()) {
echo "<script>alert('Email Sent Success!')</script>";
}
else{
echo "<script>alert('".$mail->ErrorInfo."')</script>";
}
}

I used phpmailer() concept to receive mail from users from my bigrock server using php script, but It's not working

This is my code:
<?php
require_once "PHPMailer/PHPMailerAutoload.php";
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$mail = new PHPMailer;
$mail->IsSMTP();
$mail->Host = "smtp.gmail.com"; //gethostbyname('smtp.gmail.com');
$mail->SMTPAuth = true;
$mail->SMTPDebug = 3;
$mail->SMTPSecure = "ssl";
$mail->Port = 465;
$mail->Username = "ku***wa***ar***#gmail.com";
$mail->Password = "t***r9***5******7";
$mail->From = $email;
$mail->FromName = $name;
$mail->AddAddress($mail->Username);
$mail->Subject = 'Contact form query / feedback';
$mail->Body = "
<div><span>Name : </span><span>{$name}</span></div>
<div><span>Email : </span><span>{$email}</span></div>
<div><p>{$message}</p></div>
";
$mail->isHTML(true);
if ($mail->send()){
echo "Your feedback/query is sent!";
}else{
echo "Error! Unable to forward your request.<br> Pleas try again later!";
}
Note: I have used " GMail " as my SMTP server and SMTPSecure is " ssl " and port is "465" and username & passwords are my GMail username & password
The Error Message is:
Screen Shot : https://s16.postimg.org/4hgsh9d51/smtp.png
Here is your code modified
<?php
include "PHPMailer_5.2.4/class.phpmailer.php";
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$mail = new PHPMailer;
$mail->IsSMTP();
$mail->Host = "smtp.gmail.com"; //gethostbyname('smtp.gmail.com');
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->isHTML(true);
$mail->Username = "hari.andoidsaiss#gmail.com";
$mail->Password = "supreme#12#";
$mail->From = $email;
$mail->FromName = $name;
$mail->AddAddress($mail->Username);
$mail->Subject = 'Contact form query / feedback';
$mail->Body = "
<div><span>Name : </span><span>{$name}</span></div>
<div><span>Email : </span><span>{$email}</span></div>
<div><span>Message : </span><p>{$message}</p></div>
";
if ($mail->send()){
echo "Your feedback/query is sent!";
}else{
echo "Error! Unable to forward your request.<br> Pleas try again later!";
}

Connect to Gmail via phpmailer

I trying connect to my e-mail located in gmail.com. I want create contact form. I have created form contact in HTML. Now I try use phpmailer class to connect. I show my code:
<?php
$name = isset($_POST['name']) ? $_POST['name'] : false;
$email = isset($_POST['email']) ? $_POST['email'] : false;
$topic = isset($_POST['topic']) ? $_POST['topic'] : false;
$message = isset($_POST['message']) ? $_POST['message'] : false;
error_reporting(E_ALL);
ini_set("display_errors", 1);
if(isset($_POST['send_message'])){
include "../phpmailer/class.phpmailer.php"; // include the class name
$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->SMTPKeepAlive = true;
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "myemail#gmail.com";
$mail->Password = "mypassword";
$mail->SetFrom("myemail#gmail.com");
$mail->AddAddress('myemail#gmail.com','MyName');
$mail->Subject = "Here is the subject";
$mail->Body = "This is the HTML message body <b>in bold!</b>";
if(!$mail->Send()){
echo "Mailer Error: " . $mail->ErrorInfo;
}
else{
echo "Message has been sent";
}
}
?>
When I try run this code I geting the following errors:
http://pastebin.com/R7PBZDei
How fix it?
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "user#gmail.com";
$mail->Password = "pass";
Greetings

php mailer sender form user

i have phpmailer and i can send email via php page without any problem
but the sender automatically by username it i am use in smtp server
i want take sender email from user who write message not from default sender
and this is form code
<?php
require '../../PHPMailer/PHPMailer-master/PHPMailerAutoload.php';
$name = $_POST['name'];
$Institute = $_POST['Institute'];
$email = $_POST['email'];
$message = $_POST['message'];
$mail = new PHPMailer();
$mail->isSMTP();
$mail->CharSet = 'UTF-8';
$mail->Debugoutput = 'html';
//$mail->SMTPDebug = true;
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "MyGmail";
$mail->Password = "MyGmailPass";
$mail->setFrom('Mygmail', $name);
$mail->addReplyTo('MyGmail', 'First Last');
$mail->addAddress('MyEmail', 'Nasser');
$mail->Subject = 'Database Site Reminder';
$mail->Body = ($message);
$mail->AltBody = 'This is a plain-text message body';
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
i am put `$mail->setFrom('Mygmail', $name); this like
$mail->setFrom($email, $name);
because take sender email from user , and i get Message sent
but message not arrive to my email
i cant find any solution... please assist me
thanks ...
$mail = new PHPMailer();
$body = "Body text goes here";
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp.gmail.com"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "tls"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587; // set the SMTP port for the GMAIL server
$mail->Username = "Gmail Id"; // GMAIL username
$mail->Password = "PaSsWoRd"; // GMAIL password
$mail->SetFrom('fromemail#gmail.com', 'User');
$mail->AddReplyTo("fromemail#gmail.com","User");
$mail->Subject = "Subject Goes Here";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$mail->AddAddress('toemail#gmail.com', 'User');
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo("Success");
}
try this code... its working....
If you are using PHP Mailer from Github,
then you can set the Sender name by,
$mail->SetFrom("info#mibckerala.org", "MIBC");

phpgmailer stopped working suddenly

I used phpgmailer to send emails and it was working smoothly. Today I tested my project and it's not working now.
<?php
require_once('class.phpgmailer.php');
$mail = new PHPGMailer();
$mail->Username = 'username#gmail.com';
$mail->Password = '********';
$mail->From = 'username#gmail.com';
$mail->FromName = "<blah>";
$mail->Subject = 'something';
$mail->AddAddress('xyz#gmail.com');
$mail->Body = "Hello Sir"."\n"."
Your Password is : ."."";
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Send();
if(!$mail->Send())
{
echo 'Message could not be sent.' ;
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
echo 'Message has been sent';
I suggest you download PHPMailer and try this code:
require("phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = "ssl://smtp.gmail.com";
$mail->Port = 465;
$mail->Username = "from#gmail.com";
$mail->Password = "****";
$mail->FromName = "Sender name";
$mail->Subject = "test";
$mail->Body = "Test body";
$mail->AddAddress('sender#mail.com');
if(!$mail->Send()){
echo "Mailer Error: " . $mail->ErrorInfo;
}else{
echo "Message has been sent";
}
#goose is right, remove the first $mail->Send() and leave the one in the if statement. If the From address is the same as the username email, then you don't need that either as it will take it from your gmail account.
Try that and see if it works.
EDIT: try adding the following;
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 0; // 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#gmail.com';
$mail->Password = 'password';
Try that and if there are errors, then it should give you more detail.
EDIT2: If that doesn't work then I suggest trying PHPMailer instead of PHPGmailer - and follow the tutorial here: http://www.web-development-blog.com/archives/send-e-mail-messages-via-smtp-with-phpmailer-and-gmail/
Please, make sure:
$mail->Username is equal to $mail->From, and is correct
$mail->Password is correct
$mail->FromName does not contain "<" and ">" characters; just try $mail->FromName = 'Test';

Categories