PHPMailer: hyperlink appears in brackets in outlook - php

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.

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

PHPMailer not working: Is there a new way of doing this?

I followed all of the instructions in this question:
SMTP connect() failed PHPmailer - PHP
But still I never succeeded in getting the PHPMailer to work. I searched elsewhere - no solutions.
You can view the results here: https://unidrones.co.za/JuneSecond
When I try to send a test email using my gmail account credentials, it returns the "SMTP connect() failed" error.
I am using this template code:
<?php
require 'PHPMailerAutoload.php';
if(isset($_POST['send']))
{
$email = $_POST['email'];
$password = $_POST['password'];
$to_id = $_POST['toid'];
$message = $_POST['message'];
$subject = $_POST['subject'];
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'ssl://smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = $email;
$mail->Password = $password;
$mail->addAddress($to_id);
$mail->Subject = $subject;
$mail->msgHTML($message);
if (!$mail->send()) {
$error = "Mailer Error: " . $mail->ErrorInfo;
echo '<p id="para">'.$error.'</p>';
}
else {
echo '<p id="para">Message sent!</p>';
}
}
else{
echo '<p id="para">Please enter valid data</p>';
}
?>
Edit: I don't know if there's a new way to send emails through PHP now. All the tutorials and lessons I am using teaches it this way (i.e. using the PHPMailer library).
I had a tough time finding the PHPMailer library that includes the PHPMailerAutoload.php file, which makes me think it's a little outdated or deprecated, but how else would I send emails? I don't know.
The reason you're having a hard time finding PHPMailerAutoload.php is because it's old and no longer supported. Get the latest and base your code on the gmail example provided. If you're new to PHP, learn to use composer.
These three lines are conflicting:
$mail->Host = 'ssl://smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
The ssl:// in Host overrides the tls in SMTPSecure, resulting in it trying to use implicit TLS to a port expecting explicit TLS. Either use ssl with port 465 or tls with port 587, not other combos. Regardless, it appears that's not your problem anyway.
As the troubleshooting guide says about this exact "SMTP connect() failed" error:
This is often reported as a PHPMailer problem, but it's almost always down to local DNS failure, firewall blocking (for example as GoDaddy does) or another issue on your local network. It means that PHPMailer is unable to contact the SMTP server you have specified in the Host property, but doesn't say exactly why.
It then goes on to describe several techniques you can use to try to diagnose exactly why you can't connect. Amazing stuff, documentation.
I tried your form with some random data and saw that you failed to include the most important error message in your question:
2018-06-02 14:47:25 SMTP ERROR: Failed to connect to server: Network is unreachable (101)
2018-06-02 14:47:25 SMTP connect() failed
That suggests your ISP is probably blocking outbound SMTP, so you have your answer - perhaps confirm that using the steps the guide suggests (telnet etc), and refer to your ISP's docs or support.
You also have a major omission - you're not setting a "from" address:
$mail->setFrom('myname#gmail.com', 'My Name');
Note that if you're sending through gmail, you can only use your account's address, or preset aliases (set in gmail prefs), not arbitrary addresses.
Meanwhile, this is a somewhat crazy thing to implement as you have anyway - why would anyone ever enter their gmail credentials on a form like that?
did you try to use the following config?
$mail->Host = 'smtp.gmail.com';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->CharSet = 'UTF-8';
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 4; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'your gamil id'; // SMTP username
$mail->Password = 'gmail password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('set from address', 'name');
$mail->addAddress('recipient address', 'Joe User'); // Add a recipient
$mail->addReplyTo('reply address', 'Information');
// $mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
// $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$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 'Message has been sent';
}

Email Gateway PHPMailer in Codeigniter

