简体   繁体   中英

Sending an email using php mail function - change default email address

I am trying to send a confirmation email to people who sign up at my site.

The problem is that the email is being sent from my default cpanel email account (which, according to Hostgator cannot be changed).

I have other email addresses set up in cpanel. Can't I send using one of them?

            // ---------------- SEND MAIL FORM ----------------
            // send e-mail to ...
            $to=$user;
            // Your subject
            $subject="Confirmation link";

            // From
            $header="from: your name <your email>";
            // Your message
            $message="Your confirmation link \r\n";
            $message.="Click on this link to activate your account \r\n";
            $message.="http://mysite.co/confirmation.php?passkey=$confirm_code";
            // send email
            $sentmail = mail($to,$subject,$message,$header);

You can change your From email address supposing your desired From address is on the same domain and your ISP and permissions allow. I've never tried it with an email from another domain, and I'd be surprised if that worked. Just put the email you'd like to use as the From address.

But, if you've tried that and it doesn't work, you can also try adding a fifth argument to the mail() function

$sentmail = mail($to,$subject,$message,$header, "-f newemail@email.com");

Use a mail class to send an e-mail. The mail() function is not "trusted".

Check out a useful resource here: https://github.com/PHPMailer/PHPMailer

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