error:"couldn't instantiate mail function" - php

I am using Windows. When I try to send mail using php mailer. It returns "couldn't instantiate mail function". Is it some permission issue or is there anything wrong with code? Thanks!
<?php
require_once("class.phpmailer.php");//location is correct
require_once("class.smtp.php");//location is correct
$to_name = "Good Better";
$to = "sending#example.com";
$subject = "Mail Test at ".strftime("%T", time());
$message = "This is a test.";
$message = wordwrap($message,70);
$from_name = "My Name";
$from = "myacc#example.com";
$mail = new PHPMailer();
$mail->FromName = $from_name;
$mail->From = $from;
$mail->AddAddress($to, $to_name);
$mail->Subject = $subject;
$mail->Body = $message;
$result = $mail->Send();
echo $result ? 'Sent' : $mail->ErrorInfo;
?>

Related

Sending mail to both sender and receiver using PHPmailer

I am using phpmailer in live server for sending emails for both sender and receiver. Its working fine but I want to include additional message in sender copy as "thanks for registering with us". I am unable to do it.
Could you please help with this ?
Code I tried So far:
<?php
$msg = "";
if (isset($_POST['submit'])) {
require 'phpmailer/PHPMailerAutoload.php';
function sendemail($to, $from, $fromName, $body) {
$mail = new PHPMailer();
$mail->setFrom($from, $fromName);
$mail->addAddress($to);
$mail->Subject = 'Contact Form - Email';
$mail->Body = $body;
$mail->isHTML(false);
return $mail->send();
}
function sender_mail($to, $from, $fromName, $body) {
$mail = new PHPMailer();
$mail->setFrom($from, $fromName);
$mail->addAddress($to);
$mail->Subject = 'Copy Contact Form - Email';
$mail->Body = $body . 'Thanks for registering with us';
$mail->isHTML(false);
return $mail->send();
}
$name = $_POST['username'];
$email = $_POST['email'];
$body = $_POST['body'];
sendemail('abc#domain.com', 'xyz#domain.com', $name, $body);
sender_mail('abc#domain.com', $email, $name, $body);
}?>
I always create different mail object for sending another email. So it will always pick new message.
For sending another email you can update like below:
$mail2 = new PHPMailer();
$mail2->setFrom($from, $fromName);
$mail2->addAddress($to);
$mail2->Subject = 'Copy Contact Form - Email';
$mail2->Body = $body . 'Thanks for registering with us';
$mail2->isHTML(false);
return $mail2->send();
2nd option to use clearAddresses above your second email to clear your earlier messages like below:
$mail->clearAddresses();
$mail = new PHPMailer();
$mail->setFrom($from, $fromName);
$mail->addAddress($to);
$mail->Subject = 'Copy Contact Form - Email';
$mail->Body = $body . 'Thanks for registering with us';
$mail->isHTML(false);
return $mail->send();

Cant set sender name phpmailer

So I'm setting up a script to send emails from a contact us form. Everything is working great besides the fact that I can't set sender's name properly. I get an error:
Warning: Creating default object from empty value
My code looks like this:
<?php $eemail = $_REQUEST['email'] ;
$message = $_REQUEST['message']; $subject = $_REQUEST['subject']; $fname =
$_REQUEST['name'];
require("class.phpmailer.php"); $mail = new PHPMailer();
$mail->Username = "email"; // SMTP username
$mail->Password = "password"; // SMTP password $mail->IsSMTP();
$mail->SMTPAuth = true; $mail->Host = 'aspmx.l.google.com';
$mail->Port = 25;
$mail->From = $eemail; $name->FromName = $fname; $mail->Subject =
$subject;
$mail->AddAddress("email", "name");
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Body = $message; $mail->AltBody = $message;
if(!$mail->Send()) { echo "Message could not be sent. <p>"; echo
"Mailer Error: " . $mail->ErrorInfo; exit; }
echo "Message has been sent"; ?>
add this code
$mail->SetFrom("$eemail ", "$fname");

PHP mailer Error receiving an email

