Prevent Image blocking while sending emails using php - php

I tried this code. But image is not shown in the email. I need to display image without getting blocked.
require("class.phpmailer.php")
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "smtp.sendgrid.net";
$mail->SMTPAuth = true;
$mail->Username = 'username';
$mail->Password = 'password';
$mail->From="ex#gmail.com";
$mail->FromName="Email Image";
$mail->Sender="eg#gmail.com";
$mail->AddReplyTo("xe#gmail.com", "Reply");
$mail->AddAddress("exe#gmail.com");
$mail->Subject = "Test";
$mail->IsHTML(true);
$mail->AddEmbeddedImage('https://ci4.googleusercontent.com/proxy/D7vQqu3U7j2qWYtvzCLHodLRvMHt1Fq0F5s12lEp2YQc-RPwytgpqhRLhqzIZglky59F4-A-OtVXlmglO2CoS7CrkZk=s0-d-e1-ft#http://litebreeze.com/images/profile_small.jpg',profile_pic,'profile_small.jpg', "base64", "application/octet-stream");
$mail->Body = "This is a test picture: <img src=\"cid:profile_pic\" /></p>";
$mail->AltBody="This is text only alternative body.";
if(!$mail->Send())
{
echo "Error sending: " . $mail->ErrorInfo;
}
else
{
echo "Letter is sent";
}

You're doing several things wrong here.
addEmbeddedImage expects you to provide a local path to a file, not
a remote URL.
You are deliberately setting an incorrect MIME type
You have not quoted your cid.
I see no good reason to pull the image from google's proxy cache - just pull it directly from the source.
Fixes for all these:
$mail->AddStringEmbeddedImage(file_get_contents('http://litebreeze.com/images/profile_small.jpg'),'profile_pic','profile_small.jpg');
You're not using encryption - set $mail->SMTPSecure = 'tls'; and $mail->Port = 587;.
You're also using an old version of PHPMailer and have based your code on an obsolete example. Go get the latest.

Related

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' />";

Include an image in email PHP

I'm experiencing an issue where only the text from body is appearing the image is coming through broken, does anyone see where I might be going wrong?
<?php
require("php/PHPMailer.php");
require("php/SMTP.php");
if (isset($_GET['email'])) {
$emailID = $_GET['email'];
if($emailID = 1){
$mail = new PHPMailer\PHPMailer\PHPMailer();
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "";
$mail->Password = "";
$mail->SetFrom("");
$mail->Subject = "Test";
$mail->Body = '<html><body><p>My Image</p><img src="images/EmailHeader.png" width="75%"></body></html>';
$mail->AddAddress("");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
}
else {
echo "Message has been sent";
}
}
}
?>
The image URL you are using is relative. You will need to use the full URL.
Instead of:
<img src="images/EmailHeader.png" width="75%">
It needs to be an absolute URL that the public can see, such as:
<img src="https://image-website.com/images/EmailHeader.png" width="75%">
You can not upload image with relative path in Email. You have to use full path because when the mail is send it require whole path to fetch the image content.,
Replace this line,
$mail->Body = '<html><body><p>My Image</p><img src="images/EmailHeader.png" width="75%"></body></html>';
With this line,
$mail->Body = '<html><body><p>My Image</p><img src="http://your-domainname.com/images/EmailHeader.png" width="75%"></body></html>';
Another option is that you can use use base64 images for it. But in your case you have to give full path.

phpmailer sending but not receiving

