SwiftMailer returns empty array - php

I am teaching myself php and how to use Swiftmailer. I have followed the doc and added the code below to my php file, but it returns array( ). I can't figure out what could be wrong and how to check. I don't seem to have access to the remote server's ini file from the hosting provider. Would appreciate any help in determining what may be wrong and how to correct. thanks!
require_once 'Swift/lib/swift_required.php';
//Use simple mail transport
$transport = Swift_MailTransport::newInstance();
// Create the message
$message = Swift_Message::newInstance();
$message->setTo("xxx#yyy.com");
$message->setSubject("This email is sent using Swift Mailer");
$message->setBody("You're our best client ever.");
//$message->setFrom($email);
$message->setFrom("xxx#zzz.net");
$message->setReturnPath('xxx#zzz.net');
// Send the email
$mailer = Swift_Mailer::newInstance($transport);
$mailer->send($message, $failedRecipients);
// Show failed recipients
print_r($failedRecipients);

Related

Swiftmailer send e-mail fails

I have this code which I am using to send an email via Swiftmailer.
I tried many things, I tried with Phpmailer aswell and I am not able to send a simple e-mail. I also tried with mail() function in php and nothing, I don't have the sendmail configured, so I thought to use one of these libraries.
The code I am using is:
require_once ('lib/swift_required.php');
$message = Swift_Message::newInstance()
//Give the message a subject
->setSubject('Webinar Registration')
//Set the From address with an associative array
->setFrom(array('ami#am.com' => 'FROM NAME'))
//Set the To addresses with an associative array
->setTo(array('ami#am.com'))
//Give it a body
->setBody('My Message')
//And optionally an alternative body
//->addPart('<q>Here is the message itself</q>', 'text/html')
;
//Create the Transport
$transport = Swift_SmtpTransport::newInstance('127.0.0.1', 25);
//Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
//Send the message
$result = $mailer->send($message);
When I debug it, it stops right after the require statement. I am trying to send it via localhost and i don't want to use smtp.
Please any help is appreciated.

swift mail not working in the live but working in local why?

// Create the mail transport configuration
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')
->setUsername(PASUPUKUMKUMA_EMAIL)
->setPassword(PASUPUKUMKUMA_PASS);
// Create the message
$message = Swift_Message::newInstance()
->setTo(array($email => $name))
->setSubject("Registration Success !")
->setBody('You have Registered Successfully ! Thank You For Registering With Us.Click the link to confirm your account <a href='.site_url().'/main/confirm/'.$name.'/'.$ran.'>confirm here</a>', 'text/html')
->setFrom(PASUPUKUMKUMA_EMAIL,EMAIL_FROM_NAME);
// Send the email
$mailer = Swift_Mailer::newInstance($transport);
$mailer->send($message);
This is my code for sending email through swift mailer everything was perfect when I send the mail in the local I can be able to send the mail successfully.
When comes to live website with the same code not able to send the mail and shows the error:
Connection could not be established with host smtp.gmail.com..
https://www.google.com/settings/security/lesssecureapps
Turn it on for the account that you are using. Hope it will help.

How to use swiftmailer send email with html/php template

I want to use swiftmailer to send a report on a daily basis. I am able to send a test email for testing but I would like to use an email template for the report. This is what I tried.
// Create the mail transport configuration
$transport = Swift_MailTransport::newInstance();
// Create the SMTP configuration
$transport = Swift_SmtpTransport::newInstance("smtp.gmail.com", 465, 'ssl');
$transport->setUsername("user_id");
$transport->setPassword("password");
// Create the message
$message = Swift_Message::newInstance();
$message->setTo(array(
"firstname.lastname#gmail.com" => "First"));
$message->setSubject("This email is sent using Swift Mailer");
$message->setBody("$email_content", "text/html");
$message->setFrom("test#gmail.com", "Swiftmailer Test");
// Send the email
$mailer = Swift_Mailer::newInstance($transport);
$mailer->send($message);
What I did is create a variable called $email_content which returns all of the content of the url (which actually calls a function to grab data from DB to populate some tables on the page).
//display data from db on the page
$email_content = file_get_contents("http://localhost/test/get_content.php");
However, when I open the email, it is able to display the table with the data but it doesn't display it properly like in the original url. Is it possible to get the email to look close to the url or at least decent with proper formatting?

PHP mail() function replacement?

When I change providers I always have to make sendmail work and it's a real drag.
Are there any free providers I can use within my PHP code to send the same variables like keycodes for registration verification and stuff?
I tried looking on Google but I only found stuff that had form generators, which is not what I need.
I use PHPMailer with great success.
Right now Swift Mailer is one of the best mail solutions around for PHP. It's highly modular and can easily be configured to suit your needs. You could define multiple transports (in a config file for example) and use the one you need when you change providers.
Here is an example from the docs:
require_once 'lib/swift_required.php';
// Create the Transport
$transport = Swift_SmtpTransport::newInstance('smtp.example.org', 25)
->setUsername('your username')
->setPassword('your password')
;
/*
You could alternatively use a different transport such as Sendmail or Mail:
// Sendmail
$transport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs');
// Mail
$transport = Swift_MailTransport::newInstance();
*/
// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
// Create a message
$message = Swift_Message::newInstance('Wonderful Subject')
->setFrom(array('john#doe.com' => 'John Doe'))
->setTo(array('receiver#domain.org', 'other#domain.org' => 'A name'))
->setBody('Here is the message itself')
;
// Send the message
$result = $mailer->send($message);
What you need is an outgoing authenticated SMTP server so your not using the hosts one and don't have to change the details each time.
Take a look at AuthSMTP (there are lots of other websites that provide this) and then use something like PHPMailer or Swift Mailer to send the email with authentication.
The problem is that a lot of ISPs block connections on port 25 (default smtp) to servers other than their own. Has to do with spam blocking etc.

How can I keep track of mail sent using PHP Swift Mailer?

I am using PHP Swift Mailer to send a bulk mail to a set of users. But I am not able to keep track of sent mail.
My code:
<?php
require_once("includes/database.class.php");
require_once("lib/swift_required.php");
$con=DBClass::getConnection();
$db=DBClass::getDatabase($con);
$login_id="myloginname";
$password="mypassword";
$to_mail; //list of people
//Create the Transport
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, "ssl")
->setUsername($login_id)
->setPassword($password);
//Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
//Rate limit to 25 emails per-minute
$mailer->registerPlugin(new Swift_Plugins_ThrottlerPlugin(
25, Swift_Plugins_ThrottlerPlugin::MESSAGES_PER_MINUTE
));
//Create a message
$message = Swift_Message::newInstance($subject)
->setFrom($login_id)
->setTo($to_mail)
->setBody($body,
'text/html'
);
$numSent=$mailer->batchSend($message);
?>
I am using batchSend() method to send mail, which gives me the count of mail that has been sent, but it is not giving me the list of email that has been sent. How can it be possible, is there any plugin or function available?
Using Logger plugin will give me the log, but I am unable to read from that.
You can get an array of email addresses that were rejected by passing a variable by reference to batchSend() for the system to fill in:
http://swiftmailer.org/docs/failures-byreference
Then you can array_diff() those from your $to_mail array to get the succesful ones.

Categories