PHPMailer and Style Sheet formatting - php

I'm using PHPMailer to automatically send an order acknowledgement as an HTML formatted email. Everything is working as expected except that the formatting of the acknowledgement isn't correct. I included my style sheet with an 'AddAttachment' line which seems to have fixed the header of the acknowledgement form, but the rest of the form still isn't right. Has anyone run into this situation before and know what I need to do to fix it? My edited program code follows in case it'll help!
<?php
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->SMTPDebug = 0;
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth = true;
$mail->Port = 25; // set the SMTP port
$mail->Host = "<<smtp server>>"; // SMTP server
$mail->Username = "<<username>>";
$mail->Password = "<<password>>";
$mail->From = "<<email address>>";
$mail->FromName = "<<name>>";
$mail->AddAddress("<<email address>>");
$mail->Subject = "Acknowledgement Form";
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
$mail->IsHTML(true);
$mail->Body = file_get_contents('<<acknowledgement form page>>');
$mail->AddAttachment('printer.css'); // attach style sheet
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
?>

You should'nt work with external CSS files in emails... Not many of the email-clients can handle that and not all of your customers use email-clients like Outlook or Thunderbird. Instead, you should disgn you html-code "90s style" :-P
e.g. <p style="width: 200px; text-align: center;">

Related

How Sending multiple emails with PHPmailer

I have thousands of emails and I want to send an e-mail to all of them and I can not solve it. Is anyone able to help and clarify with examples in ways of solving
As shown in the following code what is the problem and what is the solution
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'gator4164.hostgator.com '; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = "..."; // SMTP username
$mail->Password = '...'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
$mail->CharSet = 'UTF-8';
$get = new get();
$getEmails = $get->getEmails();
$CountEmails = $get->CountEmails();
$mail->setFrom('email#gmail.com', 'Email Name');
foreach($getEmails as $getEmails){
for ($x = $emails['id']; $x <= $CountEmails; $x++) {
$mail->addAddress($emails['email'],$emails['name']);
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $title;
$mail->Body = nl2br($content);
$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'<div class="done">Done</div>';
}
}
For such kind of scenario, you have to create a Process Queue because every email sending needs some time to process and if the email count is thousand then in that case your system will crash as it is having a processing time limit. For creating a process queue you have to maintain a database table in which save all the records and maintain a status with different values like:
0: Initial, 1:Processing, 2: Email Sent
And create a separate functionality that pick a record on a regular interval and send the email and change the status. Put this functionality on CRON.
Don't add all of the recipients with "add" in one loop then you have one Email with all recipients. Send your E-Mail with every duration.
foreach($getEmails as $getEmails){
$emails['email'] = $getEmails['email'];
$emails['name'] = $getEmails['name'];
$mail->addAddress($emails['email'],$emails['name']);
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $title;
$mail->Body = nl2br($content);
$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'Email send to ' + $emails['email'];
}
$mail->clearAddresses();
}
something like this. So send the Email for every duration. To prevent that you send people duplicate emails you should set the status in your database if the email is send otherwise you send it again if you have an error in your script and you have to restart the script.

Sending mails using Mandrill PHPmailer is not working

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

PHPMAILER, IsHTML already set to true

The isHTML is already set to True. But it doesnt work in the email i receive. I just get the sample html from a tutorial.
<?php
require("phpmailertest/class.phpmailer.php");
$x=$_SESSION['items'];
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
// $mail->SMTPDebug = 2;
$mail->From = "benedictpayot#gmail.com";
$mail->FromName = "BravoTech Solutions";
$mail->Host = "smtp.gmail.com"; // specif smtp server
$mail->SMTPSecure= "ssl"; // Used instead of TLS when only POP mail is selected
$mail->Port = 465; // Used instead of 587 when only POP mail is selected
$mail->SMTPAuth = true;
$mail->Username = "benedictpayot#gmail.com"; // SMTP username
$mail->Password = "Ichthys030313!"; // SMTP password
$mail->AddAddress($_SESSION['email_address']);
$mail->IsHTML(true);
$mail->Subject = "Mail Test";
$mail->Body = '<html><body>';
$mail->Body = '<table style="border-color: #eee;"';
$mail->Body = '<tr>
<td>JALOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
<td>OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
<tr>
<td>BENEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEDICT
<td>PAYOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOT
</table>';
$mail->WordWrap = 50;
if(!$mail->Send()) {
echo 'Message was not sent.';
session_destroy();
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
?>
You can set Body (and AltBody) manually to any value you like. msgHTML() is a convenience function to set moth of them and optionally apply an html to text conversion to generate your plain text version. It also sets isHTML, rewrites image URLs and various other things - but you don't have to use it.
In your code you're saying:
$mail->Body = '<html><body>';
$mail->Body = '<table style="border-color: #eee;"';
$mail->Body = '<tr>...
Which should be:
$mail->Body = '<html><body>';
$mail->Body .= '<table style="border-color: #eee;"';
$mail->Body .= '<tr>...
otherwise you're just overwriting the contents of Body each time.
You should base your code on the gmail example bundled with PHPMailer - it looks like you are using an old one from somewhere else.
I'm not sure (correct me if I'm wrong) but I was always setting body like this:
$mail->MsgHTML($body);