Just finished setting up PHPMailer to send the PDF that is created from my html form (using FPDF, the pdf file is created without a problem). It says sent successfully but im not receiving anything?
I have checked other peoples code and it looks just like mine. Is there anything im doing wrong with the PHPmailer code at the bottom?
my host,username and password are all correct 100% as far as I know we dont use TLS or SSL. Maybe that has something to do with this?
My code:
require 'PHPMailer-master/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->SMTPDebug = 4;
$mail->Host ="*****";
$Mail->SMTPAuth = true; // enable SMTP authentication;
$mail->Username = "*****";
$mail->Password = "****";
$mail->Port = 587;
$mail->SMTPSecure = "tls";
$mail->From = "******";
$mail->FromName = "Jurgen Hof";
$mail->addAddress("testingaccount23#gmail.com", "Tester");
$mail->isHTML(true);
$mail->Subject = 'Test Leave Application';
$mail->Body = 'Test.';
$mail->AddAttachment("/var/www/html/leaveform/AlpineLeaveApplication.pdf");
if(!$mail->Send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
echo 'Email Sent Successfully!';
?>
You're setting lots of SMTP properties, but not actually asking it to send via SMTP! Add this:
$mail->isSMTP();
Your browser will then explode with debug output, so I suggest you turn down SMTPDebug = 2.

PHP Mailer - Internal Server Error

I have been trying to make the contact form on my website to work and I've spent weeks trying to figure it out and I couldn't.
Here's the problem - I purchased a web template and it came with the PHPMailer. I'm now done plugging my content into the template, but the contact form has been a pain. I've followed the instructions the best I know on the PHP file, but it's giving me an "Internal Server Error" when I am testing the contact form.
Here's the code that came with my purchase:
$name = trim($_POST['name']);
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$site_owners_email = 'name#mydomain.com'; // Replace this with your own email address
$site_owners_name = 'My Name'; // Replace with your name
try {
require_once('/Beta-BRC/php/PHPMailer/class.phpmailer.php');
$mail = new PHPMailer();
$mail->CharSet = 'UTF-8';
$mail->From = $email;
$mail->FromName = $name;
$mail->Subject = "[WEB Form] ".$subject;
$mail->AddAddress($site_owners_email, $site_owners_name);
$mail->Body = $message;
$mail->Mailer = "smtp";
$mail->Host = "smtp.gmail.com"; // Replace with your SMTP server address
$mail->Port = 465;
$mail->SMTPSecure = "SSL";
$mail->SMTPAuth = true; // Turn on SMTP authentication
$mail->Username = "name#mydomain.com"; // SMTP username
$mail->Password = "mypassword"; // SMTP password
//echo "true";
if($mail->Send()) {
echo "true";
} else {
echo "Error sending: " . $mail->ErrorInfo;
}
} catch (Exception $e) {
echo $e;
}
Quick note - I've alrealy tried using a GMAIL account on this part but it still does not work.
$mail->Username = "name#mydomain.com"; // SMTP username
$mail->Password = "mypassword"; // SMTP password
There's no need to log into Gmail with phpmailer. Below is an example of my phpmailer function using the default settings.
public function sendEmail($toaddress,$toname,$subject,$message){
if($template = file_get_contents('/home/username/domains/mydomain.com/public_html/html/email-template.html')){
$template = str_replace("[SUBJECT]",$subject,$template);
$template = str_replace("[CONTENT]",nl2br($message),$template);
$mailer = new PHPMailer;
$mailer->XMailer = "Organization Name 4.0.0";
if($this->is_logged_in()){
$mailer->AddCustomHeader("X-Originating-User-ID",$this->acct['id']);
}
$mailer->AddCustomHeader("X-Originating-IP",$_SERVER['REMOTE_ADDR']);
$mailer->setFrom("outbound#mydomain.com","From Name");
$mailer->AddAddress($toaddress,$toname);
$mailer->Subject = $subject;
$mailer->MsgHTML($template);
$mailer->AltBody = $message;
return $mailer->Send();
}else{
return false;
}
}
The email address listed doesn't actually exist. The email is just being sent from my server and phpmailer just says it's from that email address.
Try modifying my function to suit your needs and let me know how that works.
Note: You'll need to make sure your mail server is turned on for this to work
Although you don't have to use my function at all. Try debugging your code by checking some error logs on your server. Typically in the apache error logs (if you're running apache, however). Checking error logs is a huge part of troubleshooting your code and often can help you become more proactive.
I hope this helps even the slightest!
The specific cause of the Internal Server Error is the incorrect path you've supplied to the require_once statement that loads the PHPMailer class.
The path you've supplied is /Beta-BRC/php/PHPMailer/class.phpmailer.php, where the correct statement should be
require_once('/home/trsta/public_html/Beta-BRC/php/PHPMailer/class.phpmailer.php');
or perhaps more generally:
require_once($_SERVER['DOCUMENT_ROOT'].'/home/trsta/public_html/Beta-BRC/php/PHPMailer/class.phpmailer.php');
You've provided effectively a URL, but PHP requires the path in the server file system, which is not the same.
That should get you past this error. It's possible that there are others.

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.

Categories