Solved

Implement PHP Mailer Code

Posted on 2014-04-11
5
767 Views
Last Modified: 2014-04-14
Hello.
I have an existing PHP script which emails a file to select recipients using the PHP mailer on the host server. However, I need to change this to use an authenticated SMTP server. I have the code from the hosting company to add the server, but do not know how to merge it with my existing PHP script. I am attaching the new smtp mail script and the part of my script dealing with emailing the file.
Thank you for any help.
smtpmail.php
orders-report-cron-test.php
0
Comment
Question by:chantalcookware
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39995141
It's not PHPMailer.
It's PEAR::Mail.

You're going to need both Mail and PEAR::Mail_Mime to send attachments.

Here's a tutorial for sending emails with attachments using Mail_Mime: http://www.tuxradar.com/practicalphp/15/5/3

HTH,
Dan
0
 
LVL 27

Expert Comment

by:skullnobrains
ID: 39995924
try this func to replace mail
you'll need to remove all headers up to the boundary declaration (included)
... and send the rest of your 'headers' as the body because they're actually the body

<?php
require_once "Mail.php";

function authmail($to, $subject, $body){


$from = "Web Master <webmaster@example.com>";
$body = "This is a test email message";
 
$host = "mail.emailsrvr.com";
$username = "webmaster@example.com";
$password = "yourPassword";
 
$headers = array ('From' => $from,
 'MIME-Version' => '1.0',
'Content-Type' => "multipart/mixed; boundary=\"".$uid."\"",
  'Subject' => $subject);

$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>");
}
}

Open in new window


note that
- my code is not good coding practice. more like a quick and dirty hack : the function contents should rather be sticked directly in your code where you call mail
- it is tryped directly in the forum so there may be a few typos
- the mail factury extension wants headers as an array : this is foolish since headers may be duplicated in a mail but is not a problem in your case
- there may be unexpected issues if the mailer tries to be smart and add headers of it's own or play with your body contents, or encodes it...
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39995936
@skullnobrains: the original code attached a csv file to the mail message.
0
 
LVL 27

Accepted Solution

by:
skullnobrains earned 500 total points
ID: 39995972
yes. i only edited the code they sent so it can be called from the existing code (see first 3 lines of my post)

the rest of the headers, which are the actual body contain the csv file

the merged result would be something like this. i'm not using the function i created but rather the code inside because $uid would not be passed and it is less messy like this anyway

$header should be understood as $body while $headers are the actual headers

function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $subject, $message) {
		$file = $path.$filename;
		$file_size = filesize($file);
		$handle = fopen($file, "r");
		$content = fread($handle, $file_size);
		fclose($handle);
		$content = chunk_split(base64_encode($content));
		$uid = md5(uniqid(time()));
		$name = basename($file);
		$header .= "This is a multi-part message in MIME format.\n";
		$header .= "--".$uid."\n";
		$header .= "Content-type:text/plain; charset=iso-8859-1\n";
		$header .= "Content-Transfer-Encoding: 7bit\n\n";
		$header .= $message."\n\n";
		$header .= "--".$uid."\n";
		$header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\n"; // use diff. tyoes here
		$header .= "Content-Transfer-Encoding: base64\n";
		$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\n\n";
		$header .= $content."\n\n";
		$header .= "--".$uid."--";


$from = "Web Master <webmaster@example.com>";
 
$host = "mail.emailsrvr.com";
$username = "webmaster@example.com";
$password = "yourPassword";
 
$headers = array ('From' => $from,
 'MIME-Version' => '1.0',
'Content-Type' => "multipart/mixed; boundary=\"".$uid."\"",
  'Subject' => $subject);

$smtp = Mail::factory('smtp',
  array ('host' => $host,
    'auth' => true,
    'username' => $username,
    'password' => $password));
 
$mail = $smtp->send($to, $headers, $header);
 
if (PEAR::isError($mail)) {
  echo("<p>" . $mail->getMessage() . "</p>");
} else {
  echo("<p>Message successfully sent!</p>");
}


	}

Open in new window

0
 

Author Closing Comment

by:chantalcookware
ID: 40000291
This gave me enough to get it working. Thank you!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question