I want to use Mandrill for sending my emails. I am customizing my phpmailer which I had used to send the mail with mandrill phpmailer. (found here: http://help.mandrill.com/entries/23737696-How-do-I-send-with-PHPMailer- )
require 'mandrillmailer/class.phpmailer.php';
$mail = new PHPMailer();
$mail->IsSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.mandrillapp.com'; // Specify main and backup server
$mail->Port = 587; // Set the SMTP port
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'MANDRILL_USERNAME'; // SMTP username
$mail->Password = 'MANDRILL_APIKEY'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
$mail->From = 'from#example.com';
$mail->FromName = 'Your From name';
$mail->AddAddress($to); // Add a recipient
$mail->IsHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $message;
echo $mail->Subject;
//$mail->Send();
//$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->Send()) {
echo 'hiii';
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
else{
echo 'hello';
echo 'Message has been sent';
}
die('here');
I am not getting response messages i.e mail is sent or not. I debugged where it is getting stuck by echo statements. I can see the messages till echo $mail->Subject; but not beyond that. I am guessing that $mail->Send() is not working that's why messages beyond that are not displaying and email sending is not working.
How do I fix that?
Assuming you've put in the proper authorization, try it by requiring the autoloader rather than phpmailer class. This worked for me:
require 'mandrillmailer/PHPMailerAutoload.php';
Alternatively you could try something a little easier. Something like the official Mandrill PHP client or a separate service like sendwithus.
First of all check the PHPMailer class version If Its below Version 5 then use the latest version 5.2.9
My issue resolved as I used the Version 5.2.9
Related
Could not instantiate mail function.
Message couldnot be sentMailer Error: Could not instantiate mail function.
require 'PHPMailer/PHPMailerAutoload.php';
$mail =new PHPMailer;
$mail->HOST ='localhost';
$mail->PORT = 25;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Username = 'Username';
$mail->Password = 'Pass';
$mail->setFrom('info#zoeticpolymers.com');
$mail->addAddress('nitesh54546#gmail.com','Nitesh');
$mail->AddReplyTo("info#zoeticpolymers.com");
$mail->isHTMl(true);
$mail->Subject = 'PHP Mailer Subject';
$mail->Body = '<h1>You are Welcome Here.....</h1>';
if(!$mail->send()){
echo 'Message couldnot be sent';
echo 'Mailer Error: ' . $mail->ErrorInfo; die;
}else{
echo 'Message has been sent'; die;
}
Have you tried with smtp? The cause of Could not instantiate mail function error may be more than one reason. when you try to send large emails and your PHP error log contains the message Can not send a message: Too big then your mail transfer agent (Sendmail, postfix, Exim, etc) is refusing to deliver these emails.
The solution is to configure the MTA to allow larger attachments. But this is not always possible. The alternate solution is to use SMTP. You will need access to a SMTP server (and login credentials if your SMTP server requires authentication), consider the given example.
$mail->IsSMTP();
$mail->Host = "smtp.example.com";
// used only when SMTP requires authentication
$mail->SMTPAuth = true;
$mail->Username = 'smtp_username';
$mail->Password = 'smtp_password';
Your Mistake: You used HOST as Localhost it's SMTP.mail.com change mail to your server
You are using older version of PHPMailer also let me help you by typing the correct one
You should download from PHPMailer GitHub
Not vendor for this example
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
$mail = new PHPMailer(true);
try {
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com'; //for Gmail
$mail->SMTPAuth = true;
$mail->Username = 'user#gmail.com';
$mail->Password = 'your Gmail pass';
$mail->Port = 587; // TCP port
$mail->setFrom('from#example.com', 'Mailer');
$mail->addAddress('joe#example.net', 'Joe User');
$mail->isHTML(true);
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}```
**Your error was that you used HOST as localhost and older version of PHPMailer.
But use default mail() but change the PHP version to 7.3 since it's better now.**
If your code looks good, try to install PostFix on your server.
sudo apt-get install postfix
It worked for me on digital ocean.
I followed all of the instructions in this question:
SMTP connect() failed PHPmailer - PHP
But still I never succeeded in getting the PHPMailer to work. I searched elsewhere - no solutions.
You can view the results here: https://unidrones.co.za/JuneSecond
When I try to send a test email using my gmail account credentials, it returns the "SMTP connect() failed" error.
I am using this template code:
<?php
require 'PHPMailerAutoload.php';
if(isset($_POST['send']))
{
$email = $_POST['email'];
$password = $_POST['password'];
$to_id = $_POST['toid'];
$message = $_POST['message'];
$subject = $_POST['subject'];
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'ssl://smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = $email;
$mail->Password = $password;
$mail->addAddress($to_id);
$mail->Subject = $subject;
$mail->msgHTML($message);
if (!$mail->send()) {
$error = "Mailer Error: " . $mail->ErrorInfo;
echo '<p id="para">'.$error.'</p>';
}
else {
echo '<p id="para">Message sent!</p>';
}
}
else{
echo '<p id="para">Please enter valid data</p>';
}
?>
Edit: I don't know if there's a new way to send emails through PHP now. All the tutorials and lessons I am using teaches it this way (i.e. using the PHPMailer library).
I had a tough time finding the PHPMailer library that includes the PHPMailerAutoload.php file, which makes me think it's a little outdated or deprecated, but how else would I send emails? I don't know.
The reason you're having a hard time finding PHPMailerAutoload.php is because it's old and no longer supported. Get the latest and base your code on the gmail example provided. If you're new to PHP, learn to use composer.
These three lines are conflicting:
$mail->Host = 'ssl://smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
The ssl:// in Host overrides the tls in SMTPSecure, resulting in it trying to use implicit TLS to a port expecting explicit TLS. Either use ssl with port 465 or tls with port 587, not other combos. Regardless, it appears that's not your problem anyway.
As the troubleshooting guide says about this exact "SMTP connect() failed" error:
This is often reported as a PHPMailer problem, but it's almost always down to local DNS failure, firewall blocking (for example as GoDaddy does) or another issue on your local network. It means that PHPMailer is unable to contact the SMTP server you have specified in the Host property, but doesn't say exactly why.
It then goes on to describe several techniques you can use to try to diagnose exactly why you can't connect. Amazing stuff, documentation.
I tried your form with some random data and saw that you failed to include the most important error message in your question:
2018-06-02 14:47:25 SMTP ERROR: Failed to connect to server: Network is unreachable (101)
2018-06-02 14:47:25 SMTP connect() failed
That suggests your ISP is probably blocking outbound SMTP, so you have your answer - perhaps confirm that using the steps the guide suggests (telnet etc), and refer to your ISP's docs or support.
You also have a major omission - you're not setting a "from" address:
$mail->setFrom('myname#gmail.com', 'My Name');
Note that if you're sending through gmail, you can only use your account's address, or preset aliases (set in gmail prefs), not arbitrary addresses.
Meanwhile, this is a somewhat crazy thing to implement as you have anyway - why would anyone ever enter their gmail credentials on a form like that?
did you try to use the following config?
$mail->Host = 'smtp.gmail.com';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->CharSet = 'UTF-8';
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 4; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'your gamil id'; // SMTP username
$mail->Password = 'gmail password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('set from address', 'name');
$mail->addAddress('recipient address', 'Joe User'); // Add a recipient
$mail->addReplyTo('reply address', 'Information');
// $mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
// $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
I have an ec2 server subscription and I am trying to use SES to send emails using PHPMailer. For some reason, Mails are not being sent from SES server. Mails go from some other server. I spoke with an AWS support member and he confirmed that all the code i have is correct and he cant provide any help with my php code.
His reply was
Thank you for your time speaking today.
As discussed, your a PHP Mailer is not relaying on SES, despite your configuration seems to be correct, it's not triggering SES SMTP Servers.
The line below shows that your message was sent from the EC2 directly, without pass through SES.
Received: from ip-xxxxx.ap-southeast-1.compute.internal (ec2-yyyyyy.ap-southeast-1.compute.amazonaws.com. [yyyyyyy])
The sample PHPMailer code i have is
<?php
require 'vendor/phpmailer/phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->SMTPDebug = 3; // Enable verbose debug output
//$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'email-smtp.us-east-1.amazonaws.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'smtp-username'; // SMTP username
$mail->Password = 'smtp-password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('contact#example.com', 'John Doer');
$mail->addAddress('abc#gmail.com', 'Random'); // Add a recipient
$mail->addAddress('xyz#gmail.com'); // Name is optional
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
Any help is appreciated.
I have a problem, with my code beneath I get this error message:
Message could not be sent.Mailer Error: SMTP Error: The following recipients failed: !Censored!
I have looked into the Host, Port, Username, Password, Recipient and all is correct, what is the problem? Thank you!
Could you please explain too cuz Im new to PHP-coding
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['amne'];
$message = $_POST['message'];
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = '!Censored!'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '!Censored!'; // SMTP username
$mail->Password = '!Censored!'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->From = $email;
$mail->FromName = $name;
$mail->addAddress('!Censored!'); // Add a recipient
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $message;
$mail->AltBody = $message;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
EDIT: I found the problem, the problem was not in the php code itself. It was in the contact form. The problem started when I put the variables as names and emails. If the email is not verified, it does not work.
This error can be caused by a few different things. You can get a better idea of the reason by adding the following line to your PHPMailer script:
$mail->SMTPDebug = 2; //<-- For debugging
Once you do that, you can check the following things that may be causing the error:
1.) A corrupt class.phpmailer.php file.
2.) The error may be caused by protection put in place by your ISP. Check with them.
3.) It could be a problem with the recipient's/sender's email addresses.
4.) Set SMTPAuth to true for PHPMailer class.
5.) Comment out the following line in your PHPMailer script: $mail->isSMTP();
Mostly, There is possibility that Your phpmailer class file is corrupted.
Download the latest version: https://github.com/PHPMailer/PHPMailer
So I've been trying this for a day or two now. At first, I tried the built in codeigniter SMTP mail class, with no luck. In hope of fixing this problem, I turned to PHPMailer. And to my disappointment, there is still no luck.
I'm certain that all details are correct. I've even tried multiple SMTP servers, those of which include gmail and mandrill.
Here's the code that I am using (I have tried many different modified versions of this, but I'll give you the one that I'm currently using)
<?php
class thankyou extends CI_Controller {
function index()
{
$this->load->model('index');
$header = array(
'title' => 'Please Confirm',
'navigation' => $this->index->loadNavigation(),
);
$this->load->view('header', $header);
$this->load->library('My_PHPMailer');
$mail = new PHPMailer();
$mail->IsSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.mandrillapp.com'; // Specify main and backup server
$mail->Port = 587; // Set the SMTP port
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'email#outlook.com'; // SMTP username
$mail->Password = 'password4mandrill'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
$mail->SMTPDebug = 1;
$mail->From = 'hello#whatever.co.uk';
$mail->FromName = 'Whatever';
$mail->AddAddress('hello#whatever.co.uk', 'Josh Adams'); // Add a recipient // Name is optional
$mail->IsHTML(false); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <strong>in bold!</strong>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->Send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
}
}
?>
If need be, you can Test it here
I just get the error 2014-06-09 11:18:40 SMTP ERROR: Failed to connect to server: Connection timed out (110) SMTP connect() failed. Message could not be sent.Mailer Error: SMTP connect() failed.
Try with this:
$this->load->library('email');
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'smtp.mandrillapp.com';
$config['smtp_user'] = 'email#outlook.com';
$config['smtp_pass'] = 'password4mandrill';
$config['smtp_port'] = 587;
$this->email->initialize($config);
$this->email->from('you#outlook.com');
$this->email->to('dest#mail.com');
$this->email->subject('Test');
$this->email->message('Message');
if($this->email->send()) {
echo 'Sent';
} else {
$this->email->print_debugger();
}
Full list of options: http://ellislab.com/codeigniter/user-guide/libraries/email.html
Since Codeigniter have email helper you need not to use alternative way of complexity. You just load the helper and send mail using the following structure.
$name="some name";
$message="your text message";
// set email data
$this->load->library('email');
$this->email->set_mailtype("html");
$this->email->from($this->input->post('sender_email'), $name);
$this->email->to('destination_email');
$this->email->cc('alternative_email');
$this->email->subject('Enquiry');
$this->email->message($message);
$this->email->send();
The problem you're having is at a lower level than your script. SMTP connection failure means it's failing to even start talking to the server, so it has nothing to do with authentication or how you construct your message - all of them are gong to either call mail() or talk SMTP directly at some point and hit the same problem, as you're seeing.
The most likely explanation is that your DNS isn't working. Next I'd look at firewall issues, then make sure that fsockopen and stream_socket_client functions are not disabled in your php env.