Jump to content

Recommended Posts

I can use this script to send a txt attachment, but not any other type.  I can't use it to send a jpeg.  The email goes through, only without an attachment if I try to send anything that isn't a text file.  Also, the text version of this email doesn't go through, only the html version.  I don't particularly mind not being able to send a text email, but it may be a symptom of why I can't send a non-text attachment.  Anyway, thanks for reading!

 

<?php

require_once "Mail.php";

require_once "Mail\mime.php";

 

$from = "John Doe <john@doe.com>";

$to = "Jane Doe <jane@doe.com>";

$subject = 'Test';

 

$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 = './logo.jpg'; // attachment

$crlf = "\n";

 

$mime = new Mail_mime($crlf);

$mime->setTXTBody($text);

$mime->setHTMLBody($html);

if($file)

$mime->addAttachment($file, 'image/jpeg');

 

//do not ever try to call these lines in reverse order

$body = $mime->get();

$headers = $mime->headers($headers);

 

$host = "mailserver.com";

$port = "25";

$username = "john";

$password = "pass1234";

 

$smtp = Mail::factory('smtp', array ('host' => $host, 'port' => $port, '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>");

}

?>

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/165527-pear-mime-attachment/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.