Pear Mail stripping part of link - php

I have a bit of a strange one. Using PHP Pear Mail I am sending an HTML email with a link in to a subdomain http://mysub.mydomain.co.uk
$body='<html><body><strong>Hello '.$forename.'</strong><br><br>Thank you for registering your details. To complete the process, please follow the link below in this email.<br><br>
Complete Verification Here</body></html>';
$headers = array ('From' => $from,'To' => $to,'Subject' => $subject);
$mime = new Mail_mime();
$mime->setHTMLBody($body);
$headers = $mime->headers($headers);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password)
);
$mail = $smtp->send($to, $headers, $body);
The email gets sent fine and if I print the body of the email onto the screen from the page sending the email the link works fine. However when it arrives in an email in MS outlook it is taking the first 2 characters out of myvalue in the link. If myvalue=12345678 it says myvalue=345678 and displays the link incorrectly for example it shows the above as ttp://mysub.mydomain.co.uk/?V=345678. Notice it removes the H in the http address and also adds a forward slash before the ?v= and the first 2 digits 12 are also missing. It then fails to open the link as it is displaying it incorrectly as a http link. The email also does not arrive as an HTML email at Gmail and there is no link.
Any idea what I am missing here?

Eventually found the issue
In the body text I had to replace every occurrence in the body of
"
with ASCII code
&#039
Now working fine

You don't have to manipulate the body text outside of the mail_mime package, you need to mime encode the body with it:
$mime = new Mail_mime();
$mime->setHTMLBody($body);
$mimebody = $mime->get();
$headers = $mime->headers($headers);
$smtp = Mail::factory(
'smtp',
[
'host' => $host,
'auth' => true,
'username' => $username,
'password' => $password,
'port' => $port
]
);
// send email
$mail = $smtp->send($to, $headers, $mimebody);

Related

How to send logo in the body of email in php?

I am attaching my email code. I want to add an image in my body section with a message in this code.
require_once "../lib/Mail-1.4.1/Mail.php";
$from = 'someone#gmail.com';
$to = $mail;
$subject = 'Account Confirmation';
$body = 'Thanks for registering yourself, your login credentials are as
follows;'."\r\n".'Your User Name is: '.$uname.''."\r\n".'Your Password is:
'.$pass1.'';
$headers = array(
'From' => $from,
'To' => $to,
'Subject' => $subject
);
$smtp = Mail::factory('smtp', array(
'host' => 'ssl://smtp.gmail.com',
'port' => '465',
'auth' => true,
'username' => 'someone#gmail.com',
'password' => '12345678'
));
$mail = $smtp -> send($to, $headers, $body);
if(PEAR::isError($mail)){
echo '<p>'.$mail->getMessage().'</p>';
}
else{
echo "<script>alert('Successfully Registered! Your credentials have been
sent on email that you have given in the form')</script>";
}
this is my email code, in body I want to add an image/logo.
Use for embedding image AddEmbeddedImage
you can also add an image as inline attachment with the content ID of my-photo, you would access it within the HTML body using <img src="cid:my-photo" alt="my-photo">.
In detail, here is the function to add an embedded attachment:
$mail->addEmbeddedImage($filename, $cid);

PHP Pear Mail not sending email with special characters

I have the following problem. I have an online application form in HTML posting to a php file. The php file should send the contents via email. It works perfectly except when the client's name posting has special characters such as ΓΌ etc. It does not give any errors on form submission it just does not send the email.
This is my code:
$host = "192.168.10.14";
$username = "exxxx#xxx.com";
$password = "xxxxx";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = new Mail_mime(array("text_charset" => "utf-8",
"html_charset" => "utf-8",
"eol" => "\n"));
foreach ($headers as $name => $value){
$headers[$name] = $mail->encodeHeader($name, $value, "utf-8",
"quoted-printable");
}
// also encode to value
$to = $mail->encodeHeader("to", $to, "utf-8", "quoted-printable");
// fetch message
$msgDone = $mail->get();
// let Mail_Mime finish the headers (adds e.g. MIME info)
$headers_done = $mail->headers($headers);
// send the email
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail2)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
header("Refresh: 0;url=OK.html");
}
It works perfectly unless there are special characters. When there are special characters it does not send the email at all without any warnings.
Thanks
Have you tried cleaning your input before allowing it to be sent?
Would you allow an apostrophe to be entered in to an SQL statement without escaping it? Also check your mail logs on the server - that should provide a better hint to the underlying issue.
Thanks,
//P

Pear PHP Mailer Emails Sender and Recipient

I'm using Pear PHP Mailer to send an email through SMTP to a number of recipients, but for each one, it sends an additional email to the sender's email address as well.
This is my code: Any help is appreciated.
...
$from = "$from_email_name <$from_email_addr>";
$html_body = "$html";
$crlf = "\r\n";
$hdrs = array(
'From' => $from,
'Subject' => $subject
);
$mime = new Mail_mime(array('eol' => $crlf));
$mime->setTXTBody($text_body);
$mime->setHTMLBody($html_body);
$body = $mime->get();
$hdrs = $mime->headers($hdrs);
$mail = Mail::factory('mail',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail->send($to_email_addr, $hdrs, $body);
....
Also, on the email it is sending to my sender address, its saying the correct recipient name and sender information, but its like the sender is getting a copy of each one it sends out.
It could be a "service" of your mail server provider to automatically put mails sent via the SMTP server in your IMAP/POP mail box. Ask him if that's the case.

Issue with html content-type header when sending an php smtp mail through PEAR

I can send emails perfectly but they are showing on plain text, I have been trying a lot of solutions to display it as HTML trying to put content-type but when I check the headers from the email sent MIME and Content-type headers are missing. What I am doing wrong?
Thank you!
$email = "example#test.com";
$subject = "example subject\r\n";
$msg_body = "example body with some html";
$host = "mail.examplehost.com";
$username = "exampleemail#test.com";
$password = "examplepass";
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$headers = array ('From' => "exampledomain.com",
'To' => $to,
'Subject' => $subject
'MIME-Version' => "1.0",
'Content-type' => "text/html; charset=iso-8859-1"
);
$mail = $smtp->send($to,$headers,$msg_body);
In the headers array, you seem to be missing a comma after $subject

PHP SMTP BCC, not going through

Recipients named in the BCC/CC (in the headers) are not received.
I've found a couple of posts with similar questions, no answers...
The code is below, the question is: "Have any of you had similar problems?"
require_once "Mail.php";
$host = "mail.mailserver.com";
$username = "notification#yourhost.com";
$password = "getyourownpassword";
$headers = array ('From' => "User Name <$username>",
'To' => $to_,
'Cc' => 'Patty <patty#gmail.com>',
'Subject' => $subj_,
'Content-type' => 'text/html');
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to_, $headers, $mail_msg);
It looks like you're using the PEAR mail module.
if you read here You'll see a discussion about the headers passed to the pear module only specifies how the message looks, not to who actually gets it. If you add a CC header, that person will show up as being CC'd but to actually receive it he needs to be added to the recipients array. For BCC, you add them to recpients array, but don't show them in the header.

Categories