-1

I'm going to try my best to not phrase this confusingly. I am trying to make a php script that can send an email to a specified address and I would like control over the name that the email reports it was send from(this way I can make it my company name), the address it was sent from(this way the recipient knows who to reply to if they need to) and the reply-to address. I found this tool online which is like what I would like on steroids. I just need the basics so that when a user tries to contact me or if I use this form to contact a user all the information is correct and its as simple as typing up a body and hitting send(as the name, reply to address, and address would just be constant variables). Heres the tool I found that I am trying to semi duplicate: http://emkei.cz

As you can see you can put in any info and it will send the email with that information. I Just need to make a simpler version of this.

Thanks for any help!

4
  • 3
    And you didn't tried at least one of the thousands solutions Google is displaying? Aug 26, 2012 at 8:41
  • I can't figure out how to customize the from name and address. I know how to make a simple script but I couldn't understand any of the advanced stuff. Aug 26, 2012 at 8:43
  • You don't really need Reply-To: unless you specifically want replies to a different address than the From: address.
    – tripleee
    Aug 26, 2012 at 8:47
  • true, but if i ever wanted to have the emails go straight to my personal email I'd like to know how(because all emails forward to it as is right now) Aug 26, 2012 at 8:48

2 Answers 2

2

Assuming I have read your question correctly, and with reference to the PHP manual (http://php.net/manual/en/function.mail.php) I think the syntax you need is:

<?php
$to      = '[email protected]';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: Webmaster <[email protected]>' . "\r\n" .
    'Reply-To: [email protected]' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
?>

etc?

There are loads of Headers to set, as appropriate... the manual is worth a read.

3
  • ok so judging off of this the only thing i was misunderstanding was the headers variable Aug 26, 2012 at 8:46
  • Yes :-) The $headers field is VERY powerful, so I commend a good read of the manual...
    – Andrew
    Aug 26, 2012 at 8:48
  • Thank you! thats exactly what i was looking for :) and sometimes google just doesn't understand that haha. and both of you really deserve the correct answer on this but u Did provide a reference link so I think I'll give it to you even though I do like his code snippet better. Thank you! Aug 26, 2012 at 8:53
2

Use this function:

function send_mail( $to, $subject, $body ) {
    $from = 'Company Name <[email protected]>';
    $message = "
        <html>
        <head>
            <title>Company - ". $subject ."</title>
        </head>
        <body>

        ". $body ."

        </body>
        </html>
    ";
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=utf-8" . "\r\n";
    $headers .= "X-Mailer: PHP/" . phpversion() . "\r\n";
    $headers .= "From: " . $from . "\r\n";
    // Send this message;
    $result = mail( $to, $subject, $message, $headers);
    return $result;
}
3
  • and again the headers variables are just used to define the reply to email, name, and sending email correct? Aug 26, 2012 at 8:47
  • @ViperCode, yes, proper headers are very important. You not only define sender, but also properly tell mail servers to correctly parse your message. For example, if you don't send proper headers your message will likely land in spam folder. Aug 26, 2012 at 11:22
  • Oh thats good to know, I'll have to look into all the options and test it sending it to myself before I fully implement it. Aug 27, 2012 at 5:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.