PHPmailer code works great to send to non-google apps email addresses but fails for google apps address

I am having a unique issue (I did a thorough search on SO before I attempted to ask this question.
When I use the PHPMailer to send to a gmail (or hotmail, etc) address, it works great. As soon as I change it to send to a Google Apps email address, I don't get any error message instead it tells me it was successfull but no emails come through.
Has anybody seen this issue before? Is my code missing something very particular that makes it a valid email to pass through Google Apps Servers (not sure if I am heading in the right direction here). Thank you!
Start of my Code:
<?php
require("/PHPMailer_5.2.0/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Port = 465;
$mail->Host = 'smtp.gmail.com'; // "ssl://smtp.gmail.com" didn't worked
$mail->IsHTML(true); // if you are going to send HTML formatted emails
$mail->Mailer = 'mail';
$mail->SMTPSecure = 'ssl';
$mail->SMTPAuth = true;
$mail->Username = "******#dynamicsafetyfirst.com";
$mail->Password = "*******";
$mail->From = $_POST['email'];
$mail->AddAddress("someuser#gmail.com");
$mail->Subject = "First PHPMailer Message";
$mail->WordWrap = 50;
$mail->FromName = $_POST['name'];
$mail->Subject = $_POST['enquiry'];
$mail->Body = $_POST['comments']. "--By--".' name: '. $_POST['name']."--". 'email: ' .$_POST['email'];
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
?>
END OF CODE.
You're putting in quite a lot of effort to do things wrong. First up you're using a pretty old version of PHPMailer - go get the latest from github. Next, your code has many issues, so start again using the gmail example provided. Use tls on port 587. Do not set Mailer - you've already called isSMTP(), and overriding Mailer later is asking for trouble.
To see what is going on set $mail->SMTPDebug = 3;, and it will show you the whole SMTP conversation. At that point you may get some clue as to what is happening to your message.

PHPMailer and 100K file limit for attachments?

I'm trying to send a file as an email attachment, but for some reason if the file is > 100k then the email doesn't go through, even though I get the email sent message.
It may also be a limit on the attachments in IIS smtp setting, but when I unchecked the Limit session size and limit message size options, it didn't change anything. I may have to restart the server tonight...
I don't know if it's a php.ini setting, or what.
<?
$path_of_attached_file = "Images/marsbow_pacholka_big.jpg";
require 'include/PHPMailer_5.2.1/class.phpmailer.php';
try {
$mail = new PHPMailer(true); //New instance, with exceptions enabled
$body = $message; //"<p><b>Test</b> another test 3.</p>";
$mail->AddReplyTo("admin#example.com","Admin");
$mail->From = "admin#example.com";
$mail->FromName = "Admin";
$mail->AddAddress($to);
$mail->Subject = "First PHPMailer Message";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->WordWrap = 80; // set word wrap
$mail->MsgHTML($body);
$mail->IsHTML(true); // send as HTML
if($attach){
$mail->AddAttachment($path_of_attached_file);
}
$mail->Send();
echo 'Message has been sent.';
} catch (phpmailerException $e) {
echo $e->errorMessage();
}
?>
I might be wrong because I don't use IIS but the code you provided would actually use a native MTA not SMTP. As far as I know you have to use the IsSMTP() method to let PHPMailer know that you intend to use SMTP.
Something like this:
<?
$path_of_attached_file = "Images/marsbow_pacholka_big.jpg";
require 'include/PHPMailer_5.2.1/class.phpmailer.php';
try {
$mail = new PHPMailer(true); //New instance, with exceptions enabled
$body = $message; //"<p><b>Test</b> another test 3.</p>";
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "mail.yourdomain.com"; // sets the SMTP server
$mail->Port = 25; // set the SMTP port
$mail->Username = "yourname#yourdomain"; // SMTP account username
$mail->Password = "yourpassword"; // SMTP account password
$mail->AddReplyTo("admin#example.com","Admin");
$mail->From = "admin#example.com";
$mail->FromName = "Admin";
$mail->AddAddress($to);
$mail->Subject = "First PHPMailer Message";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->WordWrap = 80; // set word wrap
$mail->MsgHTML($body);
$mail->IsHTML(true); // send as HTML
if($attach){
$mail->AddAttachment($path_of_attached_file);
}
$mail->Send();
echo 'Message has been sent.';
} catch (phpmailerException $e) {
echo $e->errorMessage();
}
?>
Your code is not actually checking if the message was sent or not.
You need to change your code to check the return of the send method
if ($mail->Send())
echo 'Message has been sent.';
else
echo 'Sorry there was an error '.$mail->ErrorInfo;
This should give you the error message saying what is up if it does go wrong.

Categories