I am using the code below for the mailing list of the website I am working on, and it's working fine on my localhost using my credentials and on some other server using some other credentials were given to me.
<?php
if(isset($_POST['submit']))
{
require("class.phpmailer.php");
$mail = new PHPMailer();
$query = "SELECT * FROM mailing_list";
$result = mysqli_query($connect, $query)
or die('Error querying the database!');
$mail->IsSMTP();
$mail->Host= "smtp";
$mail->Username= "some email address was given to me";
$mail->Password= "password";
$mail->SMTPAuth= true;
$mail->SMTPSecure = 'tls';
$mail->From ="some email address was given to me";
$mail->FromName ="CSCA";
while($row = mysqli_fetch_array($result))
{
$first_name = $row['first_name'];
$last_name = $row['last_name'];
$to = $row['email'];
$mail->ClearAddresses();
$mail->AddAddress($to);
$mail->Subject= $_POST['subject'];
$mail->Body = "Dear $first_name $last_name,\n" . $_POST['body'];
$mail->WordWrap = 50;
if(!$mail->Send())
{
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
}
Now when I use the following code for the contact us page of the same website it works fine one my localhost using my own credentials, but it doesn't work on the other server I used above. I got a message in the spam 'mail delivery failed'. I don't know how with the same credentials you can send mail but you can't receive! I mean with the same server and same credentials you the mailing list works fine but the contact us form gives an error. Is there a way to fix that??
Thanks in advance!
<?php
require("class.phpmailer.php");
$mail = new PHPMailer();
$name = $_REQUEST['name'];
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
$mail->IsSMTP();
$mail->Host="smtp";
$mail->Username="some email address was given to me";
$mail->Password="password";
$mail->SMTPAuth=true;
$mail->SMTPSecure = 'tls';
$mail->From =$email;
$mail->FromName =$name;
$mail->AddAddress("some email address was given to me", "Some name");
$mail->IsHTML(true);
$mail->Subject = "You have received feedback from your website!";
$mail->Body = $message;
$mail->AltBody = $message;
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}

PHPMail - can I send two unique emails

I'm using PHPMailer to successfully send an email upon a webform submission (so just using basic post data, no mysql databases or any lookups).
What I need to do is send two seperate emails, one version for the customer and the other for a customer service agent - so that when a user completes a webform, they will receive a 'marketing' version of the email, whilst the customer service agent will receive an email just containing the details submitted by the user.
See below what im using at the moment, but not sure how to best implement to send the secoind email? It presently fails and the script exits after sending only one email.
$body = ob_get_contents();
$to = 'removed';
$email = 'removed';
$fromaddress = "removed";
$fromname = "removed";
require("phpmailer.php");
$mail = new PHPMailer();
$mail->From = $fromaddress;
$mail->FromName = $fromname ;
$mail->AddAddress("email#example.com");
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = "Subject";
$mail->Body = $body;
$mail->AltBody = "This is the text-only body";
if(!$mail->Send()) {
$recipient = 'email#example.com';
$subject = 'Contact form failed';
$content = $body;
mail($recipient, $subject, $content,
"From: mail#yourdomain.com\r\nReply-To: $email\r\nX-Mailer: DT_formmail");
exit;
}
//Send the customer version
$mail=new PHPMailer();
$mail->SetFrom('email', 'FULLNAME');
$mail->AddAddress($mail_vars[2], 'sss');
$mail->Subject = "Customers email";
$mail->MsgHTML("email body here");
//$mail->Send();
In the customer version you are not setting the email's properties the same way you are in the first. For example you use $mail->From = $fromaddress; in the first and $mail->SetFrom('email', 'FULLNAME'); in the second.
Because you instantiated a new $mail=new PHPMailer(); you need to set the properties again.
Instead of just bailing out with a useless "something didn't work" message, why not have PHPMailer TELL you what the problem is?
if (!$mail->send()) {
$error = $mail->ErrorInfo;
echo "Mail failed with error: $error";
}

PHPMailer doesn`t work with AddAddress property set

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

Categories