简体   繁体   中英

php mail(Net/SMTP.php) from address not working

I am using php mail(Net/SMTP.php) to process a form, everything is working fine, it sends the email, the only problem is that when I look at the email in my inbox, it sas that there is no sender. You can see in my program I set the from address. I am using shared hosting, my shared hosting provider is planethippo.

    <?php
ini_set("include_path", '/home/gaborszi/php:' . ini_get("include_path") );

echo htmlspecialchars($_POST['name']);

require 'Net/SMTP.php';

$host = 'localhost';
$from = 'gaborszita@gaborszita.tk';
$rcpt = array('gaborszita@yahoo.com', 'gaborszita@gaborszita.tk');
$subj = "Subject: Test Message\n";
$body = "Name: " . htmlspecialchars($_POST['name']);

/* Create a new Net_SMTP object. */
if (! ($smtp = new Net_SMTP($host))) {
    die("Unable to instantiate Net_SMTP object\n");
}

/* Connect to the SMTP server. */
if (PEAR::isError($e = $smtp->connect())) {
    die($e->getMessage() . "\n");
}
$smtp->auth('gaborszita@gaborszita.tk','mypassword');
/* Send the 'MAIL FROM:' SMTP command. */
if (PEAR::isError($smtp->mailFrom($from))) {
    die("Unable to set sender to <$from>\n");
}

/* Address the message to each of the recipients. */
foreach ($rcpt as $to) {
    if (PEAR::isError($res = $smtp->rcptTo($to))) {
        die("Unable to add recipient <$to>: " . $res->getMessage() . "\n");
    }
}

/* Set the body of the message. */
if (PEAR::isError($smtp->data($subj . "\r\n" . $body))) {
    die("Unable to send data\n");
}

/* Disconnect from the SMTP server. */
$smtp->disconnect();

my inbox image

I also use PEAR on my server. I am confused by the line: "$rcpt = array('gaborszita@yahoo.com', 'gaborszita@gaborszita.tk');". I have a similar code that uses $email_to = "one.address@sample.com, two.address@sample.com"; and both addresses receive a copy. My code also includes a $from_address which is the real sender. That address is an e-mail account I set up just for sending me the form data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM