Jump to content

Mail() Function


23.12.2012

Recommended Posts

I was trying to use the PHP mail() function with additional headers (particularly From, Cc, Bcc), but it seems I've got something wrong, because the script fails to send the messages (it's not a mail server configuration error, because if I remove the additional headers, it works again). I'd also specify that I enter more than one address in the $to variable, but I think I'm doing it the right way: user1@domain1.tld, user2@domain2.tld, userN@domainN.tld. Any help here?

$to = $_POST['to'];$subject = $_POST['subject'];$text = $_POST['text'];$cc = $_POST['cc'];$bcc = $_POST['bcc'];$header = 'From: Me <me5@domain.tld>' . '\r\n';$header .= 'Cc: ' . $cc . '\r\n';$header .= 'Bcc: ' . $bcc . '\r\n';// Code...mail($to, $subject, $text, $header);

Link to comment
Share on other sites

Yeah, to make it not spam you have to set the from domain to be the same domain that the SMTP server sits on. So if you said it comes from domain.com but your SMTP server is actually at example.com, many mail agents will consider that spam.

Link to comment
Share on other sites

Now I'm trying to send using PEAR::Mail. I installed PEAR under htdocs. Everything seemed fine. Then I edited php.ini, and added the path of the PEAR implementation. Now that's how my php.ini looks, on a MAMP installation.

; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").sendmail_path = /usr/sbin/sendmail -t -i

; UNIX: "/path1:/path2"  include_path = ".:/Applications/MAMP/bin/php5/lib/php:/Applications/MAMP/htdocs/PEAR"

I don't know what else I need to do in order to get the SMTP authentication to work. Here's PEAR's sendmail.php, followed by smtp.php

var $sendmail_path = '/usr/sbin/sendmail';var $sendmail_args = '-t -i';

var $host = 'smtp.gmail.com';var $port = 465;var $auth = true;var $username = 'my_gmail_address';var $password = 'my_gmail_password';

Last but not least, here's the part of the script which sends the mail

include('/Applications/MAMP/htdocs/PEAR/Mail.php');$recipients = $to;$headers['MIME-Version'] = '1.0';$headers['Content-type'] = 'text/html; charset=iso-8859-1';$headers['From'] = 'my_gmail_address';$headers['To'] = $to;$headers['Subject'] = $subject;$headers['Cc'] = $cc;$headers['Bcc'] = $bcc;$body = $text;$params['sendmail_path'] = '/usr/sbin/sendmail -t -i';// Create the mail object using the Mail::factory method$mail_object =& Mail::factory('smtp', $params);$mail_object->send($recipients, $headers, $body);// Display a confirmation messageecho "The message has been successfully sent";

What am I doing wrong?

Link to comment
Share on other sites

Just for future reference, but a single-quoted string does not escape any characters other than a single quote. This string:'\r\n' will have those 4 characters in it. This string:"\r\n"will have a carriage return followed by a line feed.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...