Sending templates with PHPMailer and Mandrill - php

I've made a script using PHPMailer that sends email using Mandrill. I am trying to send a html template that is made with bootstrap, but when the mail comes, the CSS doesn't work.
I tried to get the html content with file_get_contents() function but it doesn't get the bootstrap stylesheet. Any ideas?
Here is the script
<?php
include "../phpmailer/PHPMailerAutoload.php";
include "../phpmailer/class.phpmailer.php";
error_reporting(E_ALL^E_NOTICE);
if (!isset($_POST['Submit']) |$_POST['Submit'] != 'Trimite') {
$adress = '';
} else {
$adress = $_POST['adress'];
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.mandrillapp.com';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = 'username';
$mail->Password = 'key';
$mail->SMTPSecure = 'tsl';
// $mail->SMTPDebug = 1;
$mail->From = 'email#example.com';
$mail->FromName = 'Name';
$mail->AddAddress($adress);
$mail->IsHTML(true);
$mail->Subject = 'Subject';
$mail->Body = file_get_contents( 'process_completed.html' );
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
}
?>

$mail->SMTPSecure = 'tsl';
should probably be:
$mail->SMTPSecure = 'tls';

If you are using any external css files in the email template will not work.
Use only inline css in email template.

Many email clients don't render CSS that's in the head of email HTML. You'll either need to inline the CSS before you submit the email to Mandrill, or use Mandrill's CSS inlining option.

I made a template with inline css an it works. Thanks.

Related

SMTP: CLIENT: 535 5.7.3 Authentication Unsuccessful

I'm trying to send an email every time someone inputs a form on my website but the SMTP isn't working. It keeps on showing me this error. The credentials of the email account are correct. Also, I have already tried SMTPAuth = False; but still the same result.
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
//Load Composer's autoloader
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = 2;
$mail->isSMTP();
$mail->Host = 'smtp.office365.com';
$mail->Port = 587;
$mail->SMTPSecure = 'TLS';
$mail->SMTPAuth = true;
$mail->Username = 'sender#test.com'; // SMTP username
$mail->Password = 'password'; // SMTP password
//Recipients
$mail->setFrom('test#test.com', ' Services');
$mail->addAddress('recipient#test.com', $name); // Add a recipient
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Contact Us Message';
$mail->Body = 'Sent a message, This is the message :';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
header('Location: tempage.php');
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
?>
What is going wrong? Thanks for your help.
I'm also struggling 2 days with same issue.
After i find this solution. It's working fine.
Try this.
$mail->Host = 'smtp.office365.com';
change to
$mail->Host = 'smtp.live.com';

PhpMailer not working with html

I am using PhpMailer class to send emails that contains html,I noticed that if the body on email contain img tags the email not receiving and no errors showing.
Any suggestions?
my code so far:
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->Username = "myusername";
$mail->Password = "mypassword";
$mail->setFrom($from);
$mail->addAddress($to);
$mail->Subject = $subject;
$mail->Body =$body;
$mail->IsHTML(true);
if ($mail->send()) {
return 1;
} else {
return 0;
}
if you're trying to send it from localhost and read in gmail, it won't work because gmail often creates proxy image links. you need to send image links that direct to internet-accessible location. That's probably it.
If you're sending email with links to internet-accessible locations, please attach them to the question and I'll update the answer, too.
EIDT: after chat with the asker, it was determined the email HTML was missing normal HTML markup: <html><body>ACTUALCONTENT</body></html>, after adding those,image shown up correctly.
You can use the AddEmbeddedImage method to attach image to your body. So your code should look like this.
$mail->AddEmbeddedImage($_REQUEST['image_name'], 'ImageName');
And then in your $body you can use this image
$body.= "<img src='cid:ImageName' />";

PHPMailer cannot be resolved

