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' />";
Related
Something weird is going on when i'm trying to send an e-mail with attachment to gmail, using phpmailer.
Sometimes the email received after a delay of 5 minutes and sometimes it is never received. From phpmailer debugger i get that the email is actually sended, so i assume it has something to do with the attachment getting blocked by gmail.
The attachment is a zip file containing only .jpg and .pdf files.
If i remove the addAttachment('path/to/file') everything works fine.
The weird thing is that sometimes, even with the attachment, the email received immediately and other times the email never gets received.
Here is the code
$mail = new PHPMailer();
$mail->CharSet = 'UTF-8';
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->Debugoutput = 'html';
$mail->Host = 'my_host';
$mail->Port = 'my_port';
$mail->SMTPAuth = true;
$mail->Username = 'my_username#domain.com';
$mail->Password = 'my_password';
$mail->setFrom('my_username#domain.com', 'my_username#domain.com');
$mail->addAddress('test#gmail.com', 'test#gmail.com');
$mail->Subject = $subject;
$mail->msgHTML($msg);
$mail->AltBody = $subject;
$mail->addAttachment($zipfilename);
if($mail->send()){
echo "ok";
}else{
echo "error";
}
I don't have any clue why this is happening so any help will be appreciated.
EDIT:
I found out that with two changes everything works fine.
The changes are the setting
$mail->SMTPSecure = 'tls'
and i had a path like this ./path/to/file and changed it to 'path/to/file'.
I use phpmailer to send an email with a hyperlink on its body. I have this code:
$body = "<a href='".DIR."activate.php?x=$id&y=$activasion'>".DIR."activate.php?x=$id&y=$activasion</a>";
require('classes/PHPMailerAutoload.php');
$mail = new PHPMailer;
$mail->CharSet = "UTF-8";
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = SMTP_HOST; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = SMTP_USER; // SMTP username
$mail->Password = SMTP_PASSWORD; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
$mail->setFrom(SMTP_USER);
$mail->addAddress($to); // Add a recipient Name is optional
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AltBody = $altbody;
if(!$mail->send()) {
echo $mail->ErrorInfo;
}
When I send the email to a Gmail address and open it in Gmail the hyperlink looks fine (I can click on the link and redirect to the page).
But when I send it to Outlook the hyperlink looks like this:
[my.domain.com/activate.php?x=52&y=aa1fdf437c526ee219decc1ea72fc266]my.domain.com/activate.php?x=52&y=aa1fdf437c526ee219decc1ea72fc266
Any ideas on what might be wrong?
E-mail clients have different rendering engines.
It appears that gmail will render a link without the http:// or https:// protocol.
Outlook may still require it.
Try using a protocol-less (aka scheme-less) URL: //
$body = "<a href='//".DIR."activate.php?x=$id&y=$activasion'>".DIR."activate.php?x=$id&y=$activasion</a>";
EDIT:
Note from #Synchro in the comments:
Anonymous/relative protocol URLs are a bad idea in email because
unless you're in a webmail client, you have no base protocol to be
relative to, and so they just break. Make it explicit and it will work
everywhere, and these days it's hard to find a good excuse not to use
https.
In PHP I have the following code that sends an email. Is there someway to encode it so that the email, when printed by the recipient, automatically prints in landscape?
$email_subject = 'Crew List';
// instantiate mailer
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "tls"; // sets the prefix to the server
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587; // set the SMTP port for the GMAIL server
$mail->Username = "xxxxxx#gmail.com";
$mail->Password = "xxxxxx";
$mail->SetFrom("yyyyyy#gmail.com"); // set From:
$mail->AddAddress("zzzzzz#gmail.com"); // set To:
$mail->Subject = $email_subject; // set Subject:
$body = $msg;
* $mail->MsgHTML($body);
if ($mail->Send() === false)// send mail
{
apologize_index("Google was not able to connect, try again later");
}
$mail->ClearAddresses();
I have a solution you may have a try:
Most of the email clients in this world supports HTML format. so you may use CSS to have a try.
Please have a look:
Landscape printing from HTML
So, generate your email as HTML format, and combine those CSS.
The support for email clients is very mixed.
OR, why not just generate a landscape PDF as an attachment?
I think that's good option to make your email in the pdf format which should be in the landscape orientation.
I know this question has been asked before but my issue is a bit different. I use the php mailer to send a bulk of mails in a loop to customers from the database. In the body of the mail when there's no html stuff like body = "hello" the emails get delivered to the normal inbox. However, when I use html tags in $mail->Body then the email goes to spam. The code I use:
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'someone'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'someemail.com'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'tls';
$mail->From = 'someemail.com';
$mail->FromName = 'Mailer';
#$mail->addAddress($email); // Name is optional
$mail->addAddress('someemail.com');
$mail->AddEmbeddedImage('rr.jpg', 'logoimg', 'rr.jpg');
$mail->AddEmbeddedImage('bottom.gif', 'bottomimg', 'bottom.gif');
$mail->WordWrap = 100; // Set word wrap to 50 characters
$mail->isHTML(true); // Set email format to HTML
$username = "123";
$mail->Subject = 'Welcome';
$mail->Body = "Hello, welcome"
$mail->AltBody = 'Welcome ';
$mail->send()
The issue is only with gmail. All other major mail providers such as yahoo, hotmail and live are fine. The emails go to inbox. It may be because I use quite a lot of html, colors etc in the email. But I don't know why gmail moves the mail to spam simply because it looks like many other spam mails. Is there any solution?
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.