Include an image in email PHP - 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.

Related

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.

PHPmailer how to show message

How I can show success message after to click send in my form? my phpmailer is working but I want to also after to click send to redirect to my homepage (index.html) and show my message div. This is my php code.
<?php
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$subject = $_POST['subject'];
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'mail.xxxxxxx.pl'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'xxxxx#xxxxxxxx.pl'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom($email, $name);
$mail->addAddress('xxxx#gmail.com', 'xxxxxxxx'); // Add a recipient // Name is optional
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = 'Body test';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
header('Location: index.html');
}
And this is my Jquery code to show/hidden div succes:
$('.box-up').animate({top : 150}, 'normal').delay(3000).animate({top : -500}, 'normal');
You could send extra parameter with your request :
header('Location: index.php?r=1');
Then in your index page get the parameter and use it to show the box :
<script>
var result="<?php echo $_GET['r']; ?>";
if(parseInt(result)){
$('.box-up').animate({top : 150}, 'normal').delay(3000).animate({top : -500}, 'normal');
}
</script>
Hope this helps.

how to send mail in php with image as body of the mail using PHP Mailer

$message.="<img src='cf_logo.png'/>";
the above code does not show the image in php
I need to show the image as body of the message.Not an attachment.
You need the full path:
<img src='add your domain here'/cf_logo.png>
Try below code:
$mail = new PHPMailer();
$mail->isSMTP(); // send via SMTP
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
//$mail->SMTPDebug = 2;
$mail->Host = $host; //SMTP server
$mail->Port = $port; //set the SMTP port; 587 for TLS
$mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for Gmail
if (strlen($username)) {
$mail->SMTPAuth = true;
$mail->Username = $username;
$mail->Password = $password;
} else {
$mail->SMTPAuth = false;
}
$mail->From = $from; // FROM EMAIL
$mail->FromName = $fromName; // FROM NAME
$mail->addAddress($to);
$mail->addReplyTo($from);
$mail->IsHTML($html);
$mail->Subject = $subject; // YOUR SUBJECT
$mail->Body = $message; // YOUR BODY
$mail->addAttachment($attachment); // YOUR ATTACHMENT FILE PATH
if ($mail->send()) {
echo "Message Sent";exit;
} else {
echo "Message Not Sent<br>";
echo "Mailer Error: " . $mail->ErrorInfo;exit;
}
Note: You need to include php mailer class.
Try this, it might work., Use http:// in the front of your image url like this
http://yoursite/folder/image.jpg

PhpMailer: loading a while and then ERR_EMPTY_RESPONSE

I am dealing with something strange..
I am setting phpmailer and if I have an error I get the error normally
echo "Mailer Error: " . $mail->ErrorInfo;
actually if everything is good, the page loads a while and then it stops loading, getting in chrome the error: ERR_EMPTY_RESPONSE (Impossible to load the page because the server didn't load the data)
This is the content
<?php
$mail = new PHPMailer();
// set mailer to use SMTP
$mail->IsSMTP();
$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.gmail.com";
$mail->Port = 645;
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "myemail#gmail.com"; // SMTP username
$mail->Password = "my password"; // SMTP password
$email = 'myemail#gmail.com';
$mail->From = $email;
$mail->AddAddress("to#example.com", "Name");
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = "Subject of the mail";
$mail->Body = "content";
$mail->AltBody = "content";
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
?>
of course I have included this files:
require_once('../library/class.phpmailer.php');
require_once('../library/PHPMailerAutoload.php');
Two mistakes - just load the autoloader, that loads the class for you so that's all you need.
You've set Port = 645; I suspect you meant 465.
For gmail you should follow the example from the docs: use Port = 587 and SMTPSecure = 'tls'.
you need to Upload the PHPMailer to 6.5.
beacause the php 7.x it's diferent
look that the code change a little bit

Prevent Image blocking while sending emails using 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.

Categories