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.
Related
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);
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?
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.
My code-
$from = "From:Company\n\r";
$mesg = include('mail.html');
function mail($u,'hey',$mesg);
basically, i want to send the mail, where the message needs to be the mail.html.
help...the mesage needs to be sent to $u.
Here's an example (composed from the examples in the manual) of how you can do this when using Swift Mailer:
<?php
require_once 'lib/swift_required.php';
//Create the message
$message = Swift_Message::newInstance()
//Give the message a subject
->setSubject('Your subject')
//Set the From address with an associative array
->setFrom(array('your#email.com' => 'Your Name'))
//Set the To addresses with an associative array
->setTo(array('recipient#domain.org' => 'Recipient\'s name'))
//Set CC
->setCc('support#example.com')
//Give it a body
->setBody(file_get_contents('mail.html'))
;
//Create the Mailer
$mailer = Swift_Mailer::newInstance(Swift_MailTransport::newInstance());
//Send the message
$result = $mailer->send($message);
This won't work because include() includes a file and evaluates (interprets the contents), you are trying to make it act like a function that returns a value which it is not.
What you need to do is read the contents of the file into a variable using file_get_contents() or a simillar function and then use it as the message body.
I advice you to use the PHPMailer Class really it will give you a lot of facilites and features and also the download contains examples you will need to include the class file then use it its free. for mor information go to this link
Don't use function in front of mail, just do
mail($emailto, $subject, file_get_contents('mail.html'), $headers);
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.