/    Sign up×
Community /Pin to ProfileBookmark

Sending mail via PHP

Hi,
I’ve been hired to fix a broken website and the mail function isn’t working.
Here is the code:
` if ($_POST[“Email”]!=””) {
$message = <<<EOF
Thanks for registering…bla bla bla.
EOF;
$from = “[email protected]”;
$to = $_POST[“Email”];
$subject = “Registration”;
$headers = <<<EOF
From: {$from}
Reply-To: {$from}
X-Mailer: Website.co.uk-Registration-{$_SERVER[“REMOTE_ADDR”]}:{$_SERVER[“REMOTE_PORT”]}
EOF;
mail($to,$subject,$message,$headers);
}`

I don’t know if it is the X-Mailer line in the header which is not working. In PHP examples I’ve read, that line isn’t present, what exactly is it doing?

Thanks in advance!

to post a comment
PHP

11 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmJun 17.2019 — Usually headers are written with a newline between each line. MIght be simpler to just place the text into a var instead of the heredocs usage.

As for xmailer - i have never had to use it. But I do know that you have to use a from address that is from your own domain.

BTW - how do you know you have a problem anyway?
Copy linkTweet thisAlerts:
@TigjawauthorJun 17.2019 — @ginerjm#1604840

Thank you, I will try using a var instead.

I registered and didn't receive an email, so there does seem to be a problem. I just can't figure out what it might be. I'm quite new to web development, learning as I go, and the code snippet looks fine to me.
Copy linkTweet thisAlerts:
@ginerjmJun 17.2019 — This is a sample of how I include headers in my emails. While using the PHPMailer package is a much better email method, since you are new and using the php mail function you can continue to experiment with it. Try this for building your header:
<i>
</i> $headers = "From: $from_addr " . "rn" .
"Reply-To: $reply_addr" . "rn" .
'X-Mailer: PHP/' . phpversion();

I see here that I have used x-mailer in the past. No idea what it really does. Note the concatenation used to build the string and the CR/LF separating each piece.
Copy linkTweet thisAlerts:
@NogDogJun 17.2019 — IIRC, the mail protocol likes a carriage-return/new-line sequence between headers, so you might want to try:
<i>
</i>$headers = "From: {$from}rnReply-To: {$from}rnX-Mailer: Website.co.uk-Registration-{$_SERVER['REMOTE_ADDR']}:{$_SERVER['REMOTE_PORT']}";
Copy linkTweet thisAlerts:
@adityaJun 17.2019 — @ginerjm#1604865 can you give answer https://www.webdeveloper.com/d/384928-how-to-select-description-using-vid
Copy linkTweet thisAlerts:
@ginerjmJun 17.2019 — Does that have ANYTHING to do with this current topic? If not, then NO.
Copy linkTweet thisAlerts:
@adityaJun 17.2019 — @ginerjm#1604883 you have not understand what i am telling or you have not sql knowledge.
Copy linkTweet thisAlerts:
@ginerjmJun 17.2019 — I think that you do not understand how forums work.


If we wanted to work on some other topic we would go to that topic instead of coming to this one.
Copy linkTweet thisAlerts:
@TigjawauthorJun 19.2019 — @NogDog#1604869

@ginerjm#1604865

I will try those out, thank you both. I'll let you know how I get on. Perhaps it is a problem with using heredoc since you both avoid it.
Copy linkTweet thisAlerts:
@ginerjmJun 19.2019 — It is just an odd place to use the heredocs construct. That is usually more applicable when outputting a large block of html code, not for building a string in php code.
Copy linkTweet thisAlerts:
@TigjawauthorJun 25.2019 — Just wanted to say thank you for the help! The email is now working :)
×

Success!

Help @Tigjaw spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 3.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,

tipper: Anonymous,
tipped: article
amount: 10 SATS,
)...