i want to send email after user checkout from cart
my controller:
include('js/phpmailer/PHPMailerAutoload.php');
$mail = new PHPMailer();
$mail->Host = "ssl://smtp.gmail.com"; // SMTP server Gmail
$mail->Mailer = "smtp";
$mail->Port = 465;
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->SMTPDebug = 1;
$mail->Username = "my gmail"; //
$mail->Password = "my pass"; // SMTP password
$webmaster_email = "my gmail"; //Reply to this email ID
$email = "recipient gmail"; // Recipients email ID
$name = "John"; // Recipient's name
$mail->From = $webmaster_email;
$mail->FromName = "Aryono King";
$mail->AddAddress($email,$name);
$mail->AddReplyTo($webmaster_email,"Goeboek I-Mut");
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = "Subject Test";
$mail->Body = "Test Content"; //HTML Body
if(!$mail->Send()) {echo "Mailer Error: " . $mail->ErrorInfo;}
else {echo "<strong>Email Send</strong>";}
but it show error like this
2015-05-20 21:46:43 SMTP ERROR: Failed to connect to server: (0) 2015-05-20 21:46:43 SMTP connect() failed. Mailer Error: SMTP connect() failed.
what's the problem? i can't solve it, i search everywhere and i can't fint the answer, please someone help me
Take a look at the example here.
While personally, I do it like this:
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 0;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'ssl://smtp.googlemail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = $email_mail; // SMTP username
$mail->Password = $email_pass; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
Try to check against yours, like for example: $mail->Host = 'ssl://smtp.googlemail.com';, which supposed to be don't have ssl://.
Two things you need to check before using phpmailer for gmail
need to check whether SMTP port and email + password is correct or not
You need to authenticate from your gmail account for sending email from unsecured sources, means: when first you send an email, google will send you an email asking your permission to allow to forward email from your id, here your required to click allow
One more thing, Port 587 is working perfectly for me, instead of 465
Hi,
there's more...I have seen that antivirus or firewall installed in your computer may block sending emails via localhost as it may considered as spam attacks
use this code...
include 'PHPMailerAutoload.php';
function send_mail($mail_to,$mail_to_fname,$mail_to_lname,$subject,$message)
{ $mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->Debugoutput = 'html';
$mail->Host = 'mail.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = " YOUR GMAIL ";
$mail->Password = " YOUR GMAIL PASSWORD";
$mail->setFrom(' YOUR GMAIL ', ' YOUR NAME ');
$mail->addAddress($mail_to,$mail_to_fname . " " . $mail_to_lname);
$mail->Subject = $subject;
$mail->msgHTML($message);
$mail->AltBody = ' ';
if (!$mail->send()){echo "FALSE" . $mail->ErrorInfo;}
else{echo "TRUE";}
}
// How to user
// send_email(" email address where to send "," reciepient first name ","reciepient last name "," subject ", " message as html code ");
you need to do one more step for allowing gmail to send messages...!
(*) accept to allow google to send emails from unsecured apps, that link will be sent to your gmail, after you send first email

php html create email and print in landscape

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.

Mailer Error: SMTP Error: The following SMTP Error: Data not accepted

The following code gives the message
Mailer Error: SMTP Error: The following SMTP Error: Data not accepted. But when I replace $EmailAdd with a a#yahoo.com. The mail was sent.
What's wrong with my code? I'm kind of new in php, especially in dealing with mail functions.
$sql1 = "SELECT Email_Address FROM participantable where IDno=$studId";
$result1 = mysql_query($sql1);
while ($row1 = mysql_fetch_assoc($result1)){
$EmailADD = $row1["Email_Address"];
}
//error_reporting(E_ALL);
error_reporting(E_STRICT);
date_default_timezone_set('America/Toronto');
include("class.phpmailer.php");
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$mail = new PHPMailer();
$body = $mail->getFile('contents.html');
$body = eregi_replace("[\]",'',$body);
$mail->IsSMTP();
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = "jsbadiola#gmail.com"; // GMAIL username
$mail->Password = "********"; // GMAIL password
$mail->AddReplyTo("jsbadiola#gmail.com","Lord Skyhawk");
$mail->From = "jsbadiola#gmail.com";
$mail->FromName = "Update";
$mail->Subject = "PHPMailer Test Subject via gmail";
$mail->Body = "Hi,<br>This is the HTML BODY<br>"; //HTML Body
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->WordWrap = 50; // set word wrap
$mail->MsgHTML($body);
$mail->AddAddress($EmailADD, "Agta ka");
$mail->AddAttachment("images/phpmailer.gif"); // attachment
$mail->IsHTML(true); // send as HTML
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
$status = "Successfully Save!";
header("location: User_retsched.php?IDno=$studId&status=$status&Lname=$Lname&Fname=$Fname&Course=$Course&Year=$Year");
}
Most times I've seen this message the email gets successfully sent anyway, but not always. To debug, set:
$mail->SMTPDebug = true;
You can either echo the debug messages or log them using error_log():
// 'echo' or 'error_log'
$mail->Debugoutput = 'echo';
A likely candidate especially on a heavily loaded server are the SMTP timeouts:
// default is 10
$mail->Timeout = 60;
class.smtp.php also has a Timelimit property used for reads from the server.
It also helps if you have not exceeded your daily sending limit of Googles Spam inforcement.
check you query to the db output & variable value at that point in the script. make sure it's returning the desired value for $EmailADD .
do a var_dump($EmailADD, true);
or try to echo somewhere the output of that query. If your actually receiving an email value from that query, I don't see why it shouldn't work, specially when you mention that assigning a value directly works; without sql query.
Try a different SMTP server? See if that works?
Or just don't use an smtp server, most servers with PHP on also have sendmail/postfix, so can relay email themselves.
remove this bit ...
$mail->IsSMTP();
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = "jsbadiola#gmail.com"; // GMAIL username
$mail->Password = "alucar"; // GMAIL password

Categories