PHPMailer: Mass Emails - Send all variable messages at once, instead of individually - php

i'm currently trying to figure out the best way to do this.
The current system i've made sends email one by one and fills in the information for each entry as in the array, such as email, first name and last name.
The problem here is if i send alot of messages it takes forever to run through as it's calling a function everytime, instead i want it to send them all at once through one single function.
I know you can add multiple to's but then the body of the email won't send the correct information relative to each email. If anyone can help me with this i'd really appreciate it, as i've searched all over for a solution.
<?php
require '../phpmailer/PHPMailerAutoload.php';?>
<?php
/* Block List */
$blocklist = array('emailblocked#gmail.com', 'emailblocked2#gmail.com');
$emaillist = array(
array(
'Email'=>'example#gmail.com',
'First Name'=>'John',
'Last Name'=>'Doe'
),
array(
'Email'=>'example2#gmail.com',
'First Name'=>'Joe',
'Last Name'=>'Doe'
),
array(
'Email'=>'example3#gmail.com',
'First Name'=>'Jane',
'Last Name'=>'Doe'
),
);
foreach($emaillist as $emailkey){
if (in_array($emailkey['Email'], $blocklist)) {
echo 'Message has been been blocked for '.$emailkey['Email'].'<br>';
}else{
$mail = new PHPMailer;
// $mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.mandrillapp.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'username#example.com'; // SMTP username
$mail->Password = 'passwordgoeshere'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->From = 'noreply#example.com';
$mail->FromName = 'Example';
$mail->addAddress($emailkey['Email'], $emailkey['First Name'].' '.$emailkey['Last Name']); // Add a recipient
$mail->addReplyTo('info#example.com', 'Information');
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $emailkey['First Name'].' '.$emailkey['Last Name'];
$emailtemp = file_get_contents('templates/temp-1.html');
$emailtempfilteremail = str_replace("[[email]]", $emailkey['Email'], $emailtemp);
$emailtempfilterfirstname = str_replace("[[firstname]]", $emailkey['First Name'], $emailtempfilteremail);
$emailtempfilterlastname = str_replace("[[lastname]]", $emailkey['Last Name'], $emailtempfilterfirstname);
$mail->Body = $emailtempfilterlastname;
$mail->AltBody = 'This is a spicy email!';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent to '.$emailkey['Email'].'<br>';
}
$mail->ClearAllRecipients();
}
}
?>
Thank you

There's an example of how to send to a list from a database efficiently in the examples bundled with PHPMailer. There's nothing inherently likely to get you blacklisted by using PHPMailer for sending large volumes, but you do need to tread carefully. Mandrill isn't magic - it's as vulnerable as anything else to being blocked if you send spam through it.
If you want to send 50 simultaneously from PHP, fire up multiple processes with the pcntl extension, but it won't actually help you very much as you'll be increasing overhead enormously. You can set SMTPKeepAlive = true in PHPMailer which will reduce overhead a lot (it avoids making a new connection for every message), but it still won't send simultaneous messages - nothing will. There isn't an option in SMTP to send multiple messages with different bodies simultaneously on the same connection.
Sending to a big list during a page load in a browser is very unreliable; use a cron script or background process to do your actual sending and just set it up through your web interface. One tip if you are waiting for a page load - call ignore_user_abort() early on so that it won't stop sending if your browser closes the connection - and beware the page refresh! If you want to send much faster, install a local mail server like postfix and use that to relay - it will be far faster and more reliable than sending directly.

