PHP Pear Mail not sending email with special characters - php

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

Related

Pear PHP Mail sending CC doesn't seem to work

I'm using PEAR's mail library via SMTP on my server and whilst I can get emails to generate, adding CC's doesn't seem to work. Basically the CC recipients never get their email even though the main recipient of the same email does.
My basic setup is as follows, all the recipient variables ($to,$cc,$bcc) are string variables containing either a single recipient email addresses or comma separated email addresses.
$headers = array (
'From' => $from,
'To' => $to,
'Cc' => $cc,
'Bcc' => $bcc,
'Subject' => $subject,
'Reply-To' => $from,
'X-Mailer' => 'PHP/' . phpversion(),
'MIME-Version' => '1.0',
'Content-Type' => 'text/html; charset=ISO-8859-1'
);
$smtp = Mail::factory('smtp', array (
'host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password
));
$result = $smtp->send($to, $headers, $message);
I've read that sending BCCs is more complex, so lets stick to the CCs... why aren't they being received ? Is there anything obvious that I'm doing wrong here ?
In order to send an email to CC or BCC with SMTP, you must list all email addresses both under recipients to the send() function and in the CC key within the header.
$to = "john#example.com";
$cc = "doe#example.com";
$recipients = $to . ", " . $cc;
$headers["From"] = "john#example.com";
$headers["To"] = $to;
$headers["Subject"] = "Hello World!";
$headers["Cc"] = "doe#example.com";
$headers["Reply-To"] = "john#example.com";
$send = $mail->send($recipients, $headers, $body);

Pear Mail stripping part of link

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);

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

Date not in mail

I have the following script to send email using SMTP authentication. Everything works fine but date is not shown in my inbox for this mail. It is marked as '?'. Can anybody help me?
require_once "Mail.php";
$from = "test#example";
$to = $mailto;
$subject = $subject;
$body = $msg;
$host = "test.com";
$username = "uname";
$password = "password";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject,
'Content-type'=>'text/html');
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
//echo("<p>" . $mail->getMessage() . "</p>");
return false;
} else {
//echo("<p>Message successfully sent!</p>");
return true;
}
The from,host,username and password values shown here are only test values for security.
Is it any problem with mail headers?
solved it? this worked for me, i just added in the headers array:
'Date' => date('r', time()),

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