Using PHP to Send an E-mail

lionsgatelionsgate MemberNAT Warrior
Sending an e-mail in PHP is another relatively simple task. An e-mail is sent through PHP using the mail() function.

[php]mail($address, $subject, $body);[/ph]

When using this function, $address should contain the e-mail address of the recipient. $subject should contain the subject heading of the e-mail. $body should contain the actual contents of the e-mail.

The following is an example of code that uses the mail() function:

[php]mail("jonesn@union.edu","My Subject", "Line1 \n Line2 \n Line3");[/php]

Note that, in the above example, /n is used to separate the different lines in the body of the e-mail. Since sending an e-mail that contains only one line would be inefficient, it is important to use these constructions to make larger e-mails easier to read.

Another common operation is concatenate-equals (.=). This operation takes the contents of the variable to the left of the operator and concatenates it with whatever is to the right. For example, consider the following code:

[php]$example1 = "straw";
$example2 = "berry";
$example1 .= $example2; [/php]

After executing this code, the variable $example1 will contain the string "strawberry". Below, we can see an example of how this would be used in the mail() function:
[php]
$mailto = $email;
$subject = "Thank you for signing up for our conference";
$body = "Hello ".$fname." ".$lname.",\n\n";
$body .= "Thank you for registering for our conference on \n";
$body .= "May 22nd, 2002 at Union College. We hope that this\n";
$body .= "will be an enjoyable experience. We look foreword to \n";
$body .= "seeing you there.\n\n";
$body .= "Thank You,\n";
$body .= "The Management\n";
mail($mailto,$subject,$body);
[/php]

Be creative! You can use information collected from forms as well as other data to send an e-mail to yourself or to a visitor that has just filled out a survey.

Comments

  • xPureNLxxPureNLx Moderator The Royal RAM
    hmm, tell me: is it a straight e-mail function without the use of third party software, i.e.: Outlook (express)?

    - xPureNLx
    signaru02am7.jpg
    [B]MSN: xPureNLx@gmail.com[/B]
    
  • lionsgatelionsgate Member NAT Warrior
    Its straight/direct PHP Email function. If you know PHP then you can easily make a email forum and use this script as sending emails.
  • chiragchirag Member NAT Warrior
    Thnx for the tut.
  • madelinekimmadelinekim Beginner Link Clerk
    This is really very nice information here. It is really good to learn for how to sent email by using php. Thanks for sharing this information here.
    Photoshop courses in melbourne
  • cloudmatecloudmate NAT Warrior

    @lionsgate said:
    Sending an e-mail in PHP is another relatively simple task. An e-mail is sent through PHP using the mail() function.

    [php]mail($address, $subject, $body);[/ph]

    When using this function, $address should contain the e-mail address of the recipient. $subject should contain the subject heading of the e-mail. $body should contain the actual contents of the e-mail.

    The following is an example of code that uses the mail() function:

    [php]mail("jonesn@union.edu","My Subject", "Line1 \n Line2 \n Line3");[/php]

    Note that, in the above example, /n is used to separate the different lines in the body of the e-mail. Since sending an e-mail that contains only one line would be inefficient, it is important to use these constructions to make larger e-mails easier to read.

    Another common operation is concatenate-equals (.=). This operation takes the contents of the variable to the left of the operator and concatenates it with whatever is to the right. For example, consider the following code:

    [php]$example1 = "straw";
    $example2 = "berry";
    $example1 .= $example2; [/php]

    After executing this code, the variable $example1 will contain the string "strawberry". Below, we can see an example of how this would be used in the mail() function:
    [php]
    $mailto = $email;
    $subject = "Thank you for signing up for our conference";
    $body = "Hello ".$fname." ".$lname.",\n\n";
    $body .= "Thank you for registering for our conference on \n";
    $body .= "May 22nd, 2002 at Union College. We hope that this\n";
    $body .= "will be an enjoyable experience. We look foreword to \n";
    $body .= "seeing you there.\n\n";
    $body .= "Thank You,\n";
    $body .= "The Management\n";
    mail($mailto,$subject,$body);
    [/php]

    Be creative! You can use information collected from forms as well as other data to send an e-mail to yourself or to a visitor that has just filled out a survey.

    Direct Answer, It will not Work. Mail function is fully going into Spam most of the time. mail() is not object oriented. PHP developers generally hate to create $headers strings while sending emails using the mail() function because they require a lot of escaping.
    Use its alternative PHPmailer available on [Here](https://github.com/PHPMailer/PHPMailer "Here")

    I used mail() in my PHP CAS System and Most Emails went to spam or did not delivered. Also, the mail() function requires a local mail server to send out emails, which is not always trivial to set up. PHPMailer can use a non-local mail server (SMTP) if you have authentication.

    Shared Hosting | VPS Servers | Dedicated Servers

    Ankesh Anand
    LHY Technologies
    CEO
    ankesh@cloudmate.in

  • It is quite a unique thing I have never heard of. But, as a programmer I love this piece of code and would like to understand this more deeply.

Sign In or Register to comment.