Yes, it is possible with a modification of your code, the problem is not with the PHPMailer itself, but with your approach. You should avoid using an new instance of the class inside a loop (this leads to memory exhaustion with large lists), instead, only invoke $mail->addAddress(...) or $mail->Subject(...) inside the foreach loop.
If you read the source code of the PHPMailer, you will notice how exactly the functions addAddress(), Subject() or Body() works.
Your code should look something like this:
<?php
/*Move your "generic" initialization outside the loop*/
$mail = new PHPMailer;
// $mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.mandrillapp.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'username#example.com'; // SMTP username
$mail->Password = 'passwordgoeshere'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->From = 'noreply#example.com';
$mail->FromName = 'Bet Monkey';
$mail->isHTML(true); // Set email format to HTML
$mail->addReplyTo('info#example.com', 'Information');
$emailtemp = file_get_contents('templates/temp-1.html');
$mail->AltBody = 'This is a spicy email!';
/*Start the loop by adding email addresses*/
foreach($emaillist as $emailkey){
if (in_array($emailkey['Email'], $blocklist)) {
echo 'Message has been been blocked for '.$emailkey['Email'].'<br>';
}else{
$mail->addAddress($emailkey['Email'], $emailkey['First Name'].' '.$emailkey['Last Name']); // Add a recipient
$mail->Subject = $emailkey['First Name'].' '.$emailkey['Last Name'];
$emailtempfilteremail = str_replace("[[email]]", $emailkey['Email'], $emailtemp);
$emailtempfilterfirstname = str_replace("[[firstname]]", $emailkey['First Name'], $emailtempfilteremail);
$emailtempfilterlastname = str_replace("[[lastname]]", $emailkey['Last Name'], $emailtempfilterfirstname);
$mail->Body = $emailtempfilterlastname;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent to '.$emailkey['Email'].'<br>';
}
$mail->ClearAllRecipients();
}
}
Using the above approach I personally have send hundreds of thousands emails, but, as they say in the comments - you'll risking to be blacklisted (you can check here or here for details).

Related

phpmailer loop duplicated email addresses (and possible message) in loop

