Hi i am new to PHP scripts but am learning fast.
I have apache, PHP and pear installed and am trying to send an email with a .jpg file attatched.
I have just about managed to do this with a lot of searching on the net.
BUT, HAVING A PROBLEM.
If i send from email1@*****.com to email2@yahoo.com via the PHP code then no picture is displayed in the
receiving email, it is a blank thumbnail. but the image can be downlaoded from the email.
If i send from email1@*****.com to email2@yahoo.com via outlook, then the picture is displayed in the receiving mail.
Confusing the H*** out of me.
WHY IS THIS HAPPENING ???
Can anyone solve this problem. Here is the code I found to use. THANK YOU.
Using Pear mail.php and mime.php
PHP Code:
<?php
require_once "c:/php/pear/mail/Mail.php"; // PEAR Mail package
require_once ('c:/php/pear/mime/mime.php'); // PEAR Mail_Mime packge
$from = "info@*******.com";
$to = "example@yahoo.com";
$subject = 'Picture from [email]info@******.com[/email]';
$headers = array ('From' => $from,'To' => $to, 'Subject' => $subject);
$text = 'Text version of email';// text and html versions of email.
//$html = '<html><body>HTML version of email. <strong>This should be bold</strong></body></html>';
$file = 'C:/pics/testpic1.jpg'; // attachment
$crlf = "\n";
$mime = new Mail_mime($crlf);
$mime->setTXTBody($text);
$mime->setHTMLBody($html);
$mime->addAttachment($file, 'text/plain');
//do not ever try to call these lines in reverse order
$body = $mime->get();
$headers = $mime->headers($headers);
//Smtp email authentication
$host = "mail.*******.com";
$username = "info@*******.com";
$password = "**********";
$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>");
}
else {
echo("<p>Message successfully sent!</p>");
}
?>
Email with .jpg attatchment PROBLEM. Help Needed.
Moderator: General Moderators
-
thegadgitman
- Forum Newbie
- Posts: 2
- Joined: Sun Jan 31, 2010 1:05 pm
-
jaiswarvipin
- Forum Commoner
- Posts: 32
- Joined: Sat Sep 12, 2009 3:43 pm
- Location: India
Re: Email with .jpg attatchment PROBLEM. Help Needed.
Use this function. THis iwll sloved you issue.
Code: Select all
function sendmsg($to, $subject, $msgtext, $from, $file, $type ,$paerameter=0)
{
if($paerameter==0)
{
if($file!="")
{
$fp = fopen($file,"rb");
$fcontent = fread($fp ,filesize($file));
fclose($fp);
$content = chunk_split(base64_encode($fcontent));
}
$sep = strtoupper(md5(uniqid(time())));
$name = basename($file);
$header = "From: $from\nReply-To: $from\n";
$header .= "MIME-Version: 1.0\n";
$header .= "Content-Type: multipart/mixed; boundary=$sep\n";
$body .= "--$sep\n";
$body .= "Content-Type: text/plain\n";
$body .= "Content-Transfer-Encoding: 8bit\n\n";
$body .= "$msgtext\n";
$body .= "--$sep\n";
$body .= "Content-Type: $type; name=\"$file\"\n";
$body .= "Content-Transfer-Encoding: base64\n";
$body .= "Content-Disposition: attachment; filename=\"$file\"\n";
$body .= "$content\n";
$body .= "--$sep--";
if (mail($to, $subject, $body, $header)) {
return true;
} else {
return false;
}
}
else
{
// To send HTML mail, the Content-type header must be set
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1; format=flowed\r\nContent-Transfer-Encoding: 8bit\r\n";
if(mail($to, $subject,$msgtext,$headers)){
return true;
} else {
return false;
}
}
}