I solved problem, changed a bit localisation of files(removed vendor) and it works. Last thing I need to do is adding attachment.
I added line, but still can send only mail without attachment. Aslo created folder upload on catalog with my project.
$file_name = $_POST['file'];
$mail->addAttachment("uploads/".$file_name);
My new working code:
```php
<?php
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require 'autoload.php';
$nameU = $_POST['name'];
$email = $_POST['email'];
$content = $_POST['message'];
$phoneU = $_POST['phone'];
$file_name = $_POST['file'];
$mail = new PHPMailer; //From email address and name
$mail->From = $email ;
$mail->FromName = $nameU; //To address and name
$mail->addAddress("jaroslaw.mor#gmail.com", "Vomo");//Recipient name is optional
$mail->isHTML(true);
$mail->Subject = "Zapytanie ze strony www";
$mail->Body = "Telefon:$phoneU<br>$content";
$mail->AltBody = "Telefon:$phoneU\n$content";
$mail->addAttachment("uploads/".$file_name);
if(!$mail->send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent successfully";
}```
Related
I've got problem with attachment name, if there are polish language diacritical signs, it won't display those signs.
I added just below php mailer class $mail->CharSet = 'UTF-8'; and it works for email body text( without it I had the same problem), but not for attachments. Also I had a similar problem with user name, but solved it with utf8_decode() function. Unfortunately that function is not working with attachment's name in my case.
<?php
//Import PHPMailer classes into the global namespace
//These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
//Load Composer's autoloader
require 'autoload.php';
//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);
//$mail->addCustomHeader('Content-Type', 'text/plain;charset=utf-8');
$mail->CharSet = 'UTF-8';
$mail->Encoding = 'base64';
$honeypot = $_POST['honey'];
$user_name = utf8_decode($_POST['name']);
$user_email = $_POST['email'];
$user_message = $_POST['message'];
$user_phone = $_POST['phone'];
$honeypot = trim($_POST["honey"]);
$max_size = 2 * 1024 * 1204; //2mb
$attachment = $_FILES['uploaded-file'];
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if(!empty($honeypot)) {
echo "NO SPAM!";
exit;
} else {
$mail = new PHPMailer; //From email address and name
$mail->isMail();
//sender
$mail->From = $user_email;
$mail->FromName = $user_name;
//recipient
$mail->addAddress("jaroslaw.mor#gmail.com");
//mail subject
$mail->Subject = "Zapytanie ze strony www";
$mail->isHTML(true);
//body mail
$mail->Body = "Telefon:$user_phone<br><br>Treść wiadomośći:<br>$user_message";
$mail->AltBody = "Telefon:$user_phone\n$content";
//attachment
if(isset($attachment)) {
for ($i = 0; $i < count($_FILES['uploaded-file']['name']); $i++) {
if ($_FILES['uploaded-file']['error'][$i] !== UPLOAD_ERR_OK) continue;
$file_TmpName = $_FILES['uploaded-file']["tmp_name"][$i];
$file_name = utf8_decode( $_FILES['uploaded-file']["name"][$i]);
if ($_FILES['uploaded-file']['size'][$i] > $max_size) {
echo "file is too big";
die();
}
else{
move_uploaded_file($file_TmpName, "uploads/" . $file_name);
$mail-> AddAttachment("uploads/". $file_name);
}
}//for
}//isset
if(!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
exit();
}
else {
header("Location: sent.html");
exit();
}//if send else
}//honey else end
}//post end
You can try to add this code before attachment upload:
setlocale( LC_ALL, "pl_PL.UTF-8" );
Try force encoding for attachment:
$mail->AddAttachment("uploads/", $file_name, $mail::ENCODING_BASE64, PHPMailer::filenameToType($file_name).'; charset=utf-8');
I tried all methods from here - without a positive result. By the accident I opened PHPMailer.php and noticed that there are some options connected with charset encoding, changed settings there and now it works.
Thx all for clues.
Here my php code for contact form using PhpMailer.
<?php
use PHPMailer\PHPMailer\PHPMailer;
include_once "PHPMailer/PHPMailer.php";
include_once "PHPMailer/Exception.php";
include_once "PHPMailer/SMTP.php";
if (isset($_POST['submit'])) {
$subject = $_POST['subject'];
$email = $_POST['email'];
$message = $_POST['message'];
echo $subject . "<br/>";
echo $email . "<br/>";
echo $message . "<br/>";
$mail = new PHPMailer();
$mail->addAddress('example#gmail.com');
$mail->setFrom($email);
$mail->Subject = $subject;
$mail->isHTML(true);
$mail->Body = $message;
if ($mail->send())
echo "Your email has been sent, thank you!";
else
echo "Please try again!";
}
?>
It shows "Your email has been sent, thank you", but does not send anyting.
How can solve it?
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!';
}
?>
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"); ?>
I'm trying to create a contact form using phpMailer and I get in firebug this:
NetworkError: 500 Internal Server Error - path/process.php
uncaught exception: [object Object]
each time I'm trying to run the code below.
Please note that the error is not shown anymore if I remove $mail->AddAddress line, that's why I suspect this line to be the cause.
Instead a new error is displayed: You must provide at least one recipient email address when I remove it.
<?php
$name = $_POST['firstName'];
$email = $_POST['email'];
require("phpmailer.inc.php");
$mail = new PHPMailer(true);
$mail->IsMail();
$mail->From = $email;
$mail->Subject = "subject ";
$mail->Body = "From $name with email: $email";
$mail->WordWrap = 50;
$mail->AddAddress('my_address#gmail.com','my name');
$mail->Send();
?>
Thanks!
Rewrite it to:
<?php
$name = $_POST['firstName'];
$email = $_POST['email'];
require 'phpmailer.inc.php';
try {
$mail = new PHPMailer(true);
$mail->IsMail();
$mail->From = $email;
$mail->Subject = "subject ";
$mail->Body = "From $name with email: $email";
$mail->WordWrap = 50;
$mail->AddAddress('my_address#gmail.com','my name');
$mail->Send();
} catch (Exception $e) {
echo $e->getMessage();
}
and you will catch the error