I have the following code sending email shipping confirmations to my customers:
while ($i < count($tracked) ) {
try {
//Server settings
//$mail->SMTPDebug = SMTP::DEBUG_SERVER; // Enable verbose debug output
$mail->isSMTP(); // Send using SMTP
$mail->Host = 'smtp.office365.com'; // Set the SMTP server to send through
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'support#mycompany.com'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('support#mycompany.com', 'mycompany');
$mail->addAddress($tracked[$i]['customers_email_address'], $tracked[$i]['customers_name']); // Add a recipient
$mail->addReplyTo('support#mycompany.com', 'mycompany');
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = EMAIL_TEXT_SUBJECT;
$mail->Body = $html;
$mail->AltBody = 'Your order with mycompany.com has shipped';
$mail->send();
echo 'order confirmation sent to order#:'.$tracked[$i]['orders_id'].'<br/>';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
$i++;
}
It appears to be sending to multiple emails. What I believe is happening is that in the while loop each new customers address is added to the list without clearing it. What doesnt seem to be happening though is the message does not seem to be duplicated? Wouldn't that duplicate at as well with each pass in the loop?
In any case I think the best thing to do before the try is:
$mail = new PHPMailer(true);
so that with each pass of the while loop the $mail object would be instantiated again. Is that proper? I know that the clearAllRecipients() function exists but i also want to be sure the body is cleaned as well.
it looks like your looping through an array of $tracked. Why not use a foreach loop? Then you don't need to use a counter.
foreach ($tracked as $track ) {
try {
//Server settings
//$mail->SMTPDebug = SMTP::DEBUG_SERVER; // Enable verbose debug output
$mail->isSMTP(); // Send using SMTP
$mail->Host = 'smtp.office365.com'; // Set the SMTP server to send through
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'support#mycompany.com'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('support#mycompany.com', 'mycompany');
$mail->addAddress($track['customers_email_address'], $track['customers_name']); // Add a recipient
$mail->addReplyTo('support#mycompany.com', 'mycompany');
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = EMAIL_TEXT_SUBJECT;
$mail->Body = $html;
$mail->AltBody = 'Your order with mycompany.com has shipped';
$mail->send();
echo 'order confirmation sent to order#:'.$track['orders_id'].'<br/>';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
}
It's because addAddress() does exactly what its name suggests; it adds an address that a message will be sent to. It doesn't set the addresses a message will be sent to.
Unsurprisingly there is a method that allows you to clear the list of addresses, called clearAddresses(), so just call that at the end of your sending loop after send() and your issue will be solved.
I'd also recommend that you base your code on the mailing list example provided with PHPMailer as it will be much faster than the code you have here. Also read the docs on sending to listshttps://github.com/PHPMailer/PHPMailer/wiki/Sending-to-lists) for furtehr advice.

PHPMailer not working: Is there a new way of doing this?

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';
}

ForEach loop values keeping previous values

I have a foreach loop from which i am sending emails for my customers.
I have 4 customers in my db 1,2,3,4
So according to foreach loop the loop will run for 4 times to send an email to every customer
But in 1st run it sends email to customer 1.
In 2nd run It is sending email to customer 1 and 2.
In thirdd run it is sending email to customer 1,2,3 and so on.
How can i make to send one email to one customer.
here is my code.
$list=$tmp_con->query("SELECT name,email FROM users WHERE user_type='$tp' AND subscribed='yes' AND verified='1'") or die($tmp_con->error);
$lis=array();
while($row=$list->fetch_array()){
$lis[] = $row;
}
foreach($lis as $lst){
$content = str_replace("{name}", $lst['name'], $mail_content);
$content=$content."".$append_unsub;
/**************************************************phpmailer class***********************/
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = $mail_smtp_host; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = $mail_smtp_username; // SMTP username
$mail->Password = $mail_smtp_password; // SMTP password
$mail->SMTPSecure = $mail_smtp_enc; // Enable TLS encryption, `ssl` also accepted
$mail->Port = $mail_smtp_port; // TCP port to connect to
$mail->setFrom($mail_smtp_from_email, $mail_smtp_from_name);
$mail->addAddress($lst['email']); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $mail_subject;
$mail->Body = $content;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
$lst['email']='';
/*************************************************php mailer ends here*************************************/
} ///foreach ends here
Create a new $mail object in the loop or even better reset the addresses in the loop. You've (probably) declared the $mail object outside the for loop.
So every time you call addAddress you're adding the email address of the user. The first time you're setting the email address of the first user, then the second time you're adding the email address to the list, so two email addresses. Third round the same, fourth round etc..
Edit: Creating $mail inside the loop fixes the problem, but isn't efficient. See Synchro's comment for a better way to create and send the email.
No need of creating an instance of PHPMailer in each and every loop. Use $mail->clearAddresses(); at the end of foreach loop to clear all the addresses.
Updated Code
$list = $tmp_con->query("SELECT name,email FROM users WHERE user_type='$tp' AND subscribed='yes' AND verified='1'") or die($tmp_con->error);
$lis = array();
while ($row = $list->fetch_array()) {
$lis[] = $row;
}
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = $mail_smtp_host; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = $mail_smtp_username; // SMTP username
$mail->Password = $mail_smtp_password; // SMTP password
$mail->SMTPSecure = $mail_smtp_enc; // Enable TLS encryption, `ssl` also accepted
$mail->Port = $mail_smtp_port; // TCP port to connect to
$mail->setFrom($mail_smtp_from_email, $mail_smtp_from_name);
foreach ($lis as $lst) {
$content = str_replace("{name}", $lst['name'], $mail_content);
$content = $content . "" . $append_unsub;
//$mail->SMTPDebug = 3;
$mail->addAddress($lst['email']); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $mail_subject;
$mail->Body = $content;
if (!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
$mail->clearAddresses(); // Clear all addresses for next loop
}
References:
PHPMailer

php mailer sends 2 copies

I am sending a report by calling this PHP page daily on my browser. It (too often) sends emails twice (even if I make sure to open a new tab each time).
What's wrong with the code + How can I prevent it?
Here is the code:
<?php
require ("/home/phpmailer/PHPMailer-master/PHPMailerAutoload.php");
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'localhost'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'USERNAME#DOMAIN.com'; // SMTP username
$mail->Password = 'PASSWORD'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->From = 'FROM-NAME#DOMAIN.com';
$mail->FromName = 'FROM NAME';
$mail->ClearAddresses();
$mail->addAddress('email1#ABC.com', 'CLARA'); // Add a recipient
$mail->addCC('email##ABC.com', 'TOM'); // Add a CC recipient
$mail->addReplyTo('email2#ABC.com', 'Info');
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'EMAIL SUBJECT TITLE';
$mail->Body = file_get_contents('http://ADDRESS-OF-THE-FILE.PHP');
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
$mail->ClearAddresses();
}
?>
Do as SmartyCoder suggested in the comments.
If you are certain you're the only one hitting it, you might try something quick and dirty with cookies to track, like:
// See if a cookie is set, and if so, compare it to today
// If cookie value == today, die() - stop executing
if ( isset( $_COOKIE['email_reports_lastsent'] ) &&
$_COOKIE['email_reports_lastsent'] == date('Y-m-d') ) die();
// Set the cookie as today's date
setcookie( 'email_reports_lastsent', date('Y-m-d') );
This does NOT solve any issue if other devices/users are hitting your script. It also requires you to be using the same browser to send, and you cannot use Incognito or other private browsing tab.
I am going to guess that you are using Google Chrome and "Prefetch resources to load pages more quickly" is enabled. Essentially Chrome is fetching the URL before you finish typing so when you finish typing and hit enter then you are requesting it again.
Either turn off prefetching or save the URL to a bookmark and click the bookmark when you need to run the task.

Codeigniter & 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.

Categories