How to use swiftmailer send email with html/php template - php

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?

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.

SwiftMailer returns empty array

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);

How to know mail is send and read by user when sending mail using SMTP,PHPmailer

is anybody know any solution for below problem
in my project, i am send email to client using smtp and php mailer and gmail script. so when i am send mail gmail send mail to particular client. for that i am passing gmail login and user name which is valid. all mail are sending properly. but sometime it may happen that some client not receive mail and at that time i am not able to get or trace any error. so i there any way, when i am sending mail to client , and when client get it and read it at that time i got any kind of confirmation.
Please if anybody have any idea , help me out.
<?php
/**
* Simple example script using PHPMailer with exceptions enabled
* #package phpmailer
* #version $Id$
*/
require ('../class.phpmailer.php');
try {
$mail = new PHPMailer(true); //New instance, with exceptions enabled
$body = "Please return read receipt to me.";
$body = preg_replace('/\\\\/','', $body); //Strip backslashes
$mail->IsSMTP(); // tell the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 25; // set the SMTP server port
$mail->Host = "SMTP SERVER IP/DOMAIN"; // SMTP server
$mail->Username = "EMAIL USER ACCOUNT"; // SMTP server username
$mail->Password = "EMAIL USER PASSWORD"; // SMTP server password
$mail->IsSendmail(); // tell the class to use Sendmail
$mail->AddReplyTo("someone#something.com","SOMEONE");
$mail->From = "someone#something.com";
$mail->FromName = "SOMEONE";
$to = "other#something.com";
$mail->AddAddress($to);
$mail->Subject = "First PHPMailer Message[Test Read Receipt]";
$mail->ConfirmReadingTo = "someone#something.com"; //this is the command to request for read receipt. The read receipt email will send to the email address.
$mail->AltBody = "Please return read receipt to me."; // optional, comment out and test
$mail->WordWrap = 80; // set word wrap
$mail->MsgHTML($body);
$mail->IsHTML(true); // send as HTML
$mail->Send();
echo 'Message has been sent.';
} catch (phpmailerException $e) {
echo $e->errorMessage();
}
?>
Some modification need to be done in above script.
Configure SMTP mail server.
Set the correct FROM & FROM Name (someone#something.com, SOMEONE)
Set the correct TO address
from
also
Delivery reports and read receipts in PHP mail
You can always add reply to mail address which will take care if there is any error so you will get email back, and for if it's read, include a picture (blank picture of 1 pixel will do) and add code in that picture like and you can see how many hits that image did recieve or recieved any at all.
You can check that things in two different ways like, by using the header "Disposition-Notification-To" to your mail function, it will not going to work in all case because most people choose not to send read receipts. If you could, from your server, influence whether or not this happened, a spammer could easily identify active and inactive email addresses quite easily.
You can set header like
$email_header .= "Disposition-Notification-To: $from";
$email_header .= "X-Confirm-Reading-To: $from";
now the another method to check is by placing an image and you can add a script to onload of that image, that script will send notification to your system that xxx user have read the mail so, in that way you can track delivery status of the mail.
In fact, I can't think of any way to confirm it's been delivered without also checking it gets read, either by including a image that is requested from your server and logged as having been accessed, or a link that the user must visit to see the full content of the message.
Neither are guaranteed (not all email clients will display images or use HTML) and not all 'delivered' messages will be read.
Though for more info https://en.wikipedia.org/wiki/Email_tracking

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.

Sending Email with content from PHP/Mysql

I want to send emails where the data is from a php/mySQL query.
I know that html will render in the email but i don't think the php codes will.
So is there any way where i can send emails with content queried from a mySQL DB ?
I already search here, there is one topic that covers it but the person who advised suggested a pdf export or to use a 3rd party tool which in my case are not applicable.
Thank you for the help guys :)
Use PHPMailer to generate the email on the server. It makes it very easy to generate multi-part messages (plaintext + html, with attachments and embedded/inline images). Basically:
// set up PHPMailer
$mail = new PHPMailer();
$mail->SetFrom('you#yourserver.com');
$mail->AddReplyTo('you#somewhereelse.com');
$mail->Subject('Your profile');
$mail->IsHTML(TRUE);
// do your database query
$con = connect_to_database();
$stmt = run_database_query($con, "SELECT ... FROM ...");
$data = fetch_from_database($stmt);
// set the email address
$mail->AddAddress($data['email'], $data['fullname']);
// html content for smart email clients
$html = <<<EOL
<h1>Welcome</h1>
<p>Your username is {$data['username']}.</p>
EOL;
// plain text alternate content
$text = <<<EOL
Welcome
Your username is {$data['username']}.
EOL;
// add the content to the mail
$mail->MsgHTML($html);
// add alternate content
$mail->AltBody($text);
// send the mail
if ($mail->Send()) {
// mail sent correctly
} else {
die("Uhoh, could not send to {$mail['email']}:" . $mail->ErrorInfo);
}
To avoid spam issues you can wrap the PHPMailer in a class and instantiate that at every e-mail address read from the database table.
For each email address you can create a new instance and do kind of a ->setMailAddr($mail,$fullName) and after send the e-mail destroy this instance.
The ideal is to place a new instance at every POST. In this case, you can put a FORM into the page.

Categories