PHPMailer Master not sending Email - php

I am trying to send an email with a .PDF attached using PHPMailer Master.
I am receiving no email, seeing no errors and cannot understand what's going on.
Here is my code:
$url = ''. $_SERVER['DOCUMENT_ROOT'] .'/free-quote/';
$mail = new PHPMailer;
$mail->setFrom('removed#email.com', 'Company Name');
$mail->addAddress(''. $email2 .'', ''. $full_name .'');
$mail->Subject = 'Website Quotation ('. $quotation_ref .')';
$mail->addStringAttachment(file_get_contents($url), ''. $quotation_ref .'.pdf');
$mail->Body = 'Hi! This is my first e-mail sent through PHPMailer.';
I have also included the required file:
require('PHPMailer-master/class.phpmailer.php');
Ideas?

From the PHPMailer Docs:
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
https://github.com/PHPMailer/PHPMailer

Related

Won't send an e-mail

So I'm using PHPAutoload to send an e-mail. However the mail won't send. I made a contact form where I ask for your name, subject and message and implemented that into my php code. Can anybody help me?
Thanks in advance.
<?php
include(HPHMailerAutoload);
$result="";
if(isset($_POST['submit'])){
require 'PHPMailerAutoload.php';
$mail->Host='smtp.gmail.com';
$mail->Port=587;
$mail->SMTPSecure='tls';
$mail->Username='test#test.com';
$mail->Password='******';
$mail->setFrom($_POST['email'], $_POST['name']);
$mail->addAddress('email');
$mail->addReplyTo($_POST['email'], $_POST['name']);
$mail->isHTML(true);
$mail->Subject='Test ';
$mail->Body='Test';
}
?>
You forgot to call send() so add this code after $mail->Body='Test';
if (!$mail->send()) {
echo 'Mailer Error: ' . $mail->ErrorInfo; // you can redirect to an error page
} else {
echo 'Message sent!'; // you can redirect to a thank page
}

$mail->send() in PHPMailer is returning true but I didn't receive mail

I'm using PHPMailer to send emails from my website but $mail->send() is returning true and mail is not sending. No error is reporting in my error log. I hosted my site in Bigrock. I didn't find any errors in my code.
<?php
if(isset($_POST['submit']))
{
require 'class.smtp.php';
require 'PHPMailerAutoload.php';
ini_set('SMTP','localhost' );
ini_set('sendmail_from', 'exmaple#gmail.com');
$fromrec=$_POST['from'];
$from="example#gmail.com";
$subject=$_POST['sf'];
$message=$_POST['message'];
$mail = new PHPMailer;
$mail->IsSMTP();
$mail->Host = "localhost";
$mail->setFrom($from, 'Rahul');
$mail->addAddress("example1#gmail.com");
$mail->Subject = $subject;
$mail->Body = "From:".$fromrec."".$message;
if(!$mail->send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else if($mail->send()) {
echo 'Message has been sent.';
echo $mail->ErrorInfo;
}
else
{
echo 'Mailer error: ' . $mail->ErrorInfo;
}
}
?>
On this issue, I have consulted Bigrock customer care and I made a chat with them. It's simple in the above code the from address email id must be domain specific and to address would be anything. After changing the email id to domain specific and Host to mail.example.com. My problem has been solved. If anyone got the same problem please try this.

PHPMailer variable in addadress

I have a PHP mailer function set up to send a mail to a customer that purchases a file. IT works perfect if I use a static emailaddress in the AddAdress method. But if I use a variable it doesn't get sent.
function sendEmailToClient($name, $address, $postcode, $city, $email, $which, $orderid, $downloadlink){
$mail = new PHPMailer;
// $mail->isSMTP();
// $mail->Host = 'localhost';
$mail->setFrom('email#email.com', 'My Store');
$mail->addAddress($email); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Thanks for your order';
$mail->Body = '<body>
<p>Order:</p>
<p>Order ID: '.$orderid.'</p>
<p>Book: '.$which.'<br />
Name: '.$name.'<br />
Address: '.$address.'<br />
Postal code: '.$postcode.'<br />
City: '.$city.'<br/>
Email: '.$email.'</p>
<p>Download: '.$downloadlink.'</p>
<p>Have fun!!</p>
</body>';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
}
I don't understand why the variable doesn't work. I checked the input from the function to see if the email is actually a valid email, and that is the case. But it's not working with the variable.
Anyone know a solution?

PHPMailer sends duplicate email

PHPMailer
<?php
$mail = new PHPMailer;
$mail->IsSMTP();
$mail->IsHTML(true);
$mail->SMTPSecure = "tls";
$mail->Mailer = "smtp";
$mail->Host = "smtp.office365.com";
$mail->Port = 587;
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "xxx";
$mail->Password = "xxx";
$mail->setFrom('xxx', 'Website');
//Send to Admin
$AdminEmail = 'admin.example#gmail.com';
$mail->AddAddress($AdminEmail, $AdminName);
$mail->Subject = "This is an email";
$mail2 = clone $mail;
$body = 'Hi Admin. This is an email';
$mail->Body = $body;
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
//Send to User
$UserEmail = 'user.example#gmail.com';
$mail2->AddAddress($UserEmail, $UserName);
$body2 = 'Hi User. This is an email';
$mail2->Body = $body2;
if(!$mail2->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail2->ErrorInfo;
} else {
echo 'Message has been sent.';
}
?>
I have an issue where when i send the emails, Admin will receive $mail and $mail2 when they supposed to just receive $mail. While there's no problem with User. Works fine by receiving $mail2. I've tried to put $mail->ClearAddresses(); but it's still the same. By chance, what is the problem?
Clone the email variable before adding the address and stuff of the admin. (As suggested in the comments)
You need to create different-different object for both admin and user email
//Send to Admin
$mail = new PHPMailer;
$mail->IsHTML(true);
$AdminEmail = 'admin.example#gmail.com';
$mail->AddAddress($AdminEmail, $AdminName);
$mail->Subject = "This is an email";
$mail2 = clone $mail;
$body = 'Hi Admin. This is an email';
$mail->Body = $body;
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
//Send to User
$mail = new PHPMailer;
$mail->IsHTML(true);
$UserEmail = 'user.example#gmail.com';
$mail->AddAddress($UserEmail, $UserName);
$body2 = 'Hi User. This is an email';
$mail->Body = $body2;
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}

Send email in plain text, html with input file attachment

EDIT:
The basic idea for sending email with PHPmailer was perfect. But I'm having issue attaching files (in different) format.
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.domain.com"; // SMTP server
$mail->From = "frommail#domain.com";
$mail->FromName="fromname";
$mail->AddAddress("admin#domain.com");
$mail->IsHTML(true);
//for file attachments
$mail->AddAttachment($_FILES['uploaded_file']['tmp_name'], $_FILES['uploaded_file']['name']);
$mail->Subject = "First PHPMailer Message";
$mail->Body = "<b>Hi! <br><br> This is my first e-mail sent through PHPMailer.</b>";
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
//$mail->WordWrap = 50;
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
I tried to display the sequence but not a help. Much appreciated for your time.

Categories