I'm using Migadu mail server and PHP Mailer to set up a simple contact form.
This is the PHP file:
<?php
require("includes/class.phpmailer.php");
require("includes/class.smtp.php");
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$smtpHost = "smtp.migadu.com";
$smtpUsername = "info#mywebsite.com";
$smtpPassword = "mypassword";
$to = 'info#mywebsite.com';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPSecure = "tls";
$mail->SMTPAuth = true;
$mail->Port = 587;
$mail->IsHTML(true);
$mail->CharSet = "utf-8";
$mail->Host = $smtpHost;
$mail->Username = $smtpUsername;
$mail->Password = $smtpPassword;
$mail->From = $smtpUsername;
$mail->FromName = $name;
$mail->AddAddress($to);
$mail->AddReplyTo($email);
$mail->Subject = "Contact Form";
$body = '<h1>Contact!</h1>';
$body .= '<p><b>Name: </b>'.$name.'</p>';
$body .= '<p><b>Email: </b>'.$email.'</p>';
$body .= '<p><b>Message: </b>'.$message.'</p>';
$mail->Body = $body;
$sentStatus = $mail->Send();
if($sentStatus){
echo json_encode(['status' => 'ok']);
}else{
echo json_encode(['status' => 'error', 'errorType' => 'server']);
}
?>
But it's not sending the mail. Also, it's not throwing any error, it just stuck in loading forever.
It's not showing any error because you're not displaying any errors - look in the ErrorInfo property. All of the examples provided with PHPMailer do this, so go look at them to see how to do that.
It's most likely that it's not "stuck forever", it's just that the timeout is long, and that's probably because your ISP blocks outbound SMTP, which is very common. The troubleshooting guide tells you how to diagnose this. Your ISP probably has an alternative method for sending email, for example they may provide their own relay, so you should refer to their docs.
Related
Good day. I need to send two different messages to two different people (user and admin). Tell me how to do this?
My mail php
<?php
require_once('phpmailer/PHPMailerAutoload.php');
$mail = new PHPMailer;
$mail->CharSet = 'utf-8';
$name = $_POST['name'];
$email = $_POST['email'];
$mail->isSMTP();
$mail->Host = 'smtp.mail.ru';
$mail->SMTPAuth = true;
$mail->Username = 'pmewilberries#mail.ru';
$mail->Password = '123456789456';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->setFrom('pmewilberries#mail.ru');
$mail->addAddress($email);
$mail->Subject = 'Заявка на участие в интенсиве Wildberries';
// $mail->Body($body);
// $mail->isHTML(true);
$body = 'Hello'
$mail->msgHTML($body);
$mail->send()
?>
Send one message, change the properties that are different, then send the second one:
$mail->addAddress($email);
$body = 'Hello';
$mail->msgHTML($body);
$mail->send();
$mail->clearAddresses();
$mail->addAddress($email2);
$body = 'Hello2';
$mail->msgHTML($body);
$mail->send();
It will be slightly quicker if you set keepalive, which will make it re-use the existing connection for the second message:
$mail->SMTPKeepAlive = true;
Try this,
$mail->addAddress($email);
$body = 'Hello';
$mail->msgHTML($body);
$mail->send();
$mail->ClearAllRecipients();
$mail->addAddress($email2);
$body = 'Hello2';
$mail->msgHTML($body);
$mail->send();
Look at the code for clearAllRecipients;
I am trying to send emails using SMTP Server in Core HP. I am getting a blank page not displaying any errors. Can any one help me out? what i am doing wrong? Unable to check the issue as well!
<?php ob_start();
if(isset($_POST['submit_contact'])) {
require 'PHPMailer/PHPMailerAutoload.php';
$to = "abc#gmail.com";
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$txt = $_POST['comment'];
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->IsHTML(true);
$mail->Username = "abc#gmail.com";
$mail->Password = "password1#3";
$mail->SetFrom('$email');
$mail->Subject = $subject;
$mail->Body = $txt;
$mail->AddAddress($to);
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message has been sent";
}
}
?>
is your source code located at public_html folder?
In that case you have to include like below
require 'aaa/PHPMailer/PHPMailerAutoload.php';
EDIT: Somehow the code only works on the webserver and not on localhost, so there's your "fix".
So, my php code works as it should like this:
require 'PHPMailer/PHPMailerAutoload.php';
$nome = $_POST["nome"];
$subject = $_POST["subject"];
$message = $_POST["message"];
$email = $_POST["email"];
$contacto = $_POST["contacto"];
$body = "<p>Mail recebido de: </p>" . $nome . "<p>Email:</p>" . $email . "<p> Contacto:</p>" . $contacto . "<p>Mensagem:</p>" . $message ;
$mail = new PHPMailer();
$mail->CharSet = "text/html; charset=UTF-8;";
$mail->SMTPDebug = 0; // debugging: 1 = errors and messages, 2 = messages only
$mail->CharSet = 'UTF-8';
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->Username = "the email";
$mail->Password = the password;
$mail->SetFrom('the email', 'Organizer');
$mail->Subject = "[ORÇAMENTO] " . $subject;
$mail->Body = $body;
$mail->IsHTML(true);
$mail->AddAddress("the email");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
$sucesso = 'Mensagem enviada com sucesso!';
}
this first script sends the email to itself, but even that doesn't work here ($mail->Send() = 1, still I don't receive the email):
require '../PHPMailer/PHPMailerAutoload.php';
$body = "<p>Car(a) " . ${'user' . $i} . ",</p> <p> Foi lhe associado(a) uma nova ocorrência. Consulte-a em http://www.organizer.com.pt . </p>";
$mail = new PHPMailer();
$mail->CharSet = "text/html; charset=UTF-8;";
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->CharSet = 'UTF-8';
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->Username = "the email";
$mail->Password = the password;
$mail->SetFrom('the email', 'Organizer');
$mail->Subject = "Nova Ocorrência";
$mail->Body = $body;
$mail->IsHTML(true);
$mail->AddAddress($email);
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
$sucesso = 'Mensagem enviada com sucesso!';
}
I have tested all the variables that I get from the database and so on and they are all correct, I just don't receive the email for some reason. Anyone can help?
P.S.: Sorry some of this is in portuguese, I think the code is still perfectly understandable.
"Doesn't work" is not a useful problem description. On SO, detail is everything.
It doesn't help that you've based your code on an obsolete & incorrect example and have not read the docs, and I suspect you are using an old version of PHPMailer too since it would have provided you with a link to relevant docs if it was a recent version. So before anything else, get the latest version and read the docs.
As for specific problems: SMTPSecure = 'ssl' with Port = 587 will not work. Port 587 is used for SMTP+STARTTLS, not implicit SSL, so set SMTPSecure = 'tls'.
This is just wrong:
$mail->CharSet = "text/html; charset=UTF-8;";
Your other script does it right:
$mail->CharSet = 'UTF-8';
If you're having trouble with something that has a debug option, enable it!
$mail->SMTPDebug = 2;
Increase that to 3 if you have initial connection problems.
I'd like to know where you got that example code so I can ask the authors to delete or update it; having broken, obsolete examples lying around just wastes everybody's time.
I have done a contact form and obviously I would like to receive a confirmation email when a user fill out this form on my website.
I'm using PHPMailer. When I test the code on my computer as localhost, everything works fine and confirmation message is received properly.
Here you are the PHP script:
<?php
$Name= $_POST['Name'];
$Email = $_POST['Email'];
$Message = $_POST['Message'];
if ($Name=='' || $Email=='' || $Message==''){
echo "<script>alert('Please fill out mandatory fields');location.href ='javascript:history.back()';</script>";
}else{
require("archivosformulario/class.phpmailer.php");
$mail = new PHPMailer();
$mail->From = $Email;
$mail->FromName = $Name;
$mail->AddAddress("myemail#gmail.com");
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = "Contact Form";
$mail->Body = "Name: $name \n<br />".
"Email: $Email \n<br />".
"Message: $Message\n<br />";
// SMTP Server
$mail->IsSMTP();
$mail->SMTPDebug = 2;
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->Port = 465;
$mail->SMTPSecure = "ssl";
$mail->Username = "myemail#gmail.com";
$mail->Password = "mypassword";
if ($mail->Send())
echo "<script>alert('We have received your message. We will contact you shortly.');location.href ='javascript:history.back()';</script>";
else
echo "<script>alert('Error');location.href ='javascript:history.back()';</script>";
}
?>
The problem is when I upload this PHP script on a web hosting (i.e 123-reg.co.uk), it doesn´t work.
Error message:
SMTP Error: Data not accepted
Here you are the PHP script:
<?php
$Name= $_POST['Name'];
$Email = $_POST['Email'];
$Message = $_POST['Message'];
if ($Name=='' || $Email=='' || $Message==''){
echo "<script>alert('Please fill out mandatory fields');location.href ='javascript:history.back()';</script>";
}else{
require("archivosformulario/class.phpmailer.php");
$mail = new PHPMailer();
$mail->From = $Email;
$mail->FromName = $Name;
$mail->AddAddress("email#mydomainat123-reg.com");
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = "Contact Form";
$mail->Body = "Name: $name \n<br />".
"Email: $Email \n<br />".
"Message: $Message\n<br />";
// SMTP Server
$mail->IsSMTP();
$mail->SMTPDebug = 2;
$mail->Host = "smtp.123-reg.co.uk";
$mail->SMTPAuth = true;
$mail->Port = 25;
$mail->Username = "email#mydomainat123-reg.com";
$mail->Password = "password";
if ($mail->Send())
echo "<script>alert('We have received your message. We will contact you shortly.');location.href ='javascript:history.back()';</script>";
else
echo "<script>alert('Error');location.href ='javascript:history.back()';</script>";
}
?>
I have tried uploading and executing this PHP script from another free web hosting but it was to not avail.
I'm kind of new in PHP, especially in dealing with mail functions.
What can be the reason? Any additional help would be appreciated.
Thanks in advance for your help.
Your server doesn't allow different sender and username you should config: $mail->From like $mail->Username
Moreover...
Most times I've seen this message the email gets successfully sent anyway, but not always. To debug, set:
$mail->SMTPDebug = true;
You can either echo the debug messages or log them using error_log():
// 'echo' or 'error_log'
$mail->Debugoutput = 'echo';
A likely candidate especially on a heavily loaded server are the SMTP timeouts:
// default is 10
$mail->Timeout = 60;
class.smtp.php also has a Timelimit property used for reads from the server!
Hope this information helps! :)
Hello i need help on PHPMailer here is my code :
$message = 'main message';
$bccmessage = 'BCC Message';
include '../inc/class.phpmailer.php';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->IsHTML(true);
$mail->CharSet = "UTF-8";
$mail->Host = "smtphost";
$mail->SMTPAuth = true;
$mail->Username = "email#domain.com";
$mail->Password = "xxxxxx";
$mail->From = "email#domain.com";
$mail->FromName = "foo.com";
$mail->AddAddress($mainemail);
$mail->AddBCC($bccemail);
$mail->AddBCC($bccemail);
$mail->Subject = "Subject";
$mail->Body = "$message";
if(!$mail->Send())
{
echo '<pre>Error: '.$mail->ErrorInfo.'</pre>';
exit;
} else {
//Display result
echo '<div class="success">message Sent</div>';
}
My question is how can i manage that the "AddAddress" get $message and the "AddBCC" get the $bccmessage message.
You will have to send 2 separate emails to accomplish that.
You could do something like:
$oMail->Body = $sToMessage;
$oMail->addAddress($sToEmail);
$oMail->send();
// clear
$oMail->ClearAddresses();
$oMail->Body = $sBccMessage;
$oMail->addAddress($sBccEmail);
$oMail->send();
But I'd have to advice against it. You would do better to wrap the email sending in an function - and calling it twice with different parameters.