So I am trying to set up a contact us page on a website i am creating but PHPMailer just refuses to work for me.
From what I can see, it just isn't recognizing the PHPMailer class at all and I am not really sure why. Here is my PHP code for the form to send the email, as far as i can tell i have correctly installed PHPMailer/Composer, and have done everything else correctly, but i still get errors when trying to use PHPMailer at all. "PHPMailer cannot resolved to a type" and "The import PHPMailer\PHPMailer\PHPMailer cannot be resolved"
<?php
use PHPMailer\PHPMailer\PHPMailer;
require_once './vendor/autoload.php';
// Bunch of code validating all the variables from the form
function send_mail($toEmail, $endSubject, $endMessage, $endName) {
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host = "smtp.domain.com";
$mail->SMTPAuth = true;
$mail->Username = "LOGIN EMAIL"; // Email
$mail->Password = "PASSWORD"; // Email password
$mail->SMTPSecure = "tls";
$mail->Port = "587";
$mail->From = "example#domain.com"; //RANDOM EMAIL TO SEND THESE MESSAGES TO OUR EMAIL
$mail->FromName = $endName; //CUSTOMERS NAME
$mail->addAddress($toEmail); //WHOEVER WE ARE SENDING THESE EMAILS TO
$mail->isHTML(true);
$uri = 'http://'. $_SERVER['HTTP_HOST'];
$mail->Subject = $endSubject;
$mail->Body = $endMessage;
if($mail->send()) {
return true;
} else {
return false;
}
}
?>

Sending mails using PHPmailer is not working on a live server

If I use Gmail smtp credential working properly and after going live server it is not working. When I use godaddy web mail it not work on localhost or live server.
Here is my PHP file.
<?php
$data = json_decode(file_get_contents("php://input"));
$Cust_Email = $data->Cust_Email;
$result = array();
require 'vendor/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'relay-hosting.secureserver.net';
$mail->SMTPAuth = true;
$mail->Username = 'orders#washbucket.info';
$mail->Password = '******';
$mail->SMTPSecure = 'ssl';
$mail->Port = 25;
$mail->setFrom('orders#washbucket.info', 'Admin');
$mail->addAddress($Cust_Email);
$mail->isHTML(true);
$mail->Subject = 'Welcome To User.';
$mail->Body = 'This is greating from Admin';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
$result = '_FAIL';
} else {
$result = '_SUCCESS';
}
echo json_encode($result);
?>
Using implicit SSL to port 25 will simply not work. Set SMTPSecure = false. PHPMailer will enable explicit TLS automatically if the server supports it. If I recall correctly, GoDaddy doesn't use authentication for its relay servers, so you may need to set SMTPAuth = false as well.

Send mail using yahoo smtp in php with html formatted text

I am trying to send mail from PHP using my yahoo credentials.
$Mbody=$Mbody."<body>";
$Mbody=$Mbody."<table border=0>";
$Mbody=$Mbody."<tr>";
$Mbody=$Mbody."<td>Hi,</td>";
$Mbody=$Mbody."</tr>";
$Mbody=$Mbody."<tr>";
$Mbody=$Mbody."<td>New Mail for testing.</td>";
$Mbody=$Mbody."</tr>";
$Mbody=$Mbody."<tr>";
$Mbody=$Mbody."<td><a href=http://www.google.com >Verify Account</a></td>";
$Mbody=$Mbody."</tr>";
$Mbody=$Mbody."</table>";
$Mbody=$Mbody."</body>";
$Mbody=$Mbody."</html>";
$mail->IsSMTP();
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl";
$mail->Host = "plus.smtp.mail.yahoo.com";
$mail->Port = 465; // set the SMTP port
$mail->Username = "********";
$mail->Password = "********";
$mail->From = "********";
$mail->FromName = "my Name";
$mail->AddAddress('********');
$mail->Subject = "Subject";
$mail->Body = $Mbody;
When I execute the code, it does send mail to desired recipient, but the body shows all HTML tag in the received mail. What is missing in above code.
Try This
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Subject';
$mail->MsgHTML($Mbody);
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
Please use content-type as HTML
$mail->IsHTML(true); // send as HTML

Categories