php mailer cannot send email to anyone except myself
Note : Please change the SMTP server setting to your own server.
$your_email = "info@example.com";
$your_smtp = "mail.example.com";
$your_smtp_user = "info@example.com";
$your_smtp_pass = "example_password";
$your_website = "http://example.com";
//get contact form details
$name = $_POST['name'];
$email = $_POST['email'];
$url = $_POST['url'];
$comments = $_POST['comments'];
$response="Name: $name\nContents:\n$comments\n";
$mail = new PHPmailer();
$mail = $mail->SetLanguage("en", "phpmailer/language");
$mail->From = $your_email;
$mail->FromName = $your_website;
$mail->Host = $your_smtp;
$mail->Mailer = "smtp";
$mail->Password = $your_smtp_pass;
$mail->Username = $your_smtp_user;
$mail->Subject = "$your_website feedback";
$mail->SMTPAuth = "true";
$mail->Body = $response;
$mail->AddAddress($your_email,"$your_website admin");
$mail->AddReplyTo($email,$name);
echo "<p>Thanks for your feedback, <em>$name</em>! We will contact you soon!</p>";
if (!$mail->Send()) {
echo "<p>There was an error in sending mail, please try again at a later time</p>";
}
$mail->ClearAddresses();
$mail->ClearAttachments();
PHPMailer not sending all emails to last email address
Because you're reusing the $mail
object to addAddress()
and send()
. So the first time you call phpmaileremail()
the first address gets the email. Then when you call it for the second time the second address is added and the first and second address get the email. And so on.
A simple solution would be to create the $mail
object inside the phpmaileremail()
function:
function phpmaileremail($reciever,$usertype, $file, $emailsubject, $email_body )
{
$mail = new PHPMailer(true);
$mail->SMTPDebug = 0;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com;';
$mail->SMTPAuth = true;
$mail->Username = 'XXXXXXXX@gmail.com';
$mail->Password = 'XXXXXXXXXXXXXXXXXX';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('XXXXXXXXXXXXXXXX', 'XXXXXXXXXXXXXX');
$mail->addAddress($reciever);
$mail->addAddress($reciever, $usertype);
// Attachments
$mail->addAttachment($file); // Add attachments
$mail->isHTML(true);
$mail->Subject = $emailsubject;
$mail->Body = $email_body;
$mail->AltBody = 'NA';
$mail->send();
echo "Mail sent";
}
PS: Not that it matters, but reciever
is written receiver
. I've made that mistake as well.
Can not send email with php mailer using gmail account
if you use ssl then change port 587 to 465
$mail->Port = 465;
$mail->SMTPSecure = 'ssl';
https://support.google.com/a/answer/176600?hl=en
Related Topics
Why Are $_Server["Php_Auth_User"] and $_Server["Php_Auth_Pw"] Not Set
How to Easily Remove the Last Comma from an Array
PHP String in Array Only Returns First Character
How to Print the Actual Query That MySQLi->Execute() Makes
How to Get Values of Xml Elements
How to Design a Hierarchical Role Based Access Control System
PHP Error: "The Zip Extension and Unzip Command Are Both Missing, Skipping."
How to Retrieve Youtube Video Details from Video Url Using PHP
Cosine Similarity VS Hamming Distance
Fatal Error: Call to Undefined Function: Ldap_Connect()
How to Get PHPunit Mockobjects to Return Different Values Based on a Parameter
How to Enable Shell_Exec and Exec on PHP
Converting Named HTML Entities to Numeric